First, arithmetic operators
1. Arithmetic Operators
The python arithmetic operators consistent with the mathematics, we use here x = 9, y = 2 to represent
Arithmetic operators | description | Examples |
---|---|---|
+ | Addition, two values are added value | x + y-->11 |
- | Save, two subtracted value | x - y-->7 |
* | Multiply, multiplying two value values | x * y-->18 |
/ | In addition, the return value obtained by dividing the integer and fractional part reserved | x / y-->4.5 |
// | Rounded, the return value obtained by dividing only the integer part reserved | x // y-->4 |
% | Modulo, returns only the remainder after division | x // y-->1 |
** | Power, a number of power take n | x ** y-->81 |
2. Comparison Operators
Here we use x = 9, y = 2 to represent
Comparison Operators | description | Examples |
---|---|---|
== | Equal, two values are equal value returns True, otherwise False | x == y -->False |
!= | It does not mean that the two values are equal True value is returned, otherwise True | x != y -->True |
> | more than the | x > y -->True |
>= | greater or equal to | x >= y-->True |
< | Less than | x < y -->False |
<= | Less than or equal | x <= y -->False |
3. assignment operator
except in python "=" This assignment symbol, there are chain assignment, cross assignment, assignment decompression, the presence of these assignment operator makes the code more compact
3.1 Augmented assignment
Assignment Operators | description | Examples |
---|---|---|
= | Simple assignment operator | x = 10 |
+= | Addition assignment operator | x + = 1 corresponds to x = x + 1 |
-= | Subtraction assignment operator | x - = 1 corresponds to x = x - 1 |
*= | Multiplication assignment operator | X =. 1 corresponds to X = X . 1 |
/= | Division assignment operator | x / = 1 corresponds to x = x / 1 |
//= | Take divisible assignment operator | x // 1 corresponds to x = x / / 1 |
%= | Modulo assignment operator | x% = 1 corresponds to x = x% 1 |
**= | Power assignment operator | Ditto |
3.2 Assignment chain
Chain assignment: the name assigned to multiple variables the same value simultaneously
x=10
y=x
z=y
z = y = x = 10 # 链式赋值
print(x, y, z) #output:10 10 10
3.3 Cross-Assignment
CROSS assignment: the two values exchange
# 原理:定义一个变量,在三者之间转换(C语言等)
m = 10
n = 20
print(m,n)
temp = m
m = n
n = temp
print(m,n) # output:20 10
# python写法
m,n=n,m # 交叉赋值
print(m,n) # output:20 10
3.4 Assignment decompression
Decompression assignment: to take out multiple values in the list, and then in turn be assigned to more than one variable name
salaries=[111,222,333,444,555]
mon0,mon1,mon2,mon3,mon4=salaries
print(mon0) # output:111
print(mon1) # output:222
print(mon2) # output:333
print(mon3) # output:444
print(mon4) # output:555
# 注意
# mon0,mon1,mon2,mon3=salaries # 对应的变量名少一个不行
# mon0,mon1,mon2,mon3,mon4,mon5=salaries # 对应的变量名多一个也不行