In this tutorial, you’ll learn about Python operator precedence and associativity. This topic is crucial for programmers to understand the semantics of Python operators.
After reading it, you should be able to know how Python evaluates the order of its operators. Some operators have higher precedence than others such as the multiplication operator has higher priority than the addition operator, so do multiplication before addition.
In an expression, Python interpreter evaluates operators with higher precedence first. And, except the exponent operator (**) all other operators get evaluated from left to right.
Python Operator Precedence and Associativity
- 1 Python operator precedence
- 2 Python operator associativity
- 3 Nonassociative operators
How does the operator precedence work in Python?
When we group a set of values, variables, operators or function calls that turn out as an expression. And once you execute that expression, Python interpreter evaluates it as a valid expression.
See a simple example given below.
>>> 3 + 4 7
Here, the ‘3 +4’ is a Python expression. It contains one operator and two operands. However, a more complex statement can include multiple operators.
To evaluate complex expressions, Python lays out the rule of precedence. It governs the order in which the operations take place.
Give examples of operator precedence in Python
See the below example which combines multiple operators to form a compound expression.
# Multiplication get evaluated before # the addition operation # Result: 17 5 + 4 * 3
However, it is possible to alter the evaluation order with the help of parentheses (). It can override the precedence of the arithmetic operators.
# Parentheses () overriding the precedence of the arithmetic operators # Output: 27 (5 + 4) * 3
Operator precedence table in Python
Refer to the below table which lists the operators with the highest precedence at the top and lowest at the bottom.
<>, !=, ==
In the above table, you can confirm that some of the groups have many operators. It means that all operators in a group are at the same precedence level.
And whenever two or more operators have the same precedence, then associativity defines the order of operations.
What does the associativity mean in Python?
The associativity is the order in which Python evaluates an expression containing multiple operators of the same precedence. Almost all operators except the exponent (**) support the left-to-right associativity.
Give examples of associativity in Python
For example, the product (*) and the modulus (%) have the same precedence. So, if both appear in an expression, then the left one will get evaluated first.
# Testing Left-right associativity # Result: 1 print(4 * 7 % 3) # Testing left-right associativity # Result: 0 print(2 * (10 % 5))
As said earlier, the only operator which has right-to-left associativity in Python is the exponent (**) operator.
See the examples below.
# Checking right-left associativity of ** exponent operator # Output: 256 print(4 ** 2 ** 2) # Checking the right-left associativity # of ** # Output: 256 print((4 ** 2) ** 2)
You might have observed that the ‘print(4 ** 2 ** 2)’ is similar to ‘(4 ** 2 ** 2).
What are nonassociative operators in Python?
Python does have some operators such as assignment operators and comparison operators which don’t support associativity. Instead, there are special rules for the ordering of this type of operator which can’t be managed via associativity.
Give examples of nonassociative operators
For example, the expression 5 < 7 < 9 does not mean (5 < 7) < 9 or 5 < (7 < 9). Also, the statement 5 < 7 < 9 is same as 5 < 7 and 7 < 9, and gets evaluated from left-to-right.
Moreover, chaining of assignments operators like a = b = c is perfectly alright whereas the ‘a = b += c’ will result in an error.
# Set the values of a, b, c x = 11, y = 12, z = 13 # Expression is incorrect # Non-associative operators # Error -> SyntaxError: invalid syntax x = y += 12
Now, you might like to check out how Python deals with operator precedence and associativity.
Quick wrap up – Python operator precedence
This tutorial did cover a very important topic – Python operator precedence and associativity. So, it should now be easier for you to create compound/complex expressions in Python.