State, expressions, and scope
September 18, 2026
Last time we built the notional machine: a finger moving down the lines, some memory, and a screen.
Today we look inside two of those pieces:
Concepts again — but today every one of them gets checked against the real machine. Keep the REPL open. Syntax details come in Lecture 3.
The machine’s memory is a set of labelled boxes. A label is a name; the box holds a value.
Read this as an instruction:
“Make a box labelled
xand put the value5in it.”
The state of a program is the collection of all its name → value bindings at a given moment. Running the program is nothing but changing the state, step by step.
x is not "x"In Lecture 1 the quotes marked the data print acts on. They still do. Quotes mean “this text, exactly as written”. No quotes means “go to the box with this label and take out what is inside”.
x
5
Hello, name
Hello, Ada
"x" is a value: the one-letter text x, the same wherever it appears. x is a name: a label on a box. The machine never prints a label — it opens the box and prints what it finds.
= puts a value in a box6
The line x = x + 1 is nonsense as algebra but perfectly clear as an instruction:
| Step | Action | State after |
|---|---|---|
| 1 | x = 5 |
x → 5 |
| 2 | evaluate x + 1 → 6, store in x |
x → 6 |
Later we meet ==, which tests equality. = changes state; == asks a question.
Trace tables now get a state column:
3 10
| Step | Action | a |
b |
|---|---|---|---|
| 1 | a = 2 |
2 | — |
| 2 | b = 3 |
2 | 3 |
| 3 | a = b |
3 | 3 |
| 4 | b = 10 |
3 | 10 |
After step 3, a is 3. Changing b afterwards does not change a: each box holds its own value.
5, "hello", True.2 + 2, x, len("cat").x = 5, print(x).Rough test: if you can put it on the right of an =, it’s an expression. If it stands alone and acts, it’s a statement.
The machine evaluates an expression down to a single value before using it.
14
Evaluation of the right-hand side:
3 * 4 → 122 + 12 → 1414 in totalMultiplication before addition — the machine follows the usual arithmetic precedence. When in doubt, add parentheses.
30
To evaluate price * qty, the machine first looks up each name in the current state (price → 10, qty → 3), then multiplies.
If a name has no box yet, the lookup fails:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 total = cost - discount NameError: name 'discount' is not defined
+ doesSame operator, different values in the boxes. Predict each output before the click.
4
22
222
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 print("2" + 2) TypeError: can only concatenate str (not "int") to str
Every value has a type: 2 is a number, "2" is a piece of text. The machine looks at the types first, then decides what + means — add, or glue together. Mix them and it refuses: a TypeError — a third error name, to put beside the two we already met, SyntaxError and NameError. Next lecture is all about types.
As programs grow, we reuse short names like x, i, result. If every name were visible everywhere, they’d collide constantly.
Scope is the region of a program where a given name is visible and usable. A name created in one scope may be invisible in another.
We meet scope properly with functions (Lecture 5), but the idea starts now.
hi NameError: name ‘message’ is not defined
message exists only inside greet — its local scope. Outside, the name is unknown. This is a feature: the function’s internals stay private.
When the machine meets a name, it searches scopes inside-out:
print, len, …).First match wins; if none match → NameError.
Scope is just the machine’s rule for resolving a name to a box. Keep it in your notional machine and “why can’t it see my variable?” stops being a mystery.
The function computed total, then returned, and its local scope vanished. To get a value out of a function we must return it — that’s Lecture 5.
= assigns (changes state); == tests (asks a question).Next: the actual data types that go in those boxes.
Four short programs, two here and two on the next slide. For each one: does it stop the machine, and with which error? Decide first, then click.
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[11], line 2 1 score = 5 ----> 2 print(score + bonus) NameError: name 'bonus' is not defined
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[12], line 1 ----> 1 print("a" + 5) TypeError: can only concatenate str (not "int") to str
NameError — score has a box, bonus never got one · TypeError — both values exist, but + cannot join text and a number.
aaaaa
5
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[14], line 3 1 Points = 5 2 print(Points) ----> 3 print(points) NameError: name 'points' is not defined
In order — NameError (no box called bonus) · TypeError (text plus number) · no error at all, aaaaa · NameError (Points and points are two different labels).
Without running it, write down the value of every name at the end:
Then check with print(p, q).
Solution — p → 2, q → 5, p → 5, q → 10, p → 0. Final: p = 0, q = 10.
What exactly appears on the screen?
fruit
apple
fruit pear
Solution — fruit · apple · fruit pear. Quotes print the text itself; no quotes prints what is in the box, whatever is in it at that moment.
Start from count = 0. Write instructions that leave count holding 3 — without the digit 3 appearing anywhere in your program — printing the value after every step.
Solution — three times count = count + 1, each followed by print(count). The ban on writing 3 is what forces you to change state step by step instead of jumping to the answer.
Predict the output line by line, then run it:
Solution — 10, then 5, then NameError: name 'bonus' is not defined. Inside show, total is not local, so the lookup moves outward and finds it. bonus is local: when the call ends its box is gone.
For each line, say whether it is an expression or a statement, and if it’s an expression, what value it evaluates to (assume x = 3).
Solution — x + 7 expression → 10 · print(x) statement (acts: displays 3) · x = x + 7 statement (changes state) · len("banana") expression → 6 · "a" * x expression → "aaa".
Given a = "left" and b = "right", write instructions so that afterwards a holds "right" and b holds "left".
Hint: if you just write a = b then b = a, trace the state — why does it fail? Introduce a third box to fix it. (Later you’ll learn Python’s one-line trick, but do it the explicit way first.)
Solution — after a = b both boxes hold "right"; "left" is lost before b = a runs. Park it first: tmp = a · a = b · b = tmp → a = "right", b = "left".
Intro to Programming (Python) · Fall 2026