Statements and Syntax
# comment \ Translate carriage return, continue on the previous line, in the case of a long line of statements can be used to cut into more than one line, because of its poor readability is not recommended; Connect two statements to one line, poor readability, not recommended: The code's head and body separate statements (blocks of code) are indented to reflect different code levels. 4 spaces are recommended (do not use tab), because the number of spaces that the tab represents in different programming locales is not necessarily 4 python files are organized in a modular way, writing a. py end file actually writes a module
definition and assignment of variables
A=1: 1 Memory variables are stored in memory, A is a reference to a variable, Python is a dynamic language, and the variable and its type do not require a prior declaration of the type to be different from C and C+ + : A=1 No return value Note:1 The . C language Variable declaration must be at the beginning of the code and be preceded by all statements 2 . C++,java can declare variables anywhere, but must declare variable names and types 3 . Python can also declare variables anywhere, but when a variable is defined, the interpreter determines its type based on the value to the right of the equation . 4. Variables must be assigned before they can be used
Memory Management
Memory management
1234. del ability to directly free memory objects (reduce reference count of objects)
Reference count
Increase reference count
1. The object is created and its reference is assigned to the variable, the reference count plus 1 (example a=1234. Object becomes an element in the container object, reference count plus 1 (example list_test=['bypp','z', A])
Reduce reference count
12. The variable is assigned to another object, the reference count of the original object is reduced by 1 (example b=234 5. The container itself is destroyed (Example del list_test)
Automatic recovery mechanism (Python-Exclusive)
Note: Python memory recycling is given to a separate code, the garbage collector (which contains reference counters and a cyclic garbage collector), and the reference count is not immediately cleared (there may be circular calls) without having to tangle with circular reference collection, just remember that the garbage collector helps you automatically clean up the memory.
Instance:
x=1 # Create a Memory variable 1, pass a reference to the variable 1 to x, the reference count of 1 to 1y=x # # of the reference count to 2 y=2 # Create a new memory variable 2, pass the reference to the variable 2 to Y, Originally pointing to 1 y, now gives 2, so 1 of the reference count is reduced to 1 del x # Delete the reference x for Memory Object 1, now 1 no reference, now it is the target of the Python interpreter recycling
Python Object
in Python, where the object model is used to store data, the factory function used to generate the data type is essentially a class, and the result of the new data is that instantiating an object object has three characteristics: 1 . Identity: Memory address, can be confirmed with ID (), the same ID is the same object 2 . Type: Can be viewed with type (), the type of the return value is also an object 3. Value
identifiers
12 3 456 . Cannot use keywords, built-in built-in: automatically imported by the interpreter (provides basic functionality), can be considered as global variables,
Private underline identifiers
_xxx: Cannot import __xxx__ with from module import *: system-defined name __XXX: Private variable underline in class has special meaning for the interpreter, and is the symbol used by the built-in identifier, it is not recommended to start the custom variable with an underscore But if it is a private variable in the class, __xxx will be a good habit system variable __name__ will derive different values depending on how the python file is loaded. Python files are imported as modules: __name__= module name or file name python file is executed: __name__='__main__' when we use Python to write a software, there should be only one main program contains a large number of top-level code (that is, no indentation code, the Python interpreter reads to the top-level code will be executed immediately), the other. py file should have only a few top-level code, All functions should be encapsulated in functions or classes.
View Codeprogram writing basic Process
1 . Title 2 . Document Comments 3 . Module Import 4 . Variable definition 5 . class definition Statements 6 . function definition Statements 7 . Main program 8. Test code
Example:
#_ *_coding:utf-8_*_#!/usr/bin/env python" "This isAn example module" "Import Sys,os Debug=TrueclassTest:" "Testclass " "Pass def Main ():" "test func:return: " "Passif__name__ = ='__main__': Main ()
View Code
Introduction to Python program writing