Computing route · lesson 1 of 5
  1. 01Procedures and stateUnseen
  2. 02Data and invariantsUnseen
  3. 03Algorithms and costUnseen
  4. 04Ownership and systemsUnseen
  5. 05Learning systems and evaluationUnseen

Computing / Field trial lesson 01

Procedures and state

Read a small program as a sequence of inspectable state changes, predict a loop before running it, and separate rebinding from mutation.

Study time
Awaiting learner observation
Evidence
Unseen
Candidate
0.1-foundations-candidate
Review
Named human reviews pending
Field-trial contentComplete enough to test; not published as reviewed Foundations curriculum.
01 / Orient

Debugging begins by slowing time down

A program does not execute as a paragraph. It advances through instructions, and each instruction sees a particular state. When the result surprises you, the useful question is not “What does this code look like it means?” but “What values were available immediately before this instruction, and what changed immediately after it?”

For this lesson, state means the bindings and object contents relevant to the next step. A trace is a compact record of those states. It turns execution from an invisible event into a sequence you can inspect, challenge, and reproduce.

width = 4
width = width + 3
area = width * 2
Three statements, read in execution order.
State trace
statementwidtharea
width = 44not bound
width = width + 37not bound
area = width * 2714
State changes one instruction at a timeFigure 1. Each row records the state after one statement; the highlighted cell is the binding changed by that statement.
Read the figure description

Row one executes width equals 4 and records width as 4. Row two evaluates the old width plus 3, then rebinds width to 7. Row three evaluates width times 2 and binds area to 14; width remains 7. The rows make evaluation order explicit.

Check your model

Arrange the reasoning for x = x + 2 when the current x is 5.

Unseen

Put all four operations in execution order.

  1. Rebind x to 7
  2. Read the current value 5 through the name x
  3. Record the after-state x equals 7
  4. Evaluate 5 plus 2 to obtain 7
02 / Model

A loop is repeated small work, not one mysterious leap

Python's for statement visits items from an iterable in order. On each visit, the loop target is bound to the next item and the body runs. You can therefore explain a loop by repeating the same local questions: What item is current? What is the accumulator before the body? What is it afterward?

An invariant is a statement that remains true at a chosen point in every iteration. For an accumulating sum, a useful invariant is: after the body finishes, total equals the sum of every item visited so far. The invariant is stronger than a guessed final answer because it explains every intermediate row.

total = 0
for item in [2, 4, 6]:
    total = total + item
A three-iteration accumulation procedure.

Worked example

Worked example — trace an accumulator

Explain the final value of total without running the code.

  1. Before the loop, total is 0 and no item has been visited. The invariant holds because the sum of no visited items is 0.
  2. First visit: item is 2. Evaluate old total plus item, which is 0 plus 2, then rebind total to 2.
  3. Second visit: item is 4. Evaluate 2 plus 4, then rebind total to 6.
  4. Third visit: item is 6. Evaluate 6 plus 6, then rebind total to 12.
  5. The iterable is exhausted. The invariant says total is the sum of 2, 4, and 6, so the final value is 12.

ConclusionThe trace 0, 2, 6, 12 explains both the final answer and the work performed at each step.

Check your model

A loop starts total at 0 and visits [3, 6, 9], adding each item to total. What is total after the second iteration?

Unseen

Enter the integer value. Use the unit “integer”.

03 / Formalize

Function calls create a boundary, not a magical box

A function definition gives a procedure a name and parameters. Calling the function binds its parameter names to the supplied argument objects, executes the body, and returns a result. The local names belong to the call, but a mutable argument object may still be shared with the caller.

This distinction matters. Rebinding a local parameter changes which object that local name refers to. Mutating a shared list changes the contents of the object itself, so the caller can observe the change. A state trace should record both bindings and the contents of mutable objects when either can change.

def add_tax(prices):
    taxed = []
    for price in prices:
        taxed.append(price * 1.05)
    return taxed

original = [10, 20]
result = add_tax(original)
The function builds a new list; it does not append to the argument list.

Worked example

Worked example — prove that the input list stays unchanged

Trace the call add_tax(original) and explain why original remains [10, 20].

  1. At the call boundary, the local name prices refers to the same list object as original.
  2. The statement taxed = [] creates a new empty list and binds only the local name taxed to it.
  3. Each loop iteration appends a computed value to the new taxed list. No statement targets prices or the original list object.
  4. The function returns the new list [10.5, 21.0], which is bound to result by the caller.
  5. The object referred to by original was read but never mutated, so its contents remain [10, 20].

ConclusionShared access does not imply mutation. The operations performed on each object determine what the caller observes.

Check your model

Which statements would mutate the existing list bound to items? Select every statement that changes that object's contents.

Unseen

Select all mutating operations.

Answer choices
04 / Experiment

Predict the next row before revealing it

Prediction before motion

With loop length 3 and input step 2, predict the current item and total after the second iteration. State the invariant in your own words before revealing the row.

Invariant
After each body execution, total equals the sum of every item visited so far.
Observe
Change the input step while holding loop length fixed. Which cells change, and why does the invariant remain true?
Static account
With items 2, 4, and 6, the before and after totals are 0 to 2, 2 to 6, and 6 to 12. Increasing the input step changes each item and total, but each after-total still equals the visited sum.
Static trace for loop length 3 and input step 2
IterationCurrent itemTotal beforeTotal afterInvariant check
12022 equals sum of visited items
24266 equals sum of visited items
3661212 equals sum of visited items
05 / Transfer

Trace a conversion function without executing it

A trace is most useful when it predicts code you have not run. In the transfer task, a function converts temperatures into a new list. You must record the loop's state and preserve the distinction between the input object and the result object.

The assessment uses a structured state trace rather than another multiple-choice prompt. That change of form matters: demonstrated evidence should show that the same idea survives a new representation.

Transfer task

A function starts converted as an empty list, visits temperatures [0, 10], and appends c times 9 divided by 5 plus 32. Submit the two after-iteration states.

Unseen

For each row, provide c and converted using the exact displayed values.

Your after-iteration states
Stepcconverted
1
2