Statements and Syntax
1) the pound sign (#) indicates that the following character is a Python comment
2) line break (\ n) is a standard line delimiter (usually one line in a statement)
3) backslash (\) to continue on line, the following two cases can not be used \
在使用闭合操作符时,单一语句可以跨多行。比如
()[]{}
三引号包括下的字符串也可以跨行
4) semicolon (;) Connect two statements in one row,即同一行可以书写多个语句。
5) colon (:) separates the head and body of the code block
6) statements (blocks of code) are represented by indented blocks, with different indentation depths separating different blocks of code
7) Python files are organized as modules
variables
Variables do not need to be declared in advance and are declared automatically on first assignment
A variable does not need to specify a type, the type of the object, and the memory footprint are determined by the runtime
Variables can be used only after they have been created and assigned values
assigning values to variables
赋值操作符
(=)
赋值并不是直接将一个值赋给一个变量,而是将对象的引用赋值给变量。
在
python
语言中,对象是通过引用传递的。
Python
的赋值语句不会返回值。链式赋值没问题
增量赋值
(+=…):
增量赋值时,第一个对象仅被处理一次。可变对象会被就地修改,不可变对象则和
A=A+B
的结果一样(分配一个新对象)
多重赋值:
将一个对象赋值给了对个变量。当然也可以将多个对象赋值给多个变量。
将多个变量同时赋值:
这种方式赋值,等号两边的对象都是元组
,
通过这种赋值方式可以实现无需中间变量就可以交换两个变量的值。
Basic Style Guide
Note: Comments must not be missing or over-
Documentation: Dynamically obtaining a document string by __DOC__ special variables
Indent: Preferably with 4 spaces, tab tab in different editors to set it differently, avoid using.
Special usage of the underline in Python:
1) _xxx do not import with ' from module import * '
2) __xxx__ System definition Name
3) Private variable name in the __xxx class
module structure and layout
Start line: The starting line is usually used only in a Unix-like environment, so that you do not need to invoke the interpreter directly, but only enter the script name to execute the script.
Module Documentation: A brief description of the function of the module and the meaning of the important global variables, outside the module can be accessed via module.__doc__
Module Import: The module import code inside the function is not executed unless the function is executing
Variable definition: This is the global variable definition, and all functions in this module can be used directly. For ease of maintenance, improve performance and save memory, using local variables as much as possible.
Class definition statement: All classes need to be defined here. When the module is imported, class is executed and the classes are defined. Document variables for class: class.__doc__
Function definition statement: The function defined here can be a module name. The function name () is called externally, and the DEF statement is executed when the module is imported, and the function is defined as the function's document variable: function.__doc__
Main program: This part of the code executes regardless of whether the module is imported by another module or executed as a script. Usually include variable assignment, class definition, function definition, and then check __name__ to decide whether to invoke another function (main), which is a good place to put the test code. In addition, the UnitTest module (pyunit) is also available in the Python standard library and is a test framework that comes in handy when you need to perform formal system regression testing of components of a large system. Usually only a large number of executable code in the main program module, all other imported modules should have very few top-level execution code, all the function code should be encapsulated in the function or class.
__NAME__: The ability to detect whether the module is imported or executed directly at run time. If the module is imported, the value of __name__ is the module name, and if the module is executed directly, the value of __name__ is ' __main__ '
Memory Management
The Python interpreter assumes a complex task of memory management. Keep track of objects in memory by using reference counts (how many references each object has).
Increase the reference count case
object is created and assigned to a variable, the reference count of the object is set to 1;
objects are assigned to other variables
Object is passed as a parameter to a function, method, or class instance
object is assigned as a member of a Window object
object is added to a container
Reduce the reference count situation
When a reference to an object is destroyed, the reference count is reduced.
When a reference is out of its scope (function run ends)
When a variable is re-assigned to another object
Delete a variable using the DEL statement
An object is removed from a Window object
The reference count of the container object itself becomes 0
The first Python program
Python Learning notes--python Basics