Python逻辑运算符优先级 [英] Python logical operator precedence

查看:131
本文介绍了Python逻辑运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个运算符在4 > 5 or 3 < 4 and 9 > 8中具有优先权?这将被评估为真还是假?

Which operator takes precedence in 4 > 5 or 3 < 4 and 9 > 8? Would this be evaluated to true or false?

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

I know that the statement 3 > 4 or (2 < 3 and 9 > 10) should obviously evaluate to false but I am not quite sure how python would read 4 > 5 or 3 < 4 and 9 > 8

推荐答案

比较在and之前执行,而在and之前执行.您可以在表达式文档中查找每个运算符的优先级.

Comparisons are executed before and, which in turn comes before or. You can look up the precedence of each operator in the expressions documentation.

因此您的表达式被解析为:

So your expression is parsed as:

(4 > 5) or ((3 < 4) and (9 > 8))

哪个归结为:

False or (True and True)

True.

这篇关于Python逻辑运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆