This question already has answers here:
How do you get the logical xor of two variables in Python?
(28 answers)
What does the caret (^) operator do?
(5 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm practicing code on codingbat and came across this logic question:
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
The solution is:
if n == 1 or n == 10:
return True
return (n in range(1,11)) ^ outside_mode
If one googles "python ^ symbol" or "what does the ^ symbol do python" Google returns answers regarding the "#" symbol and the modulo symbol. It is not easy to find what this symbol does. This question should therefor be reopened to provide better chances of an early-stage programmer finding these answers
It's bit-wise XOR. In Python, True == 1, and False == 0, so you can use bitwise operators to get the equivalent boolean operation.
For a single bit, ^ is the same as !=, so this is essentially the same as
return (n in range(1, 11) != outside_mode
^ is the bitwise exclusive or (or XOR)
>>> True ^ True
False
>>> True ^ False
True
>>> False ^ True
True
>>> False ^ False
False
Related
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:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 2 years ago.
Suppose I have the following line of code:
print("valley" in "hillside" == False)
Since the precedence of in and == is equivalent in Python, I expected the operations to be performed from left to right, producing True as the output.
However, in actuality, when I run this line of code, I get False.
I have noticed that adding brackets around "valley" in "hillside" results in True as the output but I don't seem to understand why it's necessary in the first place...
Both in and == are comparison operators, so the parser treats
print("valley" in "hillside" == False)
the same as
print("valley" in "hillside" and "hillside" == False)
See the section on Comparisons in the Python language reference for more details, in particular this note:
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).
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 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.
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