Expressions
Every calculation you've written since lesson one is an expression — now name the concept explicitly and learn the precedence rules behind it.
By the end of this lesson, you can
- Predict how a compound expression evaluates using operator precedence
- Explain why expressions can nest inside other expressions, innermost first
- Distinguish an expression, which produces a value, from a statement, which doesn't
Why it matters
Every calculation in this course so far — score + 5, quantity * price, age < 12 — has been an expression. You’ve been relying on
rules for how they combine without those rules ever being named. Once
expressions get more than one operator, those rules start to matter:
10 - 2 * 3 isn’t 24, and knowing why prevents a specific, common
class of wrong-answer bug.
Mental model
An expression is anything Python can reduce to a single value, and
expressions can nest inside larger expressions — the same way
(2 + 3) can sit inside (2 + 3) * 4. When an expression has more than
one operator, Python doesn’t just work left to right: it follows
operator precedence, the same order-of-operations rules as
arithmetic — multiplication and division happen before addition and
subtraction, regardless of the order they’re written in.
print(2 + 3 * 4)
print((2 + 3) * 4)14
202 + 3 * 4 computes 3 * 4 first (precedence), then adds 2 — 14,
not 20. Parentheses override precedence entirely, forcing 2 + 3 to
be computed first.
Trace it
Tracing an expression means finding the innermost, highest-precedence piece first, the same way you’d simplify a math expression by hand:
| Step | Expression so far | Rule applied |
|---|---|---|
| start | 10 - 2 * 3 + 1 | — |
| 1 | 10 - 6 + 1 | 2 * 3 computed first (precedence) |
| 2 | 4 + 1 | 10 - 6, left to right |
| 3 | 5 | 4 + 1, left to right |
Multiplication is computed before either subtraction, even though it’s written in the middle — precedence, not reading order, decides what happens first. Once every multiplication and division is resolved, what remains is worked left to right.
Expressions vs. statements
Not every line of Python is an expression. x = 5 is a statement —
it does something (creates a binding) but doesn’t itself evaluate to a
usable value. 5 + 3 alone is an expression. The distinction matters
because expressions can be nested inside other code — as a function
argument, inside a condition, on the right of an assignment — while
statements like x = 5 cannot.
print(5 + 3) # 5 + 3 is an expression, used as print's argument
total = (x = 5) + 3 # SyntaxError — x = 5 is a statement, not a value
Check your understanding
Or reveal the answer without checking
Answer:13
4 * 2 is computed first (8), giving 20 - 8 + 1. Then left to right: 20 - 8 = 12, then 12 + 1 = 13.
Or reveal the answer without checking
Answer:price * quantity
price * quantity evaluates to a single value and can be nested inside other code. The others perform an action (assigning, defining, importing) without themselves producing a usable value.
Practice: warm-up
Evaluate each of these by hand, using precedence, before revealing the
answers: 3 + 4 * 2, (3 + 4) * 2, 20 / 4 - 1, 20 / (4 - 1).
Stuck? Reveal one hint at a time.
Hint 1
Resolve every multiplication and division first, wherever they appear, before doing any addition or subtraction.
Hint 2
Parentheses always win — whatever is inside them is computed before anything outside them, regardless of what operators surround it.
Reveal the results
Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.
3 + 4 * 2 -> 4 * 2 = 8, then 3 + 8 = 11
(3 + 4) * 2 -> 3 + 4 = 7, then 7 * 2 = 14
20 / 4 - 1 -> 20 / 4 = 5.0, then 5.0 - 1 = 4.0
20 / (4 - 1) -> 4 - 1 = 3, then 20 / 3 = 6.666...Practice: apply it
A store applies a 20% discount, then adds 8% tax — but this line gets the wrong result:
price = 50
final_price = price - price * 0.20 + price * 0.08
print(final_price)
Or reveal the answer without checking
Answer:The tax is calculated on the original price instead of the discounted price, because there are no parentheses forcing the discount to apply first
Without parentheses, price * 0.20 and price * 0.08 are each computed from the original price independently — tax should be calculated on the discounted amount, not the original.
Modification challenge: rewrite this using a variable for the
discounted price first (discounted = price - price * 0.20), then
compute tax on discounted rather than the original price.
Summary
- An expression is anything that reduces to a single value; expressions can nest inside larger expressions and inside other code, like function arguments.
- Operator precedence — not left-to-right reading order — decides what’s computed first: multiplication and division before addition and subtraction.
- Parentheses override precedence completely, and are the fix whenever the order you want doesn’t match it.
- A statement (like an assignment) performs an action but doesn’t itself produce a value the way an expression does.