Quarry School

Give a value a name — quick review

In plain words

Think of a coat check.

Rule

An assignment has a name to the left of = and a value or calculation to the right. Python works out the whole right-hand side first, then ties the left-hand name to the result. If that name already had a value, it now names the new one instead. No other name changes.

Step

  1. Draw a state table with a column for the line, a column for what the right-hand side works out to, one column for each name and a column for output.

  2. Take the next line of the program. If it is an assignment, cover the left side with your finger and work out the right side, using the values in the row above.

  3. Write that result in the column of the name on the left. Carry every other name's value down unchanged from the row above.

  4. If the line is a print, write what it displays in the output column: the values in the order given, with one space between them.

  5. Repeat until the last line. The output column is your prediction. Compare it with the real output if you have Python 3.13, or with the printed answer if you are working on paper.

Trap

The usual mistake is to predict 5 5. It comes from reading spare = boxes as a lasting link, as if spare will always match boxes. That line ran once, at step 2, and tied spare to 4. Step 3 moved only the boxes tag, because boxes was the only name on the left.

A second mistake is reading = as the equals sign from arithmetic. Read that way, boxes = boxes + 1 looks like nonsense, since no number equals itself plus one. In Python, = means "tie this name to this value". The comparison "is equal to" is written with two signs, ==, and a later unit teaches it.

Avoid saying that spare = boxes "makes a copy". The Python tutorial states that a simple assignment never copies data. It puts a second name on the value that is already there. This lesson keeps to whole numbers on purpose.