Python core programming-Quick Start, python core programming

Source: Internet
Author: User

Python core programming-Quick Start, python core programming

The Spring Festival is finally over, returning to a full learning and research life. Open the long-overdue CSDN blog and see the official "blog Markdown Editor", which immediately gave me the desire to write, which is really a programmer's welfare. I used to read a variety of articles and books. I used MarkDownPad as my notes and liked MarkDown's concise syntax.

All in all. In order to try the effect, I will sort out the notes that I used to read "Python core programming" and try again.

Chapter 2 Quick Start
  • Use the string format operator in the print statement to replace characters.

    print "%s is %d" %("one",1)

    % S, % d, and % f are replaced with string, integer, and floating point data respectively.

  • Print statement redirection

     logfile = open('/tmp/mylog.txt','a') print >> logfile,'something....' logfile.close()

    Symbol> is used to redirect data. The exported code is exported to the log file mylog.txt.

  • Raw_input built-in function that reads keyboard input. The return value type is string.

    s = raw_input('some tips:')
  • Operator single oblique rod/traditional division, double slash // used as floating point division, rounding.

  • The naming rules for variable names in python are the same as those in most other advanced languages and are affected by the C language. (Python itself is written in C)
  • Python is a dynamic language and does not need to declare the variable type in advance. The variable type is determined at the moment of value assignment.
  • Python is a dynamic language and does not need to declare the variable type in advance. The variable type is determined at the moment of value assignment.
  • Python's long integer can express a range far beyond the C language's long integer, limited by the total virtual memory of the computer, do not worry about overflow.
  • Something interesting: 1.1 cannot be accurately expressed in binary format. 2.2, 3.3, and many numbers cannot be accurately expressed in binary format. However, python provides the decimal floating point decimal module for exact representation.
  • Tuples: a read-only list that cannot be modified.
  • Dictionary

    ADict = {"one": 1, "two": 2} aDict. keys () # output ['one', 'two'] for key in aDict: print key, aDict [key]
  • The for loop in python is not the same as the traditional for loop (counter loop). It is more like the foreach iteration in shell. In python, for accepts iteratable objects as its parameters, and one element of each iteration. In for statements, range and len functions are often used.

    for i in range(len(A)):            print A[i]
  • List parsing, which is very practical

    spdEvens = [x**2 for x in range(8) if not x%2]
  • File and built-in functions open (), file ()

    handle = open(file_name,access_mode = 'rb')

    R indicates the Read mode, B Indicates binary access, w indicates the write mode (overwrite the original), a indicates the Add mode (not overwrite), and + indicates the read/write mode.

    Open () returns the file handle. Subsequent operations such as readlines () and close () can be performed through this handle, such as handle. close ()....

  • Try-try t

    The code group after try, that is, the code you plan to manage. The code group after the counter T is the code that handles the error.

  • A function must be defined before it is called (the called function must be above the called function ).

  • If the function does not have a return statement, the system automatically returns the None object.
  • The start and end methods with two underscores (_) are special methods. For exampleName,Init
  • Module, which can contain Execution Code, functions, and classes .. The py file is a module.
  • Common functions: int (), str (), range (), len (), raw_input (), ttype (), dir ([obj]), help ([obj])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.