Python core programming basics, python core programming
The Spring Festival is finally over, returning to a full learning and research life. Open the long-overdue CSDN blog and see the official "blog Markdown Editor", which immediately gave me the desire to write, which is really a programmer's welfare. I used to read a variety of articles and books. I used MarkDownPad as my notes and liked MarkDown's concise syntax.
All in all. In order to try the effect, I will sort out the notes that I used to read "Python core programming" and try again.
Chapter 3 basics of Python 1. Statements and syntax
Note
The symbol "#" can be used for annotation in Python, or three quotation marks can be used: "'comment content "'
Continue
If the statement is too long, you can use a backslash to split a row into several lines:
if a==1 and \ b==0 :
Python indent Style
Pyhton uses indentation to separate code groups. indentation can be spaces or tabs. (4 spaces are recommended for indentation, and Tab keys are not recommended, because the tabs on different platforms represent different white spaces .)
Write multiple statements on the same line
Writing multiple statements on the same line is not recommended because the readability is greatly reduced. If you want to do this, it is also allowed.
import sys; x = 'foo'; sys.stdout.write(x)
Module
Import through import
2. Variable assignment
Multuple)
In this way, the objects on both sides of the equal sign are actually tuples:
x,y,z = 1,2,3
Essentially:
( x,y,z ) = (1,2,3)
This method of Multivariate assignment can realize the exchange of values of two variables without intermediate variables:
x,y = 1,2 x,y = y,x
Run the code. Values of x and y are exchanged.
3. identifier
Special underline identifier
_ XXX. import from module import * is not required.
_ XXX _, system defined name
_ XXX: private variable name in the class
Underlines are licensed to the parser and are the symbols used by built-in identifiers. Therefore, we recommend that you avoid using underscores as the beginning of the variable name.
4. Basic Python Style
The annotation, indentation, and flag style are described above. In addition, Python also provides a mechanism "document": You can use_ Doc _Variable to dynamically obtain the document string:
obj.__doc__
5. Memory Management
Variable definition
In most compiled languages, variables must be declared before they are used. Python, as a parsing language, does not need to display declaration variables. variables are automatically declared when they are assigned values for the first time.
Dynamic type
Python not only does not need to declare variables in advance, but also does not need to declare types. The object type and memory usage are determined at runtime.
Memory Allocation
Python undertakes the complex tasks of memory management, so programmers do not need to care about memory management.
Reference count
Python uses the reference count technique to track objects in memory.
Add reference count:
When an object is created, a reference is generated, and the reference count is set to 1. When the same object is assigned to another variable, or is passed to a function, method, or class instance as a parameter, or is assigned to a member of a window object, A new reference of this object is created, and the reference count is automatically increased by 1.
Reduce reference count:
When an object's reference is destroyed, the reference count is reduced. For example:
(1) When the function ends, local variables are automatically destroyed, and the reference count of objects is also reduced.
(2) The variable is re-assigned (the value is changed), and the reference count is reduced by 1.
(3) del obj: The reference count of obj is reduced by 1.
Garbage Collection
The Python garbage collector is actually a reference counter and a circular garbage collector.