Python 逻辑运算符优先级(not,and,or)
先看案例:
>>> not 1 or 0 and 1 or 2 and 3 or 4 and 5
这条语句的结果是:3
而不是:5
因为 Python 中逻辑运算符有优先级:
not > and > or
所以上述语事实上是:
>>> (not 1) or (0 and 1) or (2 and 3) or (4 and 5)
这里需要注意,在 Python 中,4 and 5
的结果是 5,4 or 5
的结果是 4。