0
I don't understand how this is true
(False == True) or True
16 odpowiedzi
+ 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.
+ 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
+ 8
+ 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).
+ 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
+ 5
Ben
Yes, its logic.
+ 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.
+ 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.
+ 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.
+ 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?
+ 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.
+ 2
I understand now! Thank you everyone! I like to understand how and why things work.
+ 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
0
So if it's just
print (False or True)
True just takes precedence?
0
So the program is checking for true by default, and it returns true since it's a valid option!