table of Contents
2019/07/29 Learning finishing
Python basis
variable
First, what is a variable?
Variable from mathematics, computer languages can be stored or can be a value indicating the calculation result abstraction. Variables can be accessed via the variable name. Variables can be understood from the literal meaning: is the amount of change.
- Capacity: recording status in the real world, so that the computer can like people to identify things in the world.
- Change: the state of the real world will change happen.
Second, why have variable?
Variable is the basis of all codes as a program to find a label data stored in the memory, its role is to tell the program where to go you should find the next memory to use data
Third, the definition of variables
Variable is defined and required assignment.
- format:
Identifier (i.e., the name of the variable) + assignment operator (i.e., equal sign =) + value
- Example: a = 8
in the above formula, the definition of a variable, called "a", the stored data is 8; can also be understood, called "a" variable is assigned the value 8 - Define a variable time must be assigned to a variable, namely the initial value of the variable.
- You can modify the value of a variable in the program at any time, and Python will always record the latest value of the variable.
- In Python3, the need to specify the type of the definition of variables, may be derived according to a value equal sign behind type
Fourth, the variable naming conventions
Three ways:
1. large hump nomenclature. Example: myName
2. Small hump nomenclature. Example: MyName
3. underscore nomenclature. Example: my_name
Follow the rules:
- Variable names can only contain letters, numbers and underscores.
- Variable names may begin with a letter or an underscore, but can not start with a number.
- For example, the variable can be named
message_1
, but it is not named1_message
.
- Variable names can not contain spaces, but can use underscores to separate words therein.
- For example, variable names
greeting_message
feasible, but variable namesgreeting message
will lead to error.
- For example, variable names
- Python keywords and function names not be used as a variable name
- Do not use the Python reserved word that is used for special purposes, such as print.
- Variable names should be both short and descriptive.
- Example,
name
the ration
good,student_name
specifics_n
good,name_length
specificlength_of_persons_name
good. - L caution lowercase and uppercase letters O, they may be due to human error as the numbers 1 and 0;
constant
Variable is the amount of change, constants are the same amount. python syntax is not used arbitrarily defined constants, that is to say, python variable is defined in the constant nature. If you have to define constants, variable names must be all uppercase.
Python variable memory management
First, where variables exist
Variables are stored in memory among.
Two, Python garbage collection
2.1 reference count
Python reference counting garbage collector mainly based, supplemented generational recovered. Reference counting principle is that each object maintains a ob_ref , used to record the number of times the current object is referenced, that is, in the end to keep track of how many references to the object, occurs when the following four cases, the reference count of the object +1 , if the object reference counter is 0 , the clear memory object change
Third, the smallest integer pond
Python implementation int time there is a small integer pool. In order to avoid creating the same value is repeated application efficiency caused by the memory space, Python interpreter created when the starting pool small integer range [-5,256], small integer within the range of the object is global interpreter is reused within the range, garbage collection will never be recovered.
Three characteristic variables
- The value of the variable (value)
- Data type (type)
- Address (id)
a = 5
print(a) # 打印变量的值
# 5
print(type(a)) # 打印变量的数据类型
# <class 'int'>
print(id(a)) # 打印变量的地址
# 140711551603648
Assignment
First, the chain assignment
a = 10
b = 10
c = 10
d = 10
print(f'a:{a}, b:{b}, c:{c}, d:{d}')
# a:10, b:10, c:10, d:10
a = b = c = d = 10
print(f'a:{a}, b:{b}, c:{c}, d:{d}')
# a:10, b:10, c:10, d:10
Second, cross-assignment
x = 100
y = 200
temp = x
x = y
y = temp
print(f'x:{x}')
print(f'y:{y}')
# x:200
# y:100
x, y = y, x
print(f'x:{x}')
print(f'y:{y}')
# x:200
# y:100
Note
Notes that the interpretation of the code and description , its purpose is to allow people to more easily understand the code. Comments are preparing the program, write a statement to People program, program segment, functions, and so explanations or tips can improve the readability of the code.
Note the code division multi-line and single-line comments, single-line comment with #, three multi-line comments can be single / double quotes, tris quoted comment can be wrapped.
# 单行注释
'''
三单引号注释
三单引号注释
'''
"""
三双引号多行注释
三双引号多行注释
"""
Add comments principle
1. do not all add comments, or just need to find it difficult to understand the important part to add a comment to their own
2. Comments can either Chinese or English, but do not use Pinyin