The Knowledge Institute
← Back to library

Programming Foundations · 1 MIN READ

Read a program by tracing its values

Track what each instruction changes before guessing what a program will print.

A name refers to a value

A program makes instructions precise enough for a computer to execute. In Python, assignment associates a name with a value. The statement quantity = 3 establishes a value that later expressions can use. A name is not a permanent description of the world: another assignment can change what it refers to. Reading code therefore means following execution order, not just recognizing familiar words.

Worked example: a shop estimate

Trace these instructions in order: quantity = 3; unit_price = 12; subtotal = quantity * unit_price; quantity = 4. The stored subtotal is still 36. Changing quantity does not automatically rerun the multiplication that produced subtotal. If the next instruction is subtotal = quantity * unit_price, subtotal becomes 48. Draw a table with one row per instruction and columns for quantity, unit_price, and subtotal. Leave a cell blank until its name has been assigned.

Check the meaning as well as the arithmetic

The trace describes these instructions, not every kind of program. A spreadsheet formula or a reactive application may arrange recalculation explicitly. Also distinguish text from numbers: a quoted value such as "12" is text, while 12 is a number. State the required units and data types before calculating. An output can be computationally consistent while answering the wrong business question.

Put it to work

Trace price = 8; count = 5; total = price * count; price = 9. What is total now? Which extra instruction would update it?

Knowledge check

1. After quantity changes to 4, but before subtotal is assigned again, what is subtotal?
2. What is the best way to explain an unexpected output?

Sources & further reading