Precedence and Associativity of Operators
Precedence of Python Operators
A valid expression is a combination of literal values, variables, operators, and functions.
Example:
>>> 2 + 3
5
Here 2 + 3 is an expression. Let take another expression
>>> 2 + 3 * 4
14
An expression can have more than one operator. The above expression can be evaluated in two ways
Method 1
Step 1 2 + 3 = 5
Step 2 5 * 4 = 20
Method 2
Step 1 3 * 4 = 12
Step 2 2 + 12 = 14
In the above expression there are two operators + and *. To solve the expression the order is decided by the precedence. It guides the order in which these operations are carried out, since * has higher precedence 3 * 4 is evaluated first and 2 is added to 12 ( 3 * 4).
Here is the table of operator precedence.
The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones).
Operators |
Meaning |
() |
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 |
| |
Bitwise OR |
==, !=, >, >=, <, <=, is, is not, in, not in |
Comparisons, Identity, Membership operators |
not |
Logical NOT |
and |
Logical AND |
or |
Logical OR |
Example
2 * 3 ** 2
Here ** has higher precedence than * , The 3 **2 is evaluated first 3 **2 = 9
and 2* 9 = 18 which is the outcome of the expression
Output
2 * 3 ** 2 = 18
Example
(2 * 3 ) ** 2
Here () has higher precedence , the (2 * 3) will be evaluated first, (2*3) = 6, now the expression is 6**2 which is 36
Output
(2 * 3) ** 2 = 36
Example
True and not False
The not has higher precedence first not False is evaluated which is True , True and True is True
Output
True and not False
Associativity of Python Operators
When two operators have the same precedence, associativity helps to determine the order of operations.
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.
Let take another example
Example
10000 / 100 * 100 // 10 % 10
When the operators have same precedence Operator Associativity is used to determine the order of evaluation. It can either be Left to Right or from Right to Left.
The associativity is the order in which Python evaluates an expression containing multiple operators of the same precedence.
Left to Right Associtivity means the expression is evaluated from Left to right The Right to Left Associtivity is where the expression is evaluated from Right to Left
Note: Almost all operators, except the exponent (**) support the left-to-right associativity.
Example
2 ** 3 ** 2
The above expression has two exponent operator these are evaluated from left to right, first 3 **2 is evaluated here expression become 2**9 which id 512
Output
2 ** 3 ** 2 = 512
Note: Exponent operator ** has right-to-left associativity in Python.