A variable is a variable used to Container for saving data , Using variables is to use the data stored in variables
grammar : Variable name = data
Variable name :
requirement : Is an identifier but not a keyword
standard :
1. See the name and know the meaning ( When you see the variable name, you know what data is stored in the variable )
2. You cannot use system function names 、 Class name and module name
3. All letters need lowercase , Multiple words are separated by underscores
= : Assignment operator
data : It can be any expression with a result
for example : A specific data 、 Calculation results 、 Variables that have been assigned 、 Function call expression, etc .
age = 20
name = "wlstory"
num = 3 * 5
print(age, name, num)
Be careful : Variables must be defined before using
grammar : Variable name = The new data
a = 100
print(a,id(a))
# 100 140704131793792
a = 200
print(a,id(a))
# 200 140704131796992
id( Variable ) : Get the memory address of the variable
The new data is used in the variable after re assignment
Variable name 1 = Variable name 2 = Variable name 3 = ... = data a = b = c = 20
Variable name 1, Variable name 2, ..., Variable name n = data 1, data 2, ..., data n.—> ( The number of variable names must match the number of data The number is the same ). a,b = 18,20
python To define a variable, you need to apply for memory , The size of the memory request is determined according to the needs of the saved data .
When reassigning , Will reapply for memory , How big the new memory is depends on the new data , Then bind the variables , Free the original memory .
+( Add ), -( reduce ), *( ride ), /( except ), %( Remainder ), //( to be divisible by ), **( Power operation
+、 -、 .、 / And in mathematics +、-、×、÷ The function is as like as two peas 、 print(1+1) # 2
print(2-1) # 1
print(2*1) # 2
print(1/2) # 0.5
Be careful : / The result of the operation must be float; +、-、 * The type of the result of the operation depends on Is there a Floating point numbers %x % y —> seek x Divide y The remainder of # Extract the last digit of a five digit number
num = 235941
print(num % 10) # 1
// a = 50
print(a//10) # 5
** a = 15
print(a ** 2) # 225
Calculation 1000 Number of daffodils in
for i in range(100, 1000):
bw = i // 100
sw = i // 10 % 10
gw = i % 10
if ((bw**3) + (sw**3) + (gw**3)) == i:
print(i)
# 153
# 370
# 371
# 407
>( Greater than ), <( Less than ), ==( be equal to ), >=( Greater than or equal to ), <=( Less than or equal to ), !=( It's not equal to )
Be careful :
Small scale <= x <= large-scale and( Logic and ), or( Logic or ), not( Logic is not )
and At the same time, the conditions for establishment are met # Judge whether a number can be 3 and 7 to be divisible by
num = 27
print(num % 7 == 0 and num % 3 == 0) # False
or It can be established if one condition is met # Judgement of leap year
year = 2000
print(year % 4 == 0 and year % 100 != 0 or year % 400 == 0) # True
notTrue and False=、 +=、-=、*=、/=、//=、**=
a = 15
a *= 2
print(a) # 30
a /= 5
print(a) # 6.0
a //= 2
print(a) # 3.0
a **= 3
print(a) # 27.0
Conclusion : The function of all assignment operators is to Data is stored in variables
Assignment statement has no result , Data cannot be provided directly to the program
In mixed operations , Those with higher priority shall be calculated first , Low priority post computation Mathematical operators > Comparison operator > Logical operators > Assignment operator ( The minimum ) Power operator > *、/、//、% > +、- ( The minimum ) If there are brackets, count them first