天天看點

python關系運算符和邏輯運算符的優先級,Python邏輯運算符優先級

python關系運算符和邏輯運算符的優先級,Python邏輯運算符優先級

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

解決方案

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.