逻辑运算

概念:

Python基础之逻辑运算-LMLPHP

优先级() > not > and > or
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
# T or T or F
# T or F
# or 一真则真
print(3 > 4 or 4 < 3 and 1 == 1)
# F or F
print(1 < 2 and 3 < 4 or 1 > 2)
# T or F
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
# F or F or F
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 > 8 or 7 < 6)
# F or F or T or F
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# F or F and T or F
# F and T
# int ----> bool
# 非零转换为bool True
print(bool(-2)) # True
# 0 转换成 bool 是False
print(bool(0)) # False
# bool ----> int
print(int(True)) # 1
print(int(False)) # 0
# x or y, x为真,则返回x
print(1 or 2) # 1
print(3 or 2) # 3
print(0 or 2) # 2
print(0 or 100) # 100
print(2 or 100 or 3 or 4) # 2
# 拆解
1. print(0 or 4 and 3 or 2) # 3
2. print(0 or 3 or 2)
# and 和 or返回的是相反的
print(1 and 2) # 2
print(0 and 2) # 0
# 拆解
1. print(1 > 2 and 3 or 4 and 3 < 2) # False
2. print(False or False)
# 拆解
1. print(2 or 1 < 3 and 2) # 2
2. print(2 or 2)
# 拆解
1. print(3 > 1 or 2 and 2) # True
2. print(3 > 1 or 2)

如果上面这些你是ok的,那么逻辑运算就过关了

05-13 04:40