This series of notes for learning Python for yourself, if there are errors, please correct me
Identifier
Basic usage
Identifier: Used for variables. Function, class. The name of the module, etc. Identifiers have the following specific rules
- Case sensitive
- The first character must be a letter, underscore. The following characters are: alphanumeric underscore
- Cannot use keywords
- Names beginning and ending with underscores usually have special meanings, try to avoid this type of writing
Use the Python help system to view keywords
>>> help()
help>keywords
Python identifier naming rules
During development, we usually agree to observe the following rules, commonly known as:
Types of | rule | example |
---|---|---|
Module and package name | All lowercase letters, as simple as possible. If you use underscores between multiple words | math os sys |
Function name | Use underscores between multiple words with all lowercase letters | phone my_name |
Class name | Capitalize the first letter, using the camel case principle. For multiple words, the first letter of each word is capitalized and the rest is lowercase | MyPhone MyClass |
Constant name | All uppercase letters, multiple words separated by underscore | SPEED MAX_SPEED |
Variables and simple assignment statements
Variable declaration and assignment
Variable declaration and assignment are used to bind a variable to an object, the format is as follows
Variable name = expression
The simplest expression is a literal, such as a=123. During operation, the interpreter first runs the expression on the right to generate an object representing the result of the expression operation; then, assign the address of this object to the variable on the left
Variables must be initialized before use, otherwise it will report NameError: xxx is not defined
Delete variables and garbage collection mechanism
Variables that are no longer used can be deleted through the del statement
a = 123
del a
If the object has no variable references, it will be reclaimed by the garbage collection period and the memory space will be emptied.
Chain assignment
Chained assignment is used to assign the same object to multiple variables
x=y=123 is equivalent to x=123 y=123
Series unpacking assignment
Assign series data to variables corresponding to the same number
a,b,c=4,5,6 is equivalent to a=4 b=5 c=6
Use series unpacking assignment to realize variable exchange
a,b = 1,2
a,b=b,a
print(a,b)
constant
Python does not support constants, that is, there are no grammatical rules to restrict changing the value of a constant. We can only agree on the naming rules of constants, and do not modify the value of constants in the logic of the program
Search [Zixin] on WeChat or scan the QR code below to make friends and make progress together. The article is continuously updated. At present, I am organizing the study notes of Python hundred battles, and I look forward to more updates in the future.