Python Learning Note 1 (basic syntax)

Source: Internet
Author: User

1.Python File Type:

    • Source code: The extension ends with a py. Python-written programs do not need to be compiled into binary code and can be run directly. Pyw is the source file for developing graphical interfaces under Windows.
    • BYTE code: The extension ends with PYC, is a compiled byte file and cannot be opened or modified with a text editing tool.
    • Optimized code: The extension ends with PYO, is an optimized source file and needs to be generated with a command line tool. The following compiles hello.py into Hello.pyo.
Python-o-M Py_compile hello.py

PS: The parameter-o means to generate the optimization code,-m indicates that the imported Py_compile module runs as a script

2. Naming rules

    • The variable name, package name, module name are usually lowercase, and you can use underscores
    • The class name is capitalized and the object name is lowercase. The properties and method names of the class are prefixed with an object. The private variable and private method of the class are prefixed with two underscores.
    • The function name is usually lowercase, and the readability of the name is increased with an underscore or the first letter of the word.

3. Specification of module Import

A module is a collection of classes or functions that handle a class of problems. In Python, to invoke the class of a standard library or other third-party library, you need to first use import or From....import ... Statement.

    • Import statement
Import  SYS               #  SYS module is a collection of functions that process the system environment print(sys.path)          #  output Python Environment lookup path  Print(sys.argv)          #   stores a list of input parameters. By default, the own parameter is the file name
    • From....import ... Statement (import only part of the module and create a reference to the imported object in the current namespace)
 from Import Path  from  Import argv Print (PATH) Print (argv)

4. Notes

Start with "#" plus a few spaces.

    • Chinese Note: If you have Chinese comments in your code, you must precede the Python file with the following notes.
#   -*-coding:utf-8-*-
    • Cross-platform annotations: If the Python program is running on the *nix system, it is best to add the following note to the front of the file.
# !/usr/bin/python

5. Segmentation of statements

    • Python is primarily wrapped to identify the end of a statement, so the line ends; But when a line writes multiple statements, it must be used;

PS: Usually one line is written with a single statement, not a semicolon

    • Python uses ' \ ' as a line break. In practice, it is common for a statement to be written in multiple lines.

6. Variables

    • The variable does not need to be declared, and if the variable is not assigned, Python will assume that the variable does not exist
    • Variables defined outside of a function can be global variables, usually defined at the beginning of a file

7. Data type

    • The types of numbers are integer, float, Boolean, fractional, and plural.

There is no normal type inside python, and any type is an object, and Python cannot modify the value of the object.

#   Two I is not the same object i = 1print= 2

To see the type of a variable, you can use the type class, which is a class of the _builtin_ module that returns the type of the variable or creates a new type. The _builtin_ module is a Python inline template, without an import statement.

 #Integral typei = 1Print(Type (i))#output <class ' int ' >#Long Integer typeL = 99999999999999999999990#when does python convert int to float relative to the operating system number of bitsPrinttype (l)#floating Point TypeF = 1.2#Boolean typeb =True#plural typec = 7 + 8jPrint(Type (c))#output <class ' complex ' >
    • Python has 3 ways to represent strings-single quotes, double quotes, and three quotes. Single quotes work the same as double quotes.

You can enter characters such as single quotation marks, double quotation marks, or line breaks in three quotation marks. (You can also use the escape character "\")

PS: Use double or triple quotation marks to directly output a string with special characters, without the need to use escape characters

#    Three quotation marks
""print (str)
#   Double quote ""print
' Hello World ' '    # There must be spaces between single and triple quotes, otherwise the interpreter does not recognize print

Another use of three quotation marks is to make a document string. Each Python object has an attribute _doc_, which is used to describe the purpose of the object.

#making doc documents in three quotesclassHello:" "Hello class" "                          #Description of the Hello class, which is present in the _DOC_ attribute of the class     defPrintheloo ():" "Print Hello world" "             #The description of the function, which is present in the _doc_ property of the function          Print("Hello world!")  Print(HELLO._DOC_)#Hello classPrint(HELLO.PRINTHELLO._DOC_)#Print Hello world

8. Operators and expressions

    • The new exponentiation operation (X**Y) is not supported by the self-added decrement operation.
Print "  ", the               0print"", 1.0/2.0)       #  0.5print"", 2 * * 3)          #   8
    • Relational operators <, <=, >, >= (Priority equality) precedence greater than = =,! =
    • Logical operators:

Logical vs: and Logical OR: or logical non: not

Print  not True) Print  and True) Print  and False) Print or  False)

Python Learning Note 1 (basic syntax)

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.