PythonOperator Precedence

Operator Precedence in Python

In Python, operator precedence defines the order in which operations are performed when an expression has multiple operators.

If you do not explicitly use parentheses () to group expressions, Python will evaluate the operators according to their precedence — from highest to lowest.


Why Operator Precedence Matters

Consider the expression:

precedence_example.py
result = 3 + 2 * 5
print(result)
output.txt
13

Without knowing precedence, one might expect:

(3 + 2) * 5 = 25

But Python evaluates * (multiplication) before + (addition):

3 + (2 * 5) = 3 + 10 = 13

Python Operator Precedence Table (Highest to Lowest)

Precedence LevelOperator TypeOperators
1 (highest)Parentheses()
2Exponentiation**
3Unary plus, minus, bitwise NOT+x, -x, ~x
4Multiplication, Division, Floor division, Modulo*, /, //, %
5Addition, Subtraction+, -
6Bitwise shifts<<, >>
7Bitwise AND&
8Bitwise XOR^
9Bitwise OR``
10Comparisons==, !=, >, <, >=, <=, is, is not, in, not in
11Logical NOTnot
12Logical ANDand
13Logical ORor
14 (lowest)Assignment=, +=, -=, *=, etc.

Example 1: Without Parentheses

precedence_without_parentheses.py
value = 5 + 2 * 3 ** 2
print(value)
output.txt
23

How this is evaluated:

  1. 3 ** 2 → 9
  2. 2 * 9 → 18
  3. 5 + 18 → 23

Example 2: With Parentheses

precedence_with_parentheses.py
value = (5 + 2) * (3 ** 2)
print(value)
output.txt
63

Explanation:

  • Parentheses change the natural precedence:

    • (5 + 2) → 7
    • (3 ** 2) → 9
    • 7 * 9 → 63

Example 3: Mixing Logical and Comparison Operators

logical_comparison.py
result = True or False and False
print(result)
output.txt
True

How this works:

  • and has higher precedence than or
  • Expression is treated as:
True or (False and False)
→ True or False
→ True

Example 4: Assignment with Arithmetic

assignment_arithmetic.py
x = 5
x += 2 * 3
print(x)
output.txt
11

Explanation:

  • 2 * 3 → 6
  • x += 6x = 5 + 6 → 11

Example 5: Complex Expression

complex_precedence.py
result = 4 + 3 * 2 ** 2 > 20 or False
print(result)
output.txt
False

Step-by-step:

  1. 2 ** 2 → 4
  2. 3 * 4 → 12
  3. 4 + 12 → 16
  4. 16 > 20 → False
  5. False or False → False

Important Notes:

  • Parentheses are your best friend when writing complex expressions — they improve both readability and correctness.
  • If you’re unsure of how Python will interpret an expression, use parentheses to make it explicit.
  • Relying solely on precedence can make code harder to read and maintain.

Summary

  • Operator precedence defines the order of evaluation when multiple operators appear in an expression.
  • Operators like **, *, /, and logical operators like and, or follow a well-defined precedence.
  • Use parentheses () when clarity matters — both for others reading your code and to avoid bugs.
  • Mastering operator precedence is essential for writing correct and predictable Python code.
Last updated on