Home > Blockchain >  Why is (False and False or True) true, if "and" makes any false propagate?
Why is (False and False or True) true, if "and" makes any false propagate?

Time:09-01

2>3 is false. For and, if there is one false in the whole expression, it should return false.

print(3>2 and 2>3 and 7>5 or 1>0)

Thank you,

CodePudding user response:

and has higher precedence than or, so it's equivalent to

print((3>2 and 2>3 and 7>5) or 1>0)

Since 1>0 is true, and or is true if either of its arguments is true, the whole expression is true.

I recommend using explicit parentheses whenever you have a mixture of and and or, since the default grouping is often not what you intend. It also differs between languages, so if you're used to one language you may have the wrong intuition when you write in another language.

CodePudding user response:

Python is doing this internally.

print((3>2 and 2>3 and 7>5) or 1>0)

For more understanding, Precedence and Associativity of Operators in Python - GeeksforGeeks

  • Related