Feb-05-2022, 09:40 AM
a, b, c = 9, 10, 11
Print ( 5>a and b<8 or 20>c>9)
>>> True
Print ( 5>a and b<8 or 20>c>9)
>>> True
Boolean: Anybody know why is this True?
|
Feb-05-2022, 09:40 AM
a, b, c = 9, 10, 11
Print ( 5>a and b<8 or 20>c>9) >>> True
Feb-05-2022, 09:46 AM
Because 20 > 11 > 9
The or makes it True
I welcome all feedback.
The only dumb question, is one that doesn't get asked. My Github How to post code using bbtags Download my project scripts
Feb-05-2022, 12:35 PM
Feb-05-2022, 12:37 PM
Based on the hierarchy, shouldn't we look at "and" first instead of "or"?
Or am I misunderstanding something?
Feb-05-2022, 02:29 PM
Does this help?
(Feb-05-2022, 12:37 PM)LilD Wrote: Based on the hierarchy, shouldn't we look at "and" first instead of "or"?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.
Feb-05-2022, 05:49 PM
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}") t1 is False because 9 is not less than 5t2 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-06-2022, 09:11 AM
(Feb-05-2022, 02:51 PM)Barrowman Wrote: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, 12:37 PM)LilD Wrote: Based on the hierarchy, shouldn't we look at "and" first instead of "or"?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.
Feb-06-2022, 09:15 AM
(Feb-05-2022, 05:49 PM)deanhystad Wrote: Break it up into digestible portions.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. |
|
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 | 3,237 |
Jan-09-2022, 02:39 AM Last Post: Python84 |
|
Returning True or False vs. True or None | trevorkavanaugh | 6 | 14,782 |
Apr-04-2019, 08:42 AM Last Post: DeaD_EyE |