Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python Operator Precedence and Evaluation Order

Learn how Python groups operators and evaluates expressions, so you can read complex expressions confidently and add parentheses when they make the meaning clearer.

Levels: Beginner, Intermediate ~16 min read

Basic

Python can combine several operators in one expression. To read such an expression correctly, you need to know two different rules: operator precedence decides how the expression is grouped, while evaluation order decides when its parts are evaluated.

Precedence decides the grouping

python precedence.py
1
2
3
4
5
total = 2 + 3 * 4
with_parentheses = (2 + 3) * 4

print(total)
print(with_parentheses)

Multiplication has higher precedence than addition, so the first expression is grouped like `2 + (3 * 4)`. Parentheses explicitly change the grouping.

Precedence is about which operators bind more tightly. A higher-precedence operator is grouped before a lower-precedence operator unless parentheses change the grouping. Python's reference lists the complete precedence table from the most binding forms to the least binding ones.

Parentheses make intent explicit

python grouping.py
1
2
3
4
5
6
7
8
items = 4
unit_price = 12
discount = 10

# Read as: subtract the discount from the price, then multiply.
total = items * (unit_price - discount)

print(total)

Parentheses are useful not only for changing the result, but also for showing the reader exactly how you intend the expression to be grouped.

When a calculation matters to the reader, explicit parentheses can be better than relying on memory of the precedence table. They turn an implicit grouping rule into visible intent.

Some operators group right-to-left

python associativity.py
1
2
3
4
5
6
7
power = 2 ** 3 ** 2
first = 2 ** (3 ** 2)
second = (2 ** 3) ** 2

print(power)
print(first)
print(second)

Exponentiation groups from right to left, so `2 ** 3 ** 2` means `2 ** (3 ** 2)`, not `(2 ** 3) ** 2`.

Most binary operators at the same precedence group from left to right. Exponentiation is an important exception, and conditional expressions also have their own right-to-left grouping rule. The conditional-expression lesson owns that syntax; here the important idea is that grouping direction is part of the language definition.

Unary minus and exponentiation

python unary-power.py
1
2
3
4
5
a = -2 ** 2
b = (-2) ** 2

print(a)
print(b)

The power operator binds more tightly than unary minus on its left. Therefore `-2 ** 2` means `-(2 ** 2)`.

This is a classic place where parentheses help. If the negative number itself is meant to be the base, write `(-2) ** 2` so the intended grouping is unmistakable.

Same precedence is not the same as same operation

python left-to-right.py
1
2
3
4
5
result = 20 / 5 * 2
other = 20 / (5 * 2)

print(result)
print(other)

Division and multiplication have the same precedence and group from left to right, so the first expression is `(20 / 5) * 2`.

A useful habit is to identify the precedence level first, then apply the grouping direction for operators at that level. Parentheses can always make a different grouping explicit.

Precedence is different from evaluation order

python evaluation-order.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
events = []

def mark(name, value):
    events.append(name)
    return value

result = mark("left", 10) + mark("middle", 20) * mark("right", 30)

print(result)
print(events)

The multiplication is grouped before the addition, but the operand expressions are still evaluated from left to right. The recorded order is `left`, `middle`, then `right`.

This distinction matters whenever evaluating an expression has an observable effect, such as calling a function. Precedence answers “how is the expression grouped?” Evaluation order answers “which subexpression runs first?” Python specifies left-to-right evaluation of expressions.

Function-call arguments are evaluated before the call

python call-evaluation.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
events = []

def make(label, value):
    events.append(label)
    return value

def add(left, right):
    return left + right

result = add(make("first", 2), make("second", 3))

print(result)
print(events)

Both argument expressions are evaluated before `add()` is called, and the arguments themselves are evaluated from left to right.

Do not use precedence to predict side-effect order. A subexpression can belong to a lower-precedence part of the final grouping and still be evaluated before another subexpression that is grouped more tightly.

A practical reading method

python read-an-expression.py
1
2
3
4
5
score = 8 + 4 * 3
bonus = 10 if score >= 20 else 0
total = score + bonus

print(total)

Read each expression in layers: grouping first, then evaluation order, then the resulting values.

When an expression looks dense, break it mentally into smaller parts. Decide which operators bind first, note any left-to-right or right-to-left grouping rule, then follow Python's evaluation order. If you need to explain the result to someone else, add parentheses or split the expression into named intermediate values.

Was this page helpful?

Mark this learning resource complete to track your learning progress.