0

I don't understand how this is true

(False == True) or True

17th Feb 2019, 3:56 AM
Mista Ben
Mista Ben - avatar
16 ответов
+ 11
Check out the second lesson on this link https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2279/ (False == True) Is False So ((False == True) or True) Becomes (False or True) which is True because one of the 2 in the or is True.
17th Feb 2019, 4:21 AM
Louis
Louis - avatar
+ 8
Ben Python's or operator is a bit special. It will return the first value of an expression that evaluates to True. You could even use it like this: print(0 or False or None or [] or 12 or 4). The output will be 12 because 12, like any number that isn't 0, evaluates to True. 0, None, empty lists etc. all evaluate to False. So print(False or True) will print True because that's the first "true" value in the expression. You don't need to specify a condition
17th Feb 2019, 6:19 AM
Anna
Anna - avatar
17th Feb 2019, 6:21 AM
Louis
Louis - avatar
+ 6
Ben Don't confuse "or" with the word of the English language you use every day. "Do you want to go to A or to B?" expects either "A" or "B" as an answer (or maybe "neither"). Whereas in programming, the expected answer would be either yes or no (aka true or false). It's true if you want to go to either A or B (or maybe even to both A and B) and false if you want to go neither to A nor B. That's why "false or true" is true (one of the alternatives is true).
17th Feb 2019, 5:32 AM
Anna
Anna - avatar
+ 6
A or B is true if at least one of the alternatives is true A | B | A or B -------------------- 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 1
17th Feb 2019, 5:57 AM
Anna
Anna - avatar
+ 6
Anna Since I don't work with Python in my professional work, I didn't actually realize the OR in Python returns the first operand if it evaluates as truthy. Otherwise, it returns the second operand. Thanks for pointing that out.
17th Feb 2019, 6:42 AM
David Carroll
David Carroll - avatar
+ 5
Ben Yes, its logic.
17th Feb 2019, 4:55 AM
Louis
Louis - avatar
+ 5
Ben... You can tag someone by starting with the @, type a couple of letters, then select the name from the list that pops up.
17th Feb 2019, 6:16 AM
David Carroll
David Carroll - avatar
+ 4
Restating what Louis and Anna have already explained... In boolean logic, the OR operator returns True if either of the operands evaluates as True. It should be noted that if the first operand (on the left side) evaluates as True, the second operand (on the right side) will not be evaluated. This is because the first operand satisfied the condition for returning True. If the first operand evaluates as False, then the result will essentially be based on the boolean evaluation of the second operand.
17th Feb 2019, 6:14 AM
David Carroll
David Carroll - avatar
+ 4
David Carroll, similarly in Python Boolean 'And' returns the leftmost argument that evaluates as False, else it returns the rightmost argument. Python Boolean handling is one of the most surprising innovations that make Python stand out from all other languages that I have encountered.
17th Feb 2019, 3:19 PM
Brian
Brian - avatar
+ 3
Yeah, thanks I was just confused because when it gets to "False or True", I'm not asking it to check for anything, yet it still gives me an answer. It doesn't have a condition. How do you tag someone btw?
17th Feb 2019, 6:05 AM
Mista Ben
Mista Ben - avatar
+ 3
Brian Thanks for the clarification. I observed this as well in an earlier experiment. I'm on the fence as to whether or not this is an innovation or just another opinuated nuance of the "Pythonic" way adding to the list of reasons I struggle taking this language seriously enough to consider for commercial development.
17th Feb 2019, 3:43 PM
David Carroll
David Carroll - avatar
+ 2
I understand now! Thank you everyone! I like to understand how and why things work.
17th Feb 2019, 6:29 AM
Mista Ben
Mista Ben - avatar
+ 1
i vote nuance lol but this might help when looking into why lol Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression. simple huh lol
17th Feb 2019, 7:28 PM
peter
peter - avatar
0
So if it's just print (False or True) True just takes precedence?
17th Feb 2019, 4:50 AM
Mista Ben
Mista Ben - avatar
0
So the program is checking for true by default, and it returns true since it's a valid option!
17th Feb 2019, 5:44 AM
Mista Ben
Mista Ben - avatar