3, the operator
-
Assignment operator and the extended assignment operator
a_1 = 123 print(a_1) a_2 = 20 a_2 -= 5 print(a_2)
123
15
-
Arithmetic Operators
div_1 = 5/3 print(div_1) div_2 = 5//3 # 整除 print(div_2) div_3 = 5.3//3.2 print(div_3) mod = 23%7 # 求余 print(mod)
1.6666666666666667
1
1.0
2-
math module
import math print(math.sin(3.14/4))
0.706825181105366
-
-
Indexing operator
See 2, the string of Section 9 .
-
Comparison operators, and logical operators
symbol DEFINITIONS ==(!=) Determining whether the value of the variable is equal to two (not equal) is(is not) Determining whether two variables refer to the same object (not the same) s1 = '100' s2 = str(100) print(s1 == s2) print(s1 is s2) # s1 与 s2 引用的不是统一个字符串
True
Falsea = 30 b = 27 print(3**3<a and 5**2>b) print(3**3<a or 5**2>b) print(not 3**3<a)
False
True
False -
Ternary operator
a = 20 print('大于25') if a>25 else print('小于25') b = 25 print('大于25') if b>25 else (print('等于25') if b==25 else print('小于25'))
Less than 25
equal to 25a = 30 s = print('大于25'), '!' if a>25 else print('小于等于25') # 多条语句用逗号隔开:每条语句都会执行,返回多条语句的返回值组成元祖 print(s) # print()没有返回值,因此返回 None
Greater than 25
(None, '!')a = 30 s = print('大于25'); '!' if a>25 else print('小于等于25') # 多条语句用分号隔开:每条语句都会执行,返回第一条语句的返回值 print(s) # print()没有返回值,因此返回 None
Greater than 25
None -
in operator
print('a' in 'abc')
True