This question already has an answer here:
Python's in (__contains__) operator returns a bool whose value is neither True nor False
(1 answer)
Closed 7 years ago.
How does Python parse the first of the following three expressions? (I expected it to be the same as the second, since == and in have the same precedence.)
>>> 1 == 0 in (0,1), (1==0) in (0,1), 1 == (0 in (0,1))
(False, True, True)
See the documentation of comparison operators: they are chained rather than grouped. So 1 == 0 in (0,1) is equivalent to (1==0) and (0 in (0,1)), which is obviously false.
Related
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
I started learning python from a site, and it gave me a question that was kind of weird, I had to add and multiply a number to True and False. I've never done that before, so could someone explain what that means? (Can you replace True with 1 and False with 0?)
This is the question:
What is the value of result at the end of the following code?
x = 20
y = 5
result = (x + True) / (4 - y * False)
A)
False
B)
-21
C)
5
D)
5.25
The answer is 5.25 because in Python, True is equal to 1 while False is equal to 0.
This question already has answers here:
Python boolean expression and or
(4 answers)
Closed 2 years ago.
I came across the below expression in python to evaluate if a number is odd or even. While it looks elegant I was surprised to see it work. I guess for the input 3:
x%2 equals 1 which evaluates to True and non-empty strings (odd) evaulate to True. So we have True and True or <don't have to check because it doesn't matter if True or False as the overall statement will be True>. But why is the output in that case not True but odd - what concept do I miss?
>>> (lambda x:
... (x % 2 and 'odd' or 'even'))(3)
'odd'
The and and or operators are not limited to True and False; they return one of their operands.
>>> 1 and "odd"
'odd'
>>> 0 or "even"
'even'
This question already has answers here:
What does it mean that Python comparison operators chain/group left to right?
(2 answers)
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
Where in the python docs does it allow the `in` operator to be chained?
(1 answer)
Closed 5 years ago.
I just stumbled upon the following line in Python 3.
1 in range(2) == True
I was expecting this to be True since 1 in range(2) is True and True == True is True.
But this outputs False. So it does not mean the same as (1 in range(2)) == True. Furthermore it does not mean the same as 1 in (range(2) == True) which raises an error.
Despite years of experience in Python, I am taken off guard. What is going on?
This is due to the fact that both operators are comparison operators, so it is being interpreted as operator chaining:
https://docs.python.org/3.6/reference/expressions.html#comparisons
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z, except that y is evaluated only once (but in both
cases z is not evaluated at all when x < y is found to be false).
So it is equivalent to:
>>> (1 in range(2)) and (range(2) == True)
False
This question already has answers here:
Why does (1 in [1,0] == True) evaluate to False?
(1 answer)
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
Closed 6 years ago.
Why does the statement 1 in (1, 2, 3) == True return False in Python?
Is the operator priority in Python ambiguous?
Because, per the documentation on operator precedence:
Note that comparisons, membership tests, and identity tests, all have
the same precedence and have a left-to-right chaining feature as
described in the Comparisons section.
The Comparisons section shows an example of the chaining:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z
So:
1 in (1, 2, 3) == True
is interpreted as:
(1 in (1, 2, 3)) and ((1, 2, 3) == True)
If you override this chaining by adding parentheses, you get the expected behaviour:
>>> (1 in (1, 2, 3)) == True
True
Note that, rather than comparing truthiness by equality to True or False, you should just use e.g. if thing: and if not thing:.
This question already has answers here:
What does the caret (^) operator do?
(5 answers)
Closed 9 years ago.
I'm struggling to find documentation on what the ^ does in python.
EX.
6^1 =
7
6^2 =
4
6^3 =
5
6^4 =
2
6^5 =
3
6^6 =
0
Help?
It is the bitwise exclusive-or operator, often called "xor". For each pair of corresponding bits in the operands, the corresponding bit in the result is 0 if the operand bits are the same, 1 if they are different.
Consider 6^4:
6 = 0b0110
4 = 0b0100
6^4 = 0b0010 = 2
As you can see the least-significant bit (the one on the right, in the "one's" place) is zero in both numbers. Thus the least-significant bit in the answer is zero. The next bit is 1 in the first operand and 0 in the second, so the result is 1.
XOR has some interesting properties:
a^b == b^a # xor is commutative
a^(b^c) == (a^b)^c # xor is associative
(a^b)^b == a # xor is reversible
0^a == a # 0 is the identity value
a^a == 0 # xor yourself and you go away.
You can change the oddness of a value with xor:
prev_even = odd ^ 1 (2 = 3 ^ 1)
next_odd = even ^ 1 (3 = 2 ^ 1)
for more information on XOR , please react the documentation on Python.org at here:
http://docs.python.org/2/library/operator.html