Python logical operator precedence [duplicate]

牧云@^-^@ 提交于 2019-12-25 05:13:33

问题


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

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


回答1:


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))

Which comes down to:

False or (True and True)

which is True.



来源:https://stackoverflow.com/questions/35439251/python-logical-operator-precedence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!