Notes on Python core programming-Quick Start

Source: Internet
Author: User

The Spring Festival finally over, return to enrich the study of life. Open the long-lost CSDN blog, see the official push "blog markdown Editor Online", let me immediately have the desire to write, is really the welfare of programmers. Before reading a variety of article books, are made with markdownpad notes, like and accustomed to markdown concise grammar.

All kinds of convenience. In order to try the results, the previous reading of "Python core programming" of the notebook to send up, also when reviewing.

Chapter II Quick Start
  • The character substitution function is implemented using the string format operator in the print statement.

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

    %s,%d,%f are replaced by string, Integer, floating-point type data, respectively.

  • Print statement redirection

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

    The symbol >> is used for redirection, and the above code adds the output to the log file mylog.txt.

  • Raw_input built-in function, read keyboard input, return value type is a string.

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

  • The naming conventions for variable names in Python, like most other high-level languages, are affected by the C language. (in fact, Python itself is written in C)
  • Python is a dynamic language and does not require the types of variables to be pre-declared. The type of the variable is determined at the moment that the value is assigned.
  • Python is a dynamic language and does not require the types of variables to be pre-declared. The type of the variable is determined at the moment that the value is assigned.
  • Python's long integer can be expressed in far more than the long-range C language, limited only by the total amount of virtual memory on the computer, without worrying about overflow.
  • Something interesting:1.1 cannot be accurately represented by binary, 2.2, 3.3 、、、 Many numbers cannot be represented in binary precision. However, the decimal floating-point decimal module is available in Python and can be represented precisely.
  • Tuples: can be viewed as a read-only list and cannot be modified.
  • Dictionary

    aDict = {"one":1,"two":2}aDict.keys()   #输出[‘one‘,‘two‘]forin aDict:print key,aDict[key]
  • The For loop in Python is not the same as a traditional for loop (counter loop), which is more like a foreach iteration in the shell. The for in Python accepts an iterative object as its argument, iterating over one of the elements at a time. The range and Len functions are often used in the for statement.

    forrange(len(A)):            print A[i]
  • List parsing, this is very practical

    spdEvens = [x**2forinrange(8ifnot x%2]
  • Files and built-in functions open (), file ()

    open‘rb‘)

    R represents the readable mode, b for binary access, and W for write mode (overwrites the original), a for Add mode (not overwritten), + for read and write mode.

    Open () returns a handle to the file handle, and subsequent operations such as ReadLines (), close () can be performed with this handle: such as Handle.close () ....

  • Try-except

    The code group after try, which is the code you intend to manage. The code group after except is the code that handles the error.

  • The function must be defined before it is called (the called function will be above the call).

  • If the function does not have a return statement, the None object is returned automatically.
  • It is a special method to have two underscores for all names starting and ending. such as name,init
  • module, which can contain execution code, functions, classes. The. py file is a module.
  • Commonly used functions: Int (), str (), range (), Len (), Raw_input (), Ttype (), dir ([obj]), help ([obj])

Notes on Python core programming-Quick Start

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.