whats going on here ? Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as "true." >>> x = 5 >>> x and 1 1 >>> y = 0 >>> y and 1 0
what is the code doing im confused and whats he talking about
4.3 Logical operators There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10. n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that is, if the number is divisible by 2 or 3. Finally, the not operator negates a boolean expression, so not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.
his text ^^
I will assume you understand the logical operators on purely boolean values. What is happening on your first post is this: Python has 'true' and 'false' values for basically everything that is a reference. A non-empty string is 'true', and an empty one is false; non-zero numerical values are 'true', zero is false. Taking this into consideration, we have that: "x =5", and then "x and 1", that is, "True and True" = 1 (True). When Python returns 0, basically it means that something is false, and 1 is True (because some ancient convention). With that being said, a word of warning: personally, I never use boolean operators on non-boolean values; like "x and y", when x and y are integers. You can do that in Python, but it is forbidden in many languages (also makes the code harder to understand / a bit confusing). It's just something to keep in mind, because there might be a time where the better solution is to do "x and y".
It's a little more complicated than just 0 is False and 1 is True. The first thing you need to understand is short-circuiting. First, we'll consider boolean values. If you have an expression "a and b", and a is False, then python already knows the answer will be False, so it doesn't even look at b. If a was True then it would have to look at b to determine if the expression was true. For "a or b" it's kinda the same, but backwards. If a is True then python can stop at a. If a is False then it needs to continue and look at b. That's why you can have a guardian check in a function that looks like if s == None or s[0] == 'a' If s is None then s[0] will cause a runtime error. But the first check will evaluate to True, so python will stop there and evaluate the whole expression to False, avoiding the s[0] expression entirely. Now how does that work for non-boolean variables? Well, the same way, except it won't return True or False for the expression value. What will "5 and 2" evaluate to? 5 is equivalent to True, so it has to check the second operand to determine the result of the expression. That value is 2, which is also equivalent to True, so the value of the expression will be some value equivalent to 2. What python will return is the second operand, 2, the value that actually made the expression True. If the expression was "5 or 2" python would stop when it reached the 5, because of short-circuiting, because it already knows by this point that the expression will evaluate to True. So it will return 5, the value that caused the expression to be True. For "0 and 6" it will stop at 0 because that makes the expression False, and it will return 0. As bmp said, other non-boolean values have boolean equivalents. Specifically, empty sequences are False. So "[] or 8" will evaluate to 8, while "[] and 8" will evaluate to []. Here are some examples that illustrate this property. Make sure you understand why each expression evaluates to the value printed below. http://codepad.org/jnP74Phe And, as bmp said, please don't use this functionality. I know it's pythonic, but it's confusing. I expect the result of "and" and "or" expressions to be booleans. When they're not, there will undoubtedly be trouble eventually.
i feel so smart now
When you set a variable with a value like MyVariable = 7 and you evaluate it like this If( MyVariable ) ....... the output will be TRUE. If you set a variable with a NULL value for example, the condition will change to FALSE because you are asking if a variable is SET or not. But if you set x = 5 and you want to evaluate if x equals 1 then you need to use x == 1 and the output will be FALSE because x = 5 so 5==1 is FALSE. x=5 ( x and 1) means is Set 5 and 1?, well yeah, those are valid values. but x=5 ( x == 1 ) means 5 equals 1 ? no 5 not equals 1.
if s == None or s[0] == 'a' If s is None then s[0] will cause a runtime error. But the first check will evaluate to True, so python will stop there and evaluate the whole expression to False, avoiding the s[0] expression entirely. whats going on here
why is it evaluating the whole expression to False if it is True
What's going on is I'm drunk. It's an "or" expression, so once the first operand is determined to be True, the whole expression will be True, so python will stop before evaluating the second operand. "Short-circuit." Maybe I had another example typed out and tried to change it but didn't quite get everything. Sorry for any confusion.
Join our real-time social learning platform and learn together with your friends!