Dive into Python's operator precedence hierarchy with this comprehensive guide. Uncover the rules governing expressions for precise and efficient coding.
An expression in python consists of variables, operators, values, etc. When the Python interpreter encounters any expression containing several operations, all operators get evaluated according to an ordered hierarchy, called operator precedence.
Listed below is the table of operator precedence in python, increasing from top to bottom and decreasing from bottom to top. Operators in the same box have the same precedence.
Operator | Description |
---|---|
:= | Assignment expression |
lambda | Lambda expression |
if-else | Conditional expression |
or | Boolean OR |
and | Boolean AND |
not x | Boolean NOT |
in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, membership and identity operators |
| | Bitwise OR |
^ | Bitwise XOR |
& | Bitwise AND |
<<, >> | Left and right Shifts |
+, – | Addition and subtraction |
*, @, /, //, % | Multiplication, matrix multiplication, division, floor division, remainder |
+x, -x, ~x | Unary plus, Unary minus, bitwise NOT |
** | Exponentiation |
await x | Await expression |
x[index], x[index], x(arguments…), x.attribute | Subscription, slicing, call, attribute reference |
() Parentheses | (Highest precedence) |
Operator precedence in python follows the PEMDAS rule for arithmetic expressions. The precedence of operators is listed below in a high to low manner.
Firstly, parantheses will be evaluated, then exponentiation and so on.
In the case of tie means, if two operators whose precedence is equal appear in the expression, then the associativity rule is followed.
All the operators, except exponentiation(**) follow the left to right associativity. It means the evaluation will proceed from left to right, while evaluating the expression.
Example- (43+13−9/3∗7)(43+13−9/3∗7)
In this case, the precedence of multiplication and division is equal, but further, they will be evaluated according to the left to right associativity.
Let's try to solve this expression by breaking it out and applying the precedence and associativity rule.
All comparison operations, such as <, >, ==, >=, <=, !=, is, and in, have the same priority, which is lower than arithmetic, shifting, and bitwise operations. Unlike in C, Python follows the conventional mathematical interpretation for expressions like a < b < c.
Comparisons in Python produce boolean values, either True or False.
Python allows chaining of comparisons, such as x < y <= z, which is equivalent to x < y and y <= z. It's important to note that while y is only evaluated once in the chain, z is not evaluated at all if x < y is found to be false.
a = 100
b = 80
c = 80
d = 19
print(a > b == c >= d)
Output:
True
Explanation: Before evaluating the expression a > b == c >= d, it's important to note that comparison operators in Python have equal precedence and are evaluated from left to right. The expression is transformed based on the comparison chaining rule:
The expression a > b == c >= d is equivalent to (a > b) and (b == c) and (c >= d).
Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature.
Below are two examples to illustrate the operator precedence in python. See the explanation to get a good idea of how these things work internally.
a = (10 + 12 * 3 % 34 / 8)
b = (4 ^ 2 << 3 + 48 // 24)
print (a)
print (b)
Output:
10.25
68
Explanation:
In the first expression, we have 4 operators: +,∗,/,%+,∗,/,%.
In the second expression, we have four operators: XOR, <<,+,//<<,+,//.
a = (24 ** 2 // 4 % 25 / 19 * 8)
b = (4 << 8 >> 2)
c = (3 ** 2 ** 4)
print (a)
print (b)
print (c)
Output:
8.0
256
43046721
Explanation:
#python