Python - Booleans evaluation order | SoloLearn: Learn to code for FREE!

+3

Python - Booleans evaluation order

print(1 or 0 and 0) outputs 1. Is it correct to say that, with no parentheses, Python evaluates boolean expressions right-to-left?

5/30/2019 5:10:01 PM

Paolo De Nictolis

3 Answers

New Answer

+5

No, it's not correct. Boolean AND has higher precedence than boolean OR. https://docs.python.org/3/reference/expressions.html#operator-precedence

+3

Paolo De Nictolis hi, First 0 and 0 get evaluated which becomes 0 then (1 or 0) remains which evaluated 1 or 0 is evaluate and gives 1 so output comes as 1 Have some 🍎 🍎 🍎 🍎 🍎

+2

And if my conclusion is correct, why does print(1 or 0 and 0) outputs 1?