Last modified: Oct 28, 2026

Python Operator Precedence: Rules and Examples

Introduction

Python operator precedence determines how expressions are evaluated. It defines which operations are performed first in a complex expression. Understanding this concept is crucial for writing correct and efficient code. This guide explains the order of operations, provides examples, and highlights common mistakes.

Understanding Operator Precedence

Operator precedence is like a set of rules that Python uses to decide the order of operations in an expression. For example, in the expression 2 + 3 * 4, multiplication is evaluated before addition. The result is 14, not 20.

Python follows a specific hierarchy for operators. Parentheses can override this hierarchy. Always use parentheses when in doubt to make your intentions clear.

Order of Operations

Here is the order of operator precedence in Python, from highest to lowest:

  • Parentheses()
  • Exponentiation**
  • Unary plus/minus+x, -x
  • Multiplication/Division/Modulus*, /, %
  • Addition/Subtraction+, -
  • Bitwise shifts<<, >>
  • Bitwise AND&
  • Bitwise XOR^
  • Bitwise OR|
  • Comparisons==, !=, <, >
  • Logical NOTnot
  • Logical ANDand
  • Logical ORor

Refer to Python Bitwise Operators Explained for details on bitwise operations.

Examples of Operator Precedence

Let’s look at some examples to clarify how precedence works:


# Example 1: Multiplication before addition
result = 2 + 3 * 4
print(result)
    

14
    

# Example 2: Parentheses change order
result = (2 + 3) * 4
print(result)
    

20
    

# Example 3: Complex expression
result = 2 ** 3 / 4 + 5 * 6 - 7
# 8 / 4 = 2, 5 * 6 = 30; 2 + 30 = 32 -7 = 25
print(result)
    

25.0
    

# Example 4: Bitwise operators
result = 5 & 3 | 2
# 5 is 101, 3 is 011; AND is 001 (1), OR with 2 (010) is 011 (3)
print(result)
    

3
    

Common Mistakes and How to Avoid Them

Beginners often forget to use parentheses, leading to unexpected results. For instance:


# Incorrect: Logical OR overrides AND
if 1 < 2 and 3 > 4 or 5 < 6:
    print("Condition met")
    

Condition met
    

# Correct: Parentheses clarify logic
if (1 < 2 and 3 > 4) or 5 < 6:
    print("Condition met")
    

Condition met
    

Using parentheses makes your code more readable. For more on conditionals, read Python If Else Statements: A Beginner Guide.

Best Practices

1. Use parentheses to enforce order and improve clarity.

2. Review the precedence table when writing complex expressions.

3. Test expressions with print statements to verify results.

In loops, operator precedence can affect conditions. Learn more in Python For Loop and While Loop Explained.

Conclusion

Python operator precedence is a foundational concept that impacts how your code behaves. By understanding the hierarchy, using parentheses, and testing expressions, you can avoid common pitfalls. Practice with examples and refer to the precedence table when needed. Mastering this will make your code more reliable and easier to debug.