Python Precedence and Associativity of Operators
Precedence and Associativity are characteristics that determine how the Python interpreter should evaluate expressions.
The combination of values, variables, operators, and function calls is termed as an expression. The Python interpreter can evaluate a valid expression.
Precedence of Python Operators
The precedence of operators in Python is shown in the following table in descending order (i.e., higher rows have higher priority than lower rows)
Operators | Description |
---|---|
() | Parentheses |
** | Exponent |
+x , -x , ~x | Unary plus, Unary minus, Bitwise NOT |
* , / , // , % | Multiplication, Division, Floor division, Modulus |
+ , - | Addition, Subtraction |
<< , >> | Bitwise shift operators |
& | Bitwise AND |
^ | Bitwise XOR |
&124; | Bitwise OR |
== , != , > , >= , < , <= , is , is not , in , not in | Comparisons, Identity, Membership operators |
not | Logical NOT |
and | Logical AND |
or | Logical OR |
= += -= *= /= %= &= ^= &124;= <<= >>= | Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment |
Examples
Precedence of +
and *
expr = 10 + 20 * 30
print(expr) # 610
Precedence of AND
, OR
and parenthesis ()
a = "ok"
b = 10
print(a == "ok" or a == "oook" and b>=15) # True
print((a == "ok" or a == "oook") and b>=15) # False
Associativity of Python Operators
Groups of operators in the same rows in the previous table have the same precedence.
Associativity makes it possible to determine the order of operations that have the same precedence.
Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence.
Almost all the operators have left-to-right associativity.
This table lists all operators from the highest precedence to the lowest precedence with associativity
Operator | Description | Associativity |
---|---|---|
( ) | Parentheses | left-to-right |
** | Exponent | right-to-left |
* / % | Multiplication/division/modulus | left-to-right |
+ - | Addition/subtraction | left-to-right |
<< >> | Bitwise shift left, Bitwise shift right | left-to-right |
< <= > >= | Relational less than/less than or equal to Relational greater than/greater than or equal to | left-to-right |
== != | Relational is equal to/is not equal to | left-to-right |
is , is not , in , not in | Identity Membership operators | left-to-right |
& | Bitwise AND | left-to-right |
^ | Bitwise exclusive OR | left-to-right |
&124; | Bitwise inclusive OR | left-to-right |
not | Logical NOT | right-to-left |
and | Logical AND | left-to-right |
or | Logical OR | left-to-right |
= += -= *= /= %= &= ^= &124;= <<= >>= | Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment | right-to-left |
For examples:
# Left-right associativity
print(5 * 2 // 3) # 3
print(5 * (2 // 3)) # 0
# Right-left associativity of **
print(2 ** 3 ** 2) # 512 (Since 2**(3**2) = 2**9
# If 2 needs to be exponated fisrt, need to use ()
print((2 ** 3) ** 2) # 64
Non associative operators
Some operators, like assignment operators and comparison operators, do not have associativity in Python.
For example, x < y < z
means neither (x < y) < z
nor x < (y < z)
. x < y < z
is equivalent to x < y and y < z
,
and is evaluated from left to right.
# Initialize x, y, z
x = y = z = 1
# Expression is invalid (Non-associative operators)
# x = y = z+= 2
# SyntaxError: invalid syntax