Back to Python
2026-02-125 min read

Precedence and Associativity of Operators in Python

Learn Precedence and Associativity of Operators in Python step by step with clear examples and exercises.

Why This Matters

Understanding the Precedence and Associativity of Operators in Python is essential for writing clean, efficient, and error-free code. It helps you avoid common pitfalls that might lead to unexpected results or runtime errors. Moreover, it's crucial during interviews and exams as questions related to this topic often appear.

Why This Matters

Knowledge of operator precedence and associativity is crucial for several reasons:

  1. Avoiding Errors: Understanding how expressions are evaluated helps you avoid common programming mistakes that can lead to unexpected results or runtime errors.
  2. Readability: Proper use of parentheses and understanding the order of operations makes your code more readable and easier for others to understand.
  3. Performance: By correctly ordering operators, you can sometimes improve the performance of your code by avoiding unnecessary calculations.
  4. Interviews and Exams: Questions related to operator precedence and associativity are common in programming interviews and exams, so having a solid understanding of this topic is beneficial.

Prerequisites

Before diving into the core concept, make sure you have a good understanding of basic Python syntax, variables, data types, and arithmetic operations. If you need a refresher, check out our Python Basics tutorial.

Core Concept

Operator Precedence

Operator precedence determines the order in which operators are evaluated when multiple operators appear in an expression. In Python, operator precedence is generally consistent with mathematical conventions, but it's important to be aware of specific rules for certain operators.

expression = 2 + 3 * 4
print(expression) # Output: 14

In the above example, multiplication has higher precedence than addition, so 3 * 4 is calculated first, and then the result is added to 2.

Operator Associativity

Operator associativity determines how multiple instances of an operator are grouped when they appear next to each other without parentheses. In Python, operators can be either left-associative or right-associative.

Most operators in Python are left-associative, meaning that they group from left to right. For example:

expression = 2 + 3 + 4
print(expression) # Output: 9

Here, the addition operator + is left-associative, so the expression is grouped as (2 + 3) + 4. If it were right-associative, the expression would be grouped as 2 + (3 + 4), which would result in a different output.

Precedence and Associativity Table

Python's operator precedence and associativity can be better understood with the help of the following table:

| Operator | Precedence | Associativity | Description |
|----------|------------|--------------|----------------------------------|
| **()** | 1 | N/A | Parentheses for grouping expressions|
| **//, ** | 2 | Left-to-right | Flooring division, exponentiation |
| * | 3 | Left-to-right | Multiplication |
| +, - | 4 | Left-to-right | Addition and subtraction |
| <, <=, >, >=| 5 | Left-to-right | Comparison operators |
| ==, != | 6 | Equal | Equality and inequality operators |
| **is**, **is not** | 7 | Equal | Identity operators |
| **not** | 8 | Right-to-left| Logical NOT |
| **and** | 9 | Left-to-right| Logical AND |
| **or** | 10 | Left-to-right| Logical OR |

Worked Example

Let's consider the following example:

expression = 2 + 3 * (4 - 2)
print(expression) # Output: 10

In this expression, multiplication and subtraction have higher precedence than addition. First, 4 - 2 is calculated, then the result is multiplied by 3, and finally, the result is added to 2. The parentheses are used for grouping the subtraction operation.

Common Mistakes

  1. Forgetting parentheses when multiple operators with different precedence appear in an expression. This can lead to incorrect results or runtime errors.
  2. Assuming that all operators are left-associative, which can lead to incorrect results if right-associative operators are involved.
  3. Misunderstanding the order of operations and making assumptions based on mathematical conventions rather than consulting Python's operator precedence table.
  4. Failing to account for edge cases where the default behavior of an operator might not be what was intended, such as using == instead of is for object comparison.
  5. Using unnecessary parentheses, which can make code more difficult to read and maintain.

Subheadings under Common Mistakes:

  • Mistake 1: Forgetting Parentheses
  • Mistake 2: Assuming All Operators are Left-Associative
  • Mistake 3: Misunderstanding the Order of Operations
  • Mistake 4: Failing to Account for Edge Cases
  • Mistake 5: Using Unnecessary Parentheses

Practice Questions

  1. What is the output of print(5 + 2 * 4)?
  2. Given expression = (2 + 3) * 4, what is the value of expression after each operation, step by step?
  3. Rewrite the expression a = b + c / d - e to ensure that multiplication and division are performed before addition and subtraction, using parentheses where necessary.
  4. What is the output of print(10 < 9 < 8)? Why does this expression not follow the standard mathematical order of operations (PEMDAS)?
  5. Explain the difference between the equality operators == and is. When should each be used?

FAQ

Q: Why does Python not follow the standard mathematical order of operations (PEMDAS)?

A: Python follows a slightly different order due to historical reasons and design choices. However, the difference is minimal, as Python's operator precedence is generally consistent with mathematical conventions.

Q: Can I change the associativity of operators in Python?

A: No, the associativity of operators in Python is fixed and cannot be changed by users.

Q: What happens if I mix left-associative and right-associative operators in an expression without parentheses?

A: If you mix left-associative and right-associative operators in an expression, the left-associative operators will group from left to right, while the right-associative operator will group from right to left. This can lead to unexpected results and is generally discouraged.

Q: Are there any operators in Python that are right-associative?

A: Yes, the bitwise NOT operator ~ is right-associative. However, it's rarely used in practice.

Q: Why does using unnecessary parentheses make code more difficult to read and maintain?

A: Unnecessary parentheses can clutter the code, making it harder for others to understand the intended logic. They also increase the complexity of the code without providing any benefit, which is generally discouraged in clean coding practices.

Precedence and Associativity of Operators in Python | Python | XQA Learn