14、python逻辑运算符

1、and or  not 优先级

() > not > and > or

2、x or y

如果 x 为True,则返回 x 值;如果 x 为False,则返回 y 值。

1 1 or 2   # 1
2 0 or 2   # 2

3、x and y

如果 x 为True,则返回 y 值;如果 x 为False, 则and运算会结束,返回 x 的值。

1 1 and 2    # 2
2 3 and 0    # 0
3 0 and 2    # 0

4、混合例子

按照从左至右、优先级高的先执行的原则。

(说明:比较运算符的优先级高于逻辑运算符)

1 > 2 and 3 or 4 and 3 < 2 or not 4 > 5

# 执行步骤:
# not 4 > 5   True
# 1 > 2    False
# 3 < 2    False
# False and 3   False
# 4 and False    False
# False or False    False
# False or True     True 

# 最终的结果为True  
原文地址:https://www.cnblogs.com/yif930916/p/15036932.html