Python Forum
Boolean: Anybody know why is this True?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Boolean: Anybody know why is this True?
#1
a, b, c = 9, 10, 11

Print ( 5>a and b<8 or 20>c>9)
>>> True
Reply
#2
Because 20 > 11 > 9
The or makes it True
Larz60+ and BashBedlam like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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
Reply
#4
Based on the hierarchy, shouldn't we look at "and" first instead of "or"?
Or am I misunderstanding something?
Reply
#5
Does this help?
Output:
>>> False or True True
Reply
#6
(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.
menator01 and LilD like this post
Reply
#7
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.
LilD likes this post
Reply
#8
(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!
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,037 Jan-09-2022, 02:39 AM
Last Post: Python84
  Returning True or False vs. True or None trevorkavanaugh 6 9,114 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020