Python core programming-Chapter 3-personal notes, chapter 3 of python
1. Statements and syntax
(1) The Backslash "\" indicates that the statement continues. A good programming habit of python is that the last line contains no more than 80 characters. When there are too many characters in a line, you must use a backslash to wrap the line to continue the statement.
PS: You can use parentheses, braces, and braces to write across rows without a backslash. strings with three quotation marks can also be written across rows.
(2) Semicolon ";" multiple statements can be written in the same line. Although python supports this, this is not recommended for code readability.
2. Variable assignment
(1) In python, objects are passed by reference, rather than directly assigning values to objects, for example:
>>> a = 123>>> b = a>>> a123>>> b123>>> a = 456>>> a456>>> b123
Statement a = 123: Create integer 123 and variable a, and point a to this integer;
Statement B = a: Creates the variable B and points B to the integer 123 pointed to by.
When a is assigned a value again, the orientation of a changes, while that of B does not. Therefore, after a = 456 is executed, the value of a is 456, and that of B is still 123.
(2) multiple values, for example, x = y = z = 1. An object is assigned multiple variables.
(3) Multi-Element assignment the equal signs in this assignment method are tuples on both sides.
>>> x, y, z = 1, 2, 'ab'>>> x1>>> y2>>> z'ab'
Objects 1, 2, and 'AB' are assigned to x, y, and z respectively.
To improve code readability, brackets are added to the tuples on both sides.
>>> (x, y, z) = (1, 2, 'ab')>>> x1>>> y2>>> z'ab'
Multivariate assignment can be conveniently used for variable exchange.
>>> x = 123>>> y = 456>>> x123>>> y456>>> (x, y) = (y, x)>>> x456>>> y123
3. identifier
① An identifier can only contain letters, numbers, and underscores. The first character cannot be a number.
② Avoid the python keywords and built-in characters when defining characters. Python keywords include:
③ Using underscores as the prefix and suffix of variables in python indicates special variables, which has special significance for the interpreter. Therefore, we recommend that you avoid using underscores as the start of variable name characters.
4. Basic python Style
(1) Summary
① Annotations are necessary, but note that excessive annotations are not allowed, as shown in figure
X = 5 # assign a value to x 5
The comment here is unnecessary because it is a nonsense.
② The Good indent style is four spaces, avoiding the use of tab
③ Use a short and meaningful identifier name
(2) module structure and layout
① Typical python module structure,
② Use local variables instead of global variables unless necessary
③ Apart from the code that really needs to be executed, the code that must be executed when the module needs to be imported. Almost all functional code should be encapsulated in functions or classes for the main program to call.
④ Check whether the python module is imported or directly run. Use the _ name _ system variable:
If the module is imported, the value of __name _ is the module name.
If the module is executed directly, the value of __name _ is "_ main __"
5. Memory Management
(1) variable definition
① In python, the name and type do not need to be declared before the variable is used. It will be automatically declared when the variable is assigned for the first time.
② Variables still need to be created and assigned values before they can be used.
③ After the variable is assigned a value. Direct access using variable names
(2) reference count
Python uses "reference count" to keep track of objects in memory. An internal trace variable is called a reference counter. Each object has multiple references, referred to as reference count. When the reference count of an object is 0, it is recycled.
① Add reference count
When an object is created, a reference count is created. when an object is assigned to a variable, the reference count is counted as 1.
When the same object is assigned to another variable, or is passed to a function as a parameter, or as an element of a container object, the reference count of the object increases by 1.
X = 123 # The object is created and assigned a value. The reference count is 1y = x # The object is referenced to other variables. The reference count is added with 1 float (x) # passed to the function as a parameter, add 1list1 = ['AB', 'C', x] # as an element of the container. Add 1
② Reduce reference count
The reduction in the reference count includes the following situations:
A local reference is out of the scope of action. In this case, the local variables are destroyed at the end of the function operation, and the reference count of the object is reduced;
The object alias is destroyed by del.
del y# or del x
An alias of an object is assigned to other variables.
x = 456
Object removed from a container object
list1.remove(x)
The container object is destroyed.
Del list1
6. The first python Program
(1) try-Modify T-else Structure
Structure:
Try: pass # try sub-block is the code block that you want to check for errors. try t IOError, e: pass # try T sub-block handles errors. When an error occurs, execute this code block else: pass # The else code block is executed when the try sub-block is detected to be correct.