The Python documentation for operator precedence states:
Operators in the same box group left to right (except for
comparisons, including tests, which all have the same precedence and
chain from left to right — see section Comparisons...)
What does this mean? Specifically:
"Operators in the same box group left to right (except for
comparisons...)" -- do comparisons not group left to right?
If comparisons do not group left to right, what do they do instead? Do they "chain" as opposed to "group"?
If comparisons "chain" rather than "group", what is the difference between "chaining" and "grouping"?
What would be some examples to demonstrate that the comparison operators chain from left to right rather than from right to left?
Grouping (this is what non-comparison operators do):
a + b + c means (a + b) + c
Chaining (this is what comparison operators do):
a < b < c means (a < b) and (b < c)
Grouping left to right (this is the way things are grouped):
5 - 2 - 1 means (5 - 2) - 1 == 2
as opposed to grouping right to left (this would produce a different result):
5 - (2 - 1) == 4
Chaining left to right
Chaining is left to right, so in a < b < c, the expression a < b is evaluated before b < c, and if a < b is falsey, b < c is not evaluated.
(2 < 1 < f()) gives the value False without calling the function f, because 2 < 1 evaluates to false, so the second comparison does not need to be performed.
f() > 1 > g() calls f() in order to evaluate the first comparison, and depending on the result, it might or might not need to evaluate the second condition, which requires calling g().
NB. Each operand is evaluated at most once. So in the expression 1 < f() < 2, the function f() is only called once, and the value it gives is used in both comparisons (if necessary).
https://en.wikipedia.org/wiki/Short-circuit_evaluation
In fact, the chain behavior is not so obvious.
a == b == c
although one would expect this to be converted to
a == b and b == c
it is in fact converted into somthing similar to
b == c if a == b else False
which is a bit confusing if one tries to override the behavior of the comparison operators and chain them.
When I execute in python following code
print(0<5<2)
It gives False as output
but same thing in C++
std::cout<<(0<5<2);
returns True
Why this contradiction?
In Python, 0 < 5 < 2 is equivalent to 0 < 5 and 5 < 2.
In C++, it is equivalent to static_cast<int>(0 < 5) < 2.
The Python shorthand is originally inspired by mathematics, but has been so generalized that you can write really strange stuff, like
>>> 1 < 5 in [2,3,4]
False
>>> 1 < 5 in [2,3,5]
True
and confuse all your friends.
Because they are different language that has different syntax and work differently.
In the case of c++, the statement is evaluated from left to right.
0<5 == true
true < 2, this will trigger an implicit conversion from true to 1
1 < 2 == true, which is the end result
Python has different rules for how the language works.
I don't know them, but clearly they lead to different result in this case.
I have just started learning Python. I have come across the below problem. As shown below, I am writing a same statement once using '&' operator and then using 'and' operator. Though both of these operators evaluate the 'if' condition to the same Boolean output,'True' (True 'and' True is True and also True & True is True), why are the results different? Why is '&' giving wrong answer? Appreciate any clarifications.
a = 70
b = 40
c = 60
x = a if a>b and a>c else b if b>c else c
print('Max value with "and":', x)
x = a if a>b & a>c else b if b>c else c
print('Max value with "ampersand":', x)
The output is:
Max value with "and": 70
Max value with "ampersand": 60
why are the results different? Why is '&' giving wrong answer?
Because they're completely different operators with completely different semantics and completely different precedence: & applies before > which applies before and.
So when you write a > b and a > c it's parsed as (a > b) and (a > c).
But when you write a > b & a > c, it's parsed as a > (b & a) > c.
There's an other difference which is not relevant here but would be a factor in other context: and (and or) are lazy operators, meaning they will evaluate their left operands, then do their thing, and only then evaluate the second operand. & however is a normal "eager" operator, it evaluates both operands and only then applies whatever it does.
Finally, and this is of course the biggest difference: and works on everything and is non-overridable; & only works on types which specifically support it, and does whatever it was defined as (for integers it's a bitwise AND, but for sets it's intersection).
All comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Thus "==" and "<" have the same priority, why would the first expression in the following evaluate to True, different from the 2nd expression?
>>> -1 < 0 == False
True
>>> (-1 < 0) == False
False
I would expect both be evaluated to False. Why is it not the case?
Python has a really nice feature - chained comparison, like in math expressions, so
-1 < 0 == False
is actually a syntactic sugar for
-1 < 0 and 0 == False
under the hood.
I am confused as to when I should use Boolean vs bitwise operators
and vs &
or vs |
Could someone enlighten me as to when do i use each and when will using one over the other affect my results?
Here are a couple of guidelines:
Boolean operators are usually used on boolean values but bitwise operators are usually used on integer values.
Boolean operators are short-circuiting but bitwise operators are not short-circuiting.
The short-circuiting behaviour is useful in expressions like this:
if x is not None and x.foo == 42:
# ...
This would not work correctly with the bitwise & operator because both sides would always be evaluated, giving AttributeError: 'NoneType' object has no attribute 'foo'. When you use the boolean andoperator the second expression is not evaluated when the first is False. Similarly or does not evaluate the second argument if the first is True.
Here's a further difference, which had me puzzled for a while just now: because & (and other bitwise operators) have a higher precedence than and (and other boolean operators) the following expressions evaluate to different values:
0 < 1 & 0 < 2
versus
0 < 1 and 0 < 2
To wit, the first yields False as it is equivalent to 0 < (1 & 0) < 2, hence 0 < 0 < 2, hence 0 < 0 and 0 < 2.
In theory, and and or come straight from boolean logic (and therefore operate on two booleans to produce a boolean), while & and | apply the boolean and/or to the individual bits of integers. There are a lot lot of questions here on how the latter work exactly.
Here are practical differences that potentially affect your results:
and and or short-circuiting, e.g. True or sys.exit(1) will not exit, because for a certain value of the first operand (True or ..., False and ...), the second one wouldn't change the result so does not need to be evaluated. But | and & don't short-circuit - True | sys.exit(1) throws you outta the REPL.
& and | are regular operators and can be overloaded, while and and or are forged into the language (although the special method for coercion to boolean may have side effects).
This also applies to some other languages with operator overloading
and and or return the value of an operand instead of True or False. This doesn't change the meaning of boolean expressions in conditions - 1 or True is 1, but 1 is true, too. But it was once used to emulate a conditional operator (cond ? true_val : false_val in C syntax, true_val if cond else false_val in Python). For & and |, the result type depends on how the operands overload the respective special methods (True & False is False, 99 & 7 is 3, for sets it's unions/intersection...).
This also applies to some other languages like Ruby, Perl and Javascript
But even when e.g. a_boolean & another_boolean would work identically, the right solution is using and - simply because and and or are associated with boolean expression and condition while & and | stand for bit twiddling.
If you are trying to do element-wise boolean operations in numpy, the answer is somewhat different. You can use & and | for element-wise boolean operations, but and and or will return value error.
To be on the safe side, you can use the numpy logic functions.
np.array([True, False, True]) | np.array([True, False, False])
# array([ True, False, True], dtype=bool)
np.array([True, False, True]) or np.array([True, False, False])
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
np.logical_or(np.array([True, False, True]), np.array([True, False, False]))
# array([ True, False, True], dtype=bool)
The hint is in the name:
Boolean operators are for performing logical operations (truth testing common in programming and formal logic)
Bitwise operators are for "bit-twiddling" (low level manipulation of bits in byte and numeric data types)
While it is possible and indeed sometimes desirable (typically for efficiency reasons) to perform logical operations with bitwise operators, you should generally avoid them for such purposes to prevent subtle bugs and unwanted side effects.
If you need to manipulate bits, then the bitwise operators are purpose built. The fun book: Hackers Delight contains some cool and genuinely useful examples of what can be achieved with bit-twiddling.
The general rule is to use the appropriate operator for the existing operands. Use boolean (logical) operators with boolean operands, and bitwise operators with (wider) integral operands (note: False is equivalent to 0, and True to 1). The only "tricky" scenario is applying boolean operators to non boolean operands. Let's take a simple example, as described in [SO]: Python - Differences between 'and' and '&': 5 & 7 vs. 5 and 7.
For the bitwise and (&), things are pretty straightforward:
5 = 0b101
7 = 0b111
-----------------
5 & 7 = 0b101 = 5
For the logical and, here's what [Python.Docs]: Boolean operations states (emphasis is mine):
(Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument.
Example:
>>> 5 and 7
7
>>> 7 and 5
5
Of course, the same applies for | vs. or.
Boolean operation are logical operations.
Bitwise operations are operations on binary bits.
Bitwise operations:
>>> k = 1
>>> z = 3
>>> k & z
1
>>> k | z
3
The operations:
AND &: 1 if both bits are 1, otherwise 0
OR |: 1 if either bit is 1, otherwise 0
XOR ^: 1 if the bits are different, 0 if they're the same
NOT ~': Flip each bit
Some of the uses of bitwise operations:
Setting and Clearing Bits
Boolean operations:
>>> k = True
>>> z = False
>>> k & z # and
False
>>> k | z # or
True
>>>
Boolean 'and' vs. Bitwise '&':
Pseudo-code/Python helped me understand the difference between these:
def boolAnd(A, B):
# boolean 'and' returns either A or B
if A == False:
return A
else:
return B
def bitwiseAnd(A , B):
# binary representation (e.g. 9 is '1001', 1 is '0001', etc.)
binA = binary(A)
binB = binary(B)
# perform boolean 'and' on each pair of binaries in (A, B)
# then return the result:
# equivalent to: return ''.join([x*y for (x,y) in zip(binA, binB)])
# assuming binA and binB are the same length
result = []
for i in range(len(binA)):
compar = boolAnd(binA[i], binB[i])
result.append(compar)
# we want to return a string of 1s and 0s, not a list
return ''.join(result)
Logical Operations
are usually used for conditional statements. For example:
if a==2 and b>10:
# Do something ...
It means if both conditions (a==2 and b>10) are true at the same time then the conditional statement body can be executed.
Bitwise Operations
are used for data manipulation and extraction. For example, if you want to extract the four LSB (Least Significant Bits) of an integer, you can do this:
p & 0xF