Basic Python knowledge

Source: Internet
Author: User

Code style

Variables do not need to be declared, define type, use del + variable name to delete the variable

One line statement, without semicolons, indented with tab tab

Pass indicates an empty method body

Comments

    • Single Use #

Text output

    • 2.7: print ' text '
    • 3.x: Print (' text ')
    • Default output line end return, print (b, end= ', ') modified
    • Multiple variables are separated by commas, and spaces are added by default between variables
    • Remove escape interpretation of a string you can add the letter r:print (R ' C:\name ') before the first quotation mark
    • Formatted output print ('% (language) s has% (number) 03d quote types. '% {' language ': "Python", "Number": 2})

Get input

    • Inputtext=raw_input (' hinttext hint: ') #v2.7
    • Inputtext=input () #v3. x
    • Convert string to Integer: Int (inputtext)

In interactive mode, the variable "_" represents the last output

String

    • Single and double quotes function the same, three quotation marks (single/double) are used for multiple lines of string, the end of the line with \ means that the next line continues, not the new row
    • Gets the length Len (string)
    • Support for string repetition using "*";
    • Support for string connection using "+", lower priority than *
    • The two adjacent strings of the same constant are automatically concatenated (regardless of whether there are spaces in the middle), with precedence higher than +, typically used to divide long text into multiple lines
    • There is no char type in Python, only a string of size one
    • You can treat a string as an array of letters and use the subscript to access

      +--+--+--+--+--+--+
      | P | y | T | H | o | n |
      +--+--+--+--+--+--+
      0 1 2 3 4 5 6
      -6-5-4-3-2-1

    • [Startindex:endindex] to intercept part of the string, StartIndex is empty to start from the beginning, EndIndex is empty to the end

Number

    • The Division (/) return value is float, and if you want to get the same rounding as C or Java, use '//' instead
    • The ' * * ' operator denotes ' secondary ', with precedence higher than symbol, e.g. -3**2=-9

Multivariate assignment: A, B = B, a+b #等价于a =b;b=a+b;

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.