Python BASICS (1): basic rules and assignments, 2015 python

Source: Internet
Author: User

Python BASICS (1): basic rules and assignments, 2015 python

Python has the following basic rules:
# Post-Annotation
\ N is the line Separator
\ Is to continue the previous line and separate long statements
; Semicolon connects two statements in one row
: Colon separates the code header and body
Code blocks are represented by indented blocks.
Separate different code blocks with different indentions
Python files are organized in the form of modules

Indent the recommended style and four spaces to avoid tabs.

 

Assignment Statement
In Python, the main assignment operator is equal sign (=)
A value assignment does not directly assign a value to a variable, but an object is passed through reference. Regardless of whether the variable is newly created or already exists, the reference of this object is assigned to the variable.

In C, the value assignment statement can be used as an expression and can return values. However, in Python, the value assignment statement does not return values. This makes the statement illegal.

>>> y = (x = x + 1) # assignments not expressions! File "<stdin>", line 1y = (x = x + 1)^SyntaxError: invalid syntax>>> if (a = 3):SyntaxError: invalid syntax

This avoids the problem of mistakenly writing = In the judgment statement. Is a good function.

In the same Python, chained assignment is correct.

>>> y = (x = x + 1) # assignments not expressions! File "<stdin>", line 1y = (x = x + 1)^SyntaxError: invalid syntax>>> if (a = 3):SyntaxError: invalid syntax

There are also incremental value assignment characters.

+=   -=   *=   /=   %=   **=<<=  >>=   &=   ^=   |=

Compared with normal assignment, the most meaningful change is that the first object is only processed once.
Python does not support pre-and post-auto-increment/auto-increment operations such as x ++ or -- x.
Multivariate assignment

>>> x, y, z = 1, 2, 'a string'>>> x1>>> y2>>> z'a string'>>> x, y, z(1, 2, 'a string')

When assigning values, the objects on both sides of the equal sign are treated as tuples.

Using the multivariate assignment method, you can directly exchange the value of the table volume without using the intermediate variable.

>>> x , y = 1, 2>>> x, y(1, 2)>>> x, y = y, x>>> x, y(2, 1)

 

Special underline identifier

_ Xxx do not use 'from module import * 'for import
_ Xxx _ system definition name
_ Private variable name in xxx class

Style tip: Avoid using underscores as the start of variable names
Underlines have special meanings for the interpreter and are symbols used by built-in identifiers. Therefore, do not use underscores as the beginning of the variable name. Generally, _ xxx is regarded as private and cannot be used outside the module or class. Therefore, it is a good habit to use _ xxx When the variable is private. Because _ xxx _ has special meanings, this style should be avoided when naming common variables.


Variable assignment
All values of Python variables are referenced. This design goes to its memory management mechanism. Python uses the simple technology of reference counting. The number of references of all objects in use is recorded internally.
When an object is created, a reference count is created. When the object is not required, that is, when the reference count is 0, it is reclaimed.
When an object is created and assigned a value to a variable, the reference count of the object is set to 1.
When the same object is assigned another variable, or is passed to a function, method, or class instance as a parameter, or is assigned as a member of a window object, A new reference or alias of this object is created (reference count plus 1)

x = 2y = x

In the first sentence, an integer object is created and assigned to x. X is the first reference, and the reference count of this object is set to 1. When the y = x statement is executed, no new object is created for y, but the reference count of this object is increased once. This is the increase in reference count. It is also called by functions as parameters, or when an object is added to a container object.
When a variable is assigned to another object, the reference count of the original object is automatically reduced by 1.

foo = 3foo = '123'

When 3 is created and assigned to foo, the reference count is 1. When it is assigned to the object '123', the reference count of 3 is automatically reduced by 1.
When the local reference is out of the scope, for example, when the function ends.
The object alias is explicitly destroyed.
The object alias is assigned to other objects.
Object removed from a window object
The window object itself is destroyed.
In these cases, the reference count will be reduced.

Related Article

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.