Python Forum

Full Version: Boolean: Anybody know why is this True?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
a, b, c = 9, 10, 11

Print ( 5>a and b<8 or 20>c>9)
>>> True
Because 20 > 11 > 9
The or makes it True
(Feb-05-2022, 09:40 AM)LilD Wrote: [ -> ]a, b, c = 9, 10, 11

Print ( 5>a and b<8 or 20>c>9)
>>> True
Based on the hierarchy, shouldn't we look at "and" first instead of "or"?
Or am I misunderstanding something?
Does this help?
Output:
>>> False or True True
(Feb-05-2022, 12:37 PM)LilD Wrote: [ -> ]Based on the hierarchy, shouldn't we look at "and" first instead of "or"?
Or am I misunderstanding something?
It will look at the AND first but all that is saying is if both parts of the AND are true then it's true. In this case this fails so it would say false.
Then it also has to look at the OR and finds it to be true so the answer will be true.
Perhaps it would be simpler to understand if it were bracketed.
(if A is true AND B is true) OR if C is true.
So it's saying, in effect, if the first part is true OR if the second part is true the answer will be True.
Break it up into digestible portions.
a, b, c = 9, 10, 11
t1 = 5 > a
t2 = b > 8
t3 = t1 and t2
t4 = 20 > c > 9
t5 = t3 or t4
print(f"t1={t1}, t2={t2}, t3={t3}, t4={t4}, t5={t5}")
Output:
Ft1=False, t2=True, t3=False, t4=True, t5=Truee
t1 is False because 9 is not less than 5
t2 is True because 10 is greater than 8
t3 is False because "and" requires both expressions (t1 and t2) to be True
t4 is True because 11 is between 9 and 20
t5 is True because "or" is True if either expression (t3 or t4) is True

The order I used, t1, t2, t3..., is the order that python uses to evaluate the expression. Actually, that is not correct. Python never evaluates the t2 expresssion "b > 8". As mentioned, for "x and y" to be True, both "x" and "y" must be True. If the "x" expression evaluates to be False, there is no reason to evaluate the "y" expression. The result will be False no matter what "y" is, so Python skips evaluating "y".

If you reorder your expression
print (20>c>9 or 5>a and b<8)
It still prints True, but now it only evaluates 20>c>9. Because this evaluates True Python doesn't need to evaluate 5>a or b<8. The results cannot change the value of the expression, so Python skips evaluating the results.
(Feb-05-2022, 02:51 PM)Barrowman Wrote: [ -> ]
(Feb-05-2022, 12:37 PM)LilD Wrote: [ -> ]Based on the hierarchy, shouldn't we look at "and" first instead of "or"?
Or am I misunderstanding something?
It will look at the AND first but all that is saying is if both parts of the AND are true then it's true. In this case this fails so it would say false.
Then it also has to look at the OR and finds it to be true so the answer will be true.
Perhaps it would be simpler to understand if it were bracketed.
(if A is true AND B is true) OR if C is true.
So it's saying, in effect, if the first part is true OR if the second part is true the answer will be True.
Oh, so it doesn't ignore the "or" if the "and" is false. It still evaluates the "or" which gives True. Interesting~ Thank you!
(Feb-05-2022, 05:49 PM)deanhystad Wrote: [ -> ]Break it up into digestible portions.
a, b, c = 9, 10, 11
t1 = 5 > a
t2 = b > 8
t3 = t1 and t2
t4 = 20 > c > 9
t5 = t3 or t4
print(f"t1={t1}, t2={t2}, t3={t3}, t4={t4}, t5={t5}")
Output:
Ft1=False, t2=True, t3=False, t4=True, t5=Truee
t1 is False because 9 is not less than 5
t2 is True because 10 is greater than 8
t3 is False because "and" requires both expressions (t1 and t2) to be True
t4 is True because 11 is between 9 and 20
t5 is True because "or" is True if either expression (t3 or t4) is True

The order I used, t1, t2, t3..., is the order that python uses to evaluate the expression. Actually, that is not correct. Python never evaluates the t2 expresssion "b > 8". As mentioned, for "x and y" to be True, both "x" and "y" must be True. If the "x" expression evaluates to be False, there is no reason to evaluate the "y" expression. The result will be False no matter what "y" is, so Python skips evaluating "y".

If you reorder your expression
print (20>c>9 or 5>a and b<8)
It still prints True, but now it only evaluates 20>c>9. Because this evaluates True Python doesn't need to evaluate 5>a or b<8. The results cannot change the value of the expression, so Python skips evaluating the results.
Wow! Thanks! This is so clear. Initially, I expected a False and Python showed True so I was confused. But now, it is very clear.