Operator Precedence & Associativity in Python

One of the most important concepts in any programming language for beginners is operator precedence and associativity. This is the foundation of logical reasoning in any programming language and significantly impacts how expressions are evaluated with multiple operators and values. Let me teach you this crucial concept because if you master these, you will avoid logical errors, build efficient programmes, and even debug code for logical errors.

"Operator Precedence & Associativity
“Operator Precedence & Associativity

What is Operator Precedence?

Operator precedence determines the order of execution for different operators in an expression. For example:

Operator precedence tells us which operation executes first when there are multiple operators in an expression.

2 + 2 / 2
Python

Firstly, you might calculate

  • 2 + 2 = 4 and then 4 / 2 = 2.0

But the correct answer is 3. Why? Because of the BODMAS (Brackets, Orders, Division, Multiplication, Addition, Subtraction) rule:

  • First, division happens: 2 / 2 = 1.
  • Then, addition: 2 + 1 = 3.

Similarly, in programming, every operator has a priority, and this priority is called precedence. I personally recommend you to learn below the table.

Operator(s)DescriptionAssociativity
()ParenthesesLeft-to-Right
**Exponentiation (Power)Right-to-Left
*///%Multiplication, Division, Floor division, ModulusLeft-to-Right
+-Addition, SubtractionLeft-to-Right
<<,>>ShiftsLeft-to-Right
&Bitwise ANDLeft-to-Right
^Bitwise XORLeft-to-Right
|Bitwise ORLeft-to-Right
==!=<<=>>=Comparison OperatorsLeft-to-Right
isis notinnot inIdentity and Membership operatorsLeft-to-Right
notLogical NOTRight-to-Left
andorLogical AND and ORLeft-to-Right
=+=-=*=/=%=Logical AND and ORLeft-to-Right

In the table, precedence goes from top to bottom; hence, the operator mentioned at the top has the highest precedence (solve first). If two operators have the same precedence, use associativity. But what is associativity?

What is Operator Associativity?

Associativity defines the order in which operators of the same precedence are evaluated. For example, in the expression (3 + 5 - 2), both addition and subtraction have the same precedence, so we evaluate from left to right according to the table. Some operators, like exponentiation (**) and assignment (=), follow right to left rule.

for example:

2 ** 3 ** 2
Python

This means 3 ** 2 (9) is evaluated first, and then 2 ** 9 = 512.

Some other Examples

Example 1
2 + 6 * 3 ** 2 / 2 - (6 + 8)
Python

Solution:

  1. Based on precedence, parentheses are resolved first. (6 + 8) = 14, Hence the expression is now 2 + 6 * 3 ** 2 / 2 - 14
  2. The next highest precedence is for ** (right-to-left associativity). 3 ** 2 = 9, Hence the expression becomes 2 + 6 * 9 / 2 - 14
  3. Both * and / have the same precedence and are evaluated left-to-right.3.1. First, perform multiplication: 6 * 9 = 54, Hence the expression become 2 + 54 / 2 - 14.
    3.2. Next, perform division: 54 / 2 = 27.0, Hence the expression become 2 + 27.0 - 14
  4. + and - have the same precedence and are evaluated left-to-right.
    4.1. First, perform addition: 2 + 27.0 = 29.0, Hence the expression is now 29.0 - 14.
    4.2. Next, perform subtraction: 29.0 - 14 = 15.0. Hence 15.0 is the answer.
Example 2
(3 + 2 ** 3 > 20 or 5 * 2 < 15) and not (10 / 2 == 5)
Python
  1. Bracket: As we have brackets on both sides between and not, we use associativity.
  2. Exponentiation of first bracket: 2 ** 3 = 8
  3. Arithmetic3 + 8 = 115 * 2 = 1010 / 2 = 5.0
  4. Comparison11 > 20 = False10 < 15 = True5.5 == 5 = True
  5. Logical NOT: not True = False
  6. Logical OR: False or True = True
  7. Logical AND: True and False = False

Conclusion

Understanding these rules helps build a solid foundation for tackling more complex problems. I recommend practicing with platforms that provide precedence-related problems to strengthen your logical thinking.

Thank you for reading, and I hope this post helps clarify these important concepts! If you have any questions or need further clarification, feel free to ask in the comments below.

H.W.

Before ending, I want you to solve some of the questions to test your understanding of operator.

# 1
5 + 3 * 2 - (8 / 4)

# 2
4 * 3 ** 2 / 2 + (6 - 3) > 15 and 10 % 3 == 1

# 3
(10 + 2 ** 3 < 50) or (20 / 5 * 3 == 12) and not (15 % 4 > 1)

# 4
not (3 * 4 + 2 ** 2 < 20) and (7 % 3 == 1 or 5 * 2 != 10)

# 5
((3 + 5) ** 2 / 4 < 50) or not (6 * 2 > 10 and 15 // 4 == 3)
Python

If you want to practice more problems, you can visit this website

Operator Precedence & Associativity in Python

Evaluated left-to-right

1 / 4

In the expression (3 + 5 - 2), how are the operations performed?

Multiplication before addition

2 / 4

Which operator has higher precedence: multiplication (*) or addition (+)?

Order of operator evaluation

3 / 4

What does operator precedence determine in Python?

Right-associative exponentiation

4 / 4

How is the expression 2 ** 3 ** 2 evaluated?

Your score is

The average score is 0%

0%

Leave a Reply

Your email address will not be published. Required fields are marked *