Python Operators Precedence and Associativity


Python Operators Precedence and Associativity

Precedence is the order in which operators are evaluated in an expression, and the associativity defines the directions of evaluation when we have operators with the same precedence. Associativity can be Left to Right or Right to Left. Understanding the precedence and associativity of operators is essential for correctly interpreting and evaluating expressions.

Operator Precedence

Precedence means priority. When multiple operators are used in a single expression, they are evaluated as per the priority assigned to it. Operators are given priority as per PEMDAS.

CharacterFull FormSymbolPriority
PParentheses()1
EExponential**2
MDMultiplication, Division*, /, //, %3
ASAddition, Subtraction+, –4

Here we have precedence and associativity table. The table is arranged in the order of higher precedence to lower precedence. Most of the operators have Left to Right associativity and only a few operators have Right to Left associativity(highlighted one)

Python Operators Precedence and Associativity

Note: Precedence is moving from Highest to Lowest

Example 1:

In [1]:
print(2 + 5 * 2)
12
Operator Precedence

In this example, we can see that the multiplication operator has more precedence than the division operator. So, the multiplication operator is evaluated first, i.e 5 * 2 = 10, and then 2 is added to 10, which results in 12.

Example 2:

In [1]:
print((2 + 5) * 2)
14

In this example, we can see that Parentheses have higher priority than the multiplication, so (5+2) is evaluated first and then 7 is multiplied to 2, which results in 14.

How Precedence works Logical Operators

In the below example, you can see that as and have more precedence over or, so first “qualification == “Post Graduate” and age >= 21″ is evaluated, as both are false, so the result is False and then “qualification == “Graduate” or False” is evaluated. Because the qualification == “Graduate” is True and True or False is True, so the result is also True.

In [1]:
qualification = "Graduate"
age = 20
 
if qualification == "Graduate" or qualification == "Post Graduate" and age >= 21:
    print("You are eligible")
else:
    print("Better Luck Next Time!!")
You are eligible

Now, we have used the parenthesis in the below example and because of parenthesis (qualification == “Graduate” or qualification == “Post Graduate”) is evaluated first, which is True and then (True and age >= 21) is evaluated, which results in False.

In [1]:
qualification = "Graduate"
age = 20
 
if (qualification == "Graduate" or qualification == "Post Graduate") and age >= 21:
    print("You are eligible")
else:
    print("Better Luck Next Time!!")
Better Luck Next Time!!

Operator Associativity

Associativity, in case we have multiple operators with the same precedence in an expression, they are evaluated as per the associativity. This means the direction we will follow to evaluate them. Only exponential is evaluated from right to left, and all others from left to right.

Example 1:

The below example only use multiplication and division, which have the same precedence, so evaluated from left to right.

In [1]:
print(4 / 2 * 2)
4.0
Operator Associativity

Example 2:

The below code has an exponential operator, so evaluated from right to left.

In [1]:
print(2 ** 3 ** 2)
512

Operator Associativity

Non-associative operators

Non-associative operators are the operators that do not have any associativity rules, i.e. right to left or left to right. These operators have their own rules for sequences and do not come under associativity rules.

Both the examples below comes under the non-associative operators, as they do not come under any associativity rule.

In [1]:
x = y = z = 5

In [1]:
x = y = (z>2) = 2
  File "<ipython-input-1-bef5bf339738>", line 1
    x = y = (z>2) = 2
           ^
SyntaxError: can't assign to comparison

Python Operator Short Circuiting

For evaluating some expression’s python uses Short Circuiting. Short Circuiting means it will stop the evaluation of the expression as soon as the result is determined. Short-circuiting is a very useful technique to improve the efficiency. Let us see a few cases where short-circuiting is performed:

Example 1:

This example has all “or” operators and in the case of the “or” operator, if any of the expression is True, then the result it True, so it will not evaluate all expressions.

In [1]:
print((1<2) or  (2<1) or (3<4))
True

Example 2:

This example has all “and” operators and in the case of the “and” operator, if any of the expression is False, then the result it False, so it will not evaluate all expressions.

In [1]:
print((1<2) and  (2<1) and (3<4))
False

References

  1. Operator Precedence
  2. Operator Associativity
  3. Non-associative operators