问题
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