notes
-
1. What is a comment?
- Annotation is the explanation of the code. The content of annotation does not participate in the operation of the program, but only plays a role of prompt
-
2. Why comment?
- Enhance code readability
-
3. How do I use annotations?
- Code comments are divided into single line and multi line comments
- 1. Single line comments are # numbered and can be written directly above or after the code
- 2. Multiline comments can use three pairs of double quotation marks or single quotation marks """""
- Code comments are divided into single line and multi line comments
-
Principles of code annotation
-
1. You don't need to annotate all of them, just annotate the parts that you think are important or difficult to understand
-
2. Notes can be in Chinese or English, but not Pinyin
-
3. There must be a space between the single line comment # number and the comment text, and two spaces are required after the code#
-
variable
-
What are variables?
-
Quantity refers to the state of things, change refers to that the state of things can be changed, and variable refers to that the state of things can be recorded, and the recorded results can be changed
-
Variables are a mechanism for accessing memory
-
-
Why have variables?
- Let the computer remember the state of things like people, and the state can change
In detail:
The essence of program execution is a series of state changes. Change is the direct embodiment of program execution, so we need a mechanism to reflect or save the state and state changes during program execution.
Definition and use of variables
Define before reference
definition
name = "mike" # name age = 18 # Age height = 1.8 # height weight = 80 # weight
Three components
-
Variable name: used to find value
-
Assignment symbol: bind the memory address of the variable value to the variable name
-
Variable value: it is the data we store, or the state of recorded things
quote
print(age) # Print
Naming conventions and styles of variable names
- The premise of naming: the naming of variable names should see the meaning of names
age = 18 # Age level = 18 # Grade count = 18 # quantity
Naming conventions
-
It can only be composed of letters, numbers and underscores
-
Cannot start with a number. It is recommended not to start with an underscore because it has special meaning
-
You can't use python keywords. There will be conflicts
# Example: level_of_age = 18 _=19 # It can be used this way, but it is not recommended print(_)
Python keyword
'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else','except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda','not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'
Bad way to define variable names
-
Variable names are Chinese and Pinyin
-
Variable nouns do not convey meaning
Naming style
# 1. Hump body Big hump # All words are named with the first letter uppercase and the remaining letters lowercase LevelOfAge = 18 NumberOfStudents = 80 Small hump # The first word is lowercase and the other words are capitalized levelOfAge = 18 numberOfStudents = 80 # 2.Plain lowercase underlined # Words are separated by underscores level_of_age = 18 number_of_students = 80 """python Underline is recommended"""
Variable value
1. Three elements of variables
name='jason' print(name) # Value: the value of the variable print(id(name)) # id: it reflects the memory address of the variable. Different memory addresses must have different IDs print(type(name)) # Type: data type < class' STR '> age = 18 salary = 3.3 res = age + 1 print(res) # =============Type print(type(age)) print(type(salary)) # id = the id number in memory, id reflects memory. print(id(age)) print(id(salary))
is and ==
is : It judges the identity of the left and right values id Are they equal == : It determines whether the left and right values are equal
be careful:
(1) If the id is the same, it means that the type and value must be the same
x = 1000 y = x print(x is y)
(2) if the value is the same, the type must be the same, but the id may be different
C:\Users\oldboy>python3 Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> x=1000 >>> y=1000 >>> id(x)x 2577369562032 >>> id(y) 2577369562096 >>> >>> x is y False >>> x == y True >>>
2. Small integer pool * * [- 5256]
From the moment the python interpreter starts, it will apply for a series of memory space in advance to store commonly used integers
x=111111111111111 y=111111111111111 print(id(x)) print(id(y))
3. Garbage collection mechanism
Memory management: gc mechanism
Garbage: when the number of bound variable names of a variable value is 0, the variable value cannot be accessed, which is called garbage (used to recycle the value without any associated variable name)
Python's garbage collection mechanism, in fact, high-level languages have their own garbage collection mechanism, abbreviated as GC. Python mainly solves garbage collection in three ways:
Reference counting: core principles
Generational recycling: improving efficiency
Mark clearing: memory leakage caused by circular reference???
>Reference count: if a new reference points to an object, the object reference count will increase by one. When the reference is destroyed, the object reference count will decrease by one. When the user's reference count is 0, the memory will be released >Mark removal: mark the object first (garbage detection), and then remove the garbage (garbage collection) First, all objects are initially marked as white, and the root node objects (these objects will not be deleted) are identified and marked as black (indicating that the objects are valid). Object tags that reference valid objects It is gray (indicating that the objects are reachable, but the objects they reference have not been checked). After checking the objects referenced by the gray objects, the gray is marked as black. Repeat until no gray nodes exist. Finally, the white nodes are all objects that need to be cleared >Generation recycling:The garbage collector processes new objects more frequently. A new object is just created by your program, while an old object is an object that still exists after several time cycles. Python When an object moves from generation zero to generation one, or from generation one to generation two, it will be promoted(promote)This object. >>>In fact, the garbage collection mechanism is internal Cpython interpreter GIL The underlying principle of global locking. # Reference count increase x = 100 # The reference count for 100 is 1 y = x # The reference count for 100 is 2 z = x # The reference count for 100 is 3 # Reference count decrease del x # Unbind the variable name x from the value 100, and the reference count of 100 becomes 2 print(y) del y # The reference count for 100 becomes 1 print(z) z = 12345 # # The reference count for 100 becomes 0 print(z)
constant
It is mainly used to record some unchanged states
A constant is a constant quantity, such as pai 3.141592653... Or a quantity that will not change during the operation of the program
In python, there is no constant in the real sense, and we routinely regard all uppercase variables as constants
AGE = 18 print(AGE) HOST = '127.0.0.1' # Generally, it is used more in the configuration file In other programming languages, there are constants in the real sense, which cannot be modified once they are defined const age int = 18 # Define constants age = 19 # Modification is not supported
data type
- What is a data type?
In real life, there are many ways and manifestations of storing data
Text file table file video file audio file picture file... stay IT The storage methods and forms of data in the world are also changeable
Integer int of data type
-
Function: used to record the status related to age, number, number, and other integers
age = 18
Floating point float of data type
-
Function: used to record the status of height, weight, salary and other decimals
weight = 80.3