Python Part I · Module 3 · Operators and Expressions
Expressions: Code That Produces a Value
Module 2 left you able to hold values under names and find out what they are. What you could not do was compute with them — every example stored something and displayed it, and nothing ever combined two values into a third. This module is about that, and it starts with the machinery rather than with a list of symbols. An expression is any piece of code Python can work out; evaluating it means working it out; and the result is always exactly one value, with a type. Once that model is solid, every operator family in the rest of the module is just a different thing you can ask for, and you will be able to predict what comes back rather than run it and hope.
Foundation9 min readPythonPart IFundamentalsExpressionsEvaluation
Part I · Module 3 · Chapter 3.1 · Expressions: Code That Produces a Value
1. What an Expression Is
An expression is any piece of code Python can work out to produce a value.
All of these are expressions:
print(4210)
print(10 + 5)
print(600 - 11)4210
15
589The first one is interesting. 4210 on its own is already an expression — Python works it out, and the answer is 4210. An expression does not have to do arithmetic; it only has to produce a value.
A name is an expression too:
seed = 4210
print(seed)4210Python worked out what seed refers to, and that was the value.
2. Evaluation
Evaluating an expression means working it out. The result is always exactly one value.
10 + 5 ← the expression
↓ ← Python evaluates it
15 ← the resulting valueThat is the whole model, and it is worth holding on to because it does not change for the rest of the module. Whatever operators appear, whatever types are involved, an expression reduces to one value.
3. The Result Has a Type
Module 2 gave you type(), and it is immediately useful here, because the type of what comes out is not always obvious from what went in.
print(type(10 + 5))
print(type(10 / 5))<class 'int'>
<class 'float'>Two numbers went into each. One produced an int, the other a float. That is not an accident and Chapter 3.2 explains exactly why — the point here is only that the operation decides the result type, not just the inputs.
You will use type() several times in this module for exactly this reason.
4. Expression or Statement
A distinction worth having straight, because it explains something you already do.
An expression produces a value. A statement performs an action.
total = 10 + 5That line contains both. 10 + 5 is an expression. The whole line is an assignment statement.
And you already know the order, from Chapter 2.3 §4: Python evaluates the right-hand side first, then points the name at the result.
total = 10 + 5
↓ Python evaluates the expression
total = 15 then assigns the resulting valueSo an assignment is a statement whose right-hand side is an expression. That is why count = count + 1 made sense in Chapter 2.3 — the expression was worked out before anything was stored.
5. Expressions Can Contain Expressions
An expression can be built from smaller ones, and Python works out the inner parts first.
print((10 + 5) * 2)30Traced step by step:
(10 + 5) * 2
↓ work out the bracketed part
15 * 2
↓ now the multiplication
30The brackets here are doing two jobs: they group the addition, and they make the order visible to you as a reader. Chapter 3.8 covers what happens when you leave them out, and what order Python uses then. Until that chapter, the examples in this module will either use one operation at a time or bracket the order explicitly.
6. Names Are Evaluated Too
An expression can mix names and values freely. Python works out what each name refers to, then does the operation.
total_tests = 120
failed_tests = 7
print(total_tests - failed_tests)113Traced:
total_tests - failed_tests
↓ each name is worked out
120 - 7
↓
113The names were not changed by this. total_tests still refers to 120. An expression produces a new value; it does not modify what it was built from.
7. Not Every Expression Is Arithmetic
Worth flagging now, because it shapes how you read the next few chapters.
print(3 > 7)False3 > 7 is an expression. Python evaluated it and the resulting value is False — the boolean from Chapter 2.8.
8. Predict, Run, Observe
For each, predict the value and its type, then run it.
print(100)
print(100 - 1)
print(100 / 4)
print(100 * 2)
print(2 < 9)
print(type(100 / 4))
print(type(2 < 9))100
99
25.0
200
True
<class 'float'>
<class 'bool'>Two to check yourself on: 100 / 4 produced 25.0 rather than 25, and 2 < 9 produced a bool rather than a number. Both are covered properly in the next few chapters — if you predicted them, you are already reading expressions rather than symbols.
9. Exercise
Exercise 1 — expression or not. For each, say whether it is an expression that produces a value: (a) 4210 (b) seed (c) 10 + 5 (d) seed = 10 (e) 3 > 7
Exercise 2 — trace it. Write out the evaluation of (120 - 7) * 2 the way §5 does, one step per line. Then run it and check.
Exercise 3 — predict the type. Without running, predict the type of each result, then verify with type():
(a) 50 + 50 (b) 50 / 50 (c) 50 > 50
Exercise 4 — the unchanged name. Predict all three lines of output, then run:
total = 100
print(total - 1)
print(total)Explain in one sentence why the last line prints what it does, using the phrase "produces a new value".
10. Summary
- An expression is code that produces a value. A bare value is one; a name is one; arithmetic is one.
- Evaluating means working it out, and the result is always exactly one value.
- The result has a type, and the operation helps decide it —
10 + 5gives anint,10 / 5gives afloat. - A statement performs an action; an expression produces a value. An assignment is a statement whose right-hand side is an expression, evaluated first.
- Expressions nest, and Python works out the inner parts first. Until Chapter 3.8, order is shown with brackets rather than assumed.
- An expression does not modify the names it was built from. It produces something new.
- A comparison is an expression producing a
bool, not yet a condition. Conditions are Module 9.
The machine is now in place. The next chapter fills it with the operator family you will use most.
Where this fits
Part of the Python curriculum.
