Python operator, a logic operation

Operators

  There are operations that can be performed a variety of computer, can not only simple addition, subtraction, according to the type of operation can be divided into arithmetic, comparison operations, logical operators, assignment operators, members of the operation, operation status, bit operation, and today we learn arithmetic only temporarily operations, comparison operations, logical operators, assignment operators, members of the operation

Arithmetic

Suppose the following variables: a = 10, b = 20

Comparison operation

Suppose the following variables: a = 10, b = 20

Assignment Operators

Suppose the following variables: a = 10, b = 20

logic operation! ! !

Priority, ()> not> and> or the same priority, from left to right
 1 #and or not
 2 #优先级,() > not > and > or  同一优先级,从左到右
 3 print(2>1 and 1<4)      #True
 4 print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
 5 #先算and  T or T or F 结果True
 6 #练习题:
 7 print(3>4 or 4<3 and 1==1)
 8 # F or F 结果:F
 9 print(1 < 2 and 3 < 4 or 1>2)
10 # T or F 结果:T
11 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
12 # T or F 结果:T
13 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
14 # F or F or F 结果:F
15 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
16 # F or F and T or F--> F or F or F 结果:F
17 print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
18 # F and T or F or F 结果:F

  x or y ; x为真,则返回x ,x为假则返回y;

  and 与上相反 x为真,则返回y

 1 print(1 or 2)   # 1
 2 print(3 or 2)   # 3
 3 print(0 or 2)   # 2
 4 print(0 or 100)   # 100
 5 
 6 print(2 or 100 or 3 or 4)   #2
 7 print(0 or 4 and 3 or 2)    #3
 8 
 9 print(2 or 1 < 3)   #2
10 print(0 or 1 < 3)   #True
11 print(1 < 3 or 2)   #True
12 print(3 > 1 or 2 and 2) #True
13 print(1 > 2 and 3 or 4 and 3 < 2) #false 

数字,布尔值转换:
  非0转换成布尔值--True 0转换成布尔值--False
  True-->1, False-->0

Guess you like

Origin www.cnblogs.com/RevelationTruth/p/11443904.html