Python逻辑运算符优先级

时间:2016-02-16 17:32:42

标签: python operators operator-precedence

哪个运算符优先于4 > 5 or 3 < 4 and 9 > 8?这会被评估为真还是假?

我知道语句3 > 4 or (2 < 3 and 9 > 10)显然应该评估为false但我不太确定python会如何阅读4 > 5 or 3 < 4 and 9 > 8

1 个答案:

答案 0 :(得分:2)

比较在and之前执行,而or之前执行比较(4 > 5) or ((3 < 4) and (9 > 8)) 。您可以在expressions documentation中查找每个运算符的优先级。

所以你的表达式被解析为:

False or (True and True)

归结为:

True

是{{1}}。