2015/8/26 Python Basics (1): Basic rules and assignments

Source: Internet
Author: User

Python has the following basic rules:
#后表示注释
\ n is the line delimiter
\ is to continue on the previous line, separating the long statement
; Semicolon joins two statements in one row
: Colon separates Dock and body
Code blocks are represented by indented blocks
Different indentation depths separate different blocks of code
Python files are organized in the form of modules

Indent the recommended style, indent four space widths, and avoid using tabs.

Assignment statements
In Python, the primary assignment operator is the equals sign (=)
An assignment is not to assign a value directly to a variable, and the object is passed by reference. Whether a variable is newly created or already exists, the reference to the object is assigned to the variable.

In C, an assignment statement can be an expression that returns a value. In Python, however, the assignment statement does not return a value. This makes the statement illegal.

# Assignments Not expressions! File "<stdin>", line 1y = (x = x + 1)^syntaxerror:invalid syntaxif (a = 3): Synt Axerror:invalid Syntax

This avoids the problem that the = = symbol is mistakenly written in the judgment statement. is a very good feature.

There is no problem with chained assignments in the same python.

# Assignments Not expressions! File "<stdin>", line 1y = (x = x + 1)^syntaxerror:invalid syntaxif (a = 3): Synt Axerror:invalid Syntax

There are also incremental assignments

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

The most significant change is that the first object is processed only once, relative to the normal assignment, which is more than just a change in wording.
Python does not support pre/post self-increment/decrement operations such as X + + or--x
Multi-value Assignment

' A String '>>> x1>>> y2>>> z'a string' >>> x, Y, Z ('a string')

The object on both sides of the equal sign is evaluated when the Narimoto group is assigned

Use multivariate assignment methods to exchange the value of a table quantity without using intermediate variables directly

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

Private underline identifiers

_xxx no ' from module import * ' Import
__xxx__ System Definition Name
Private variable names in the __xxx class

Style tip: Avoid using underscores as the start of variable names
Underscores have special meanings for the interpreter and are symbols used by built-in identifiers, so you should avoid underlining as the start of a variable name. Generally speaking, _xxx is considered private and is not available in modules or classes, so it is good practice to use _xxx when the variables are private. Because __xxx__ has a special meaning, the naming of ordinary variables should avoid this style


About the assignment portion of a variable
The assignment of a Python variable is a reference. This is designed to its memory management mechanism. Python uses the simple technique of reference counting. The internal records how many references each object in use has.
When an object is created, a reference count is created, which is garbage collected when the object is not needed, that is, the reference count is 0 o'clock.
When an object is created and assigned to a variable, the object's reference count is set to 1
When the same object is assigned to another variable, or is passed as a parameter to a function, method or class instance, or is assigned to a member of a Window object, a new reference or alias of that object is created (reference count plus 1)

x = 2= x

The first sentence creates an integer object and references the assigned value to X. X is the first reference, and the reference count of the object is set to 1. When the y = x statement is executed, no new object is created for Y, but the reference count of the object is incremented by 1 times. This is an increase in the reference count. There are also arguments that are called by a function, 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 = 3'123'

When 3 is created to assign to Foo, the reference count is 1. When it is re-assigned to object ' 123 ', 3 of the reference count automatically minus 1
When a local reference leaves the scope, such as when the function ends.
An object alias is explicitly destroyed
Object aliases are assigned to other objects
objects are removed from a Window object
The Window object itself is destroyed
These cases will reduce the reference count

2015/8/26 Python Basics (1): Basic rules and assignments

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.