The Python execution model · Public lesson

Program state and the variable table

How do you follow the values of a Python program's variables line by line? Program state, the variable table, a worked trace and two exercises checked instantly.

Beginner · about 20 min · Free, no sign-in required

By the end of this lesson

  • Write down the variable table of a short program step by step.
  • Answer what value a variable holds at a given point of execution.
  • Name which variable's value changed in which step.

Prerequisite: If this is new to you, start here: Execution order (Available with an account)

The question that starts every debugging session

When a program gives a wrong result, the useful question is not "where is the bug in the code?". It is:

What is true right now? - that is, which value belongs to which name at the moment where execution currently is.

This is called the program state. It is a snapshot: names and their current values. Not the source code, but what the run has produced so far.

The state depends on two things, and only on these two:

  1. which statements have run so far (execution order);
  2. what each of them did.

The variable table

We do not keep the state in our head; we write it down. That is the variable table: every step is a row, every variable is a column, and there is one column for the output.

Four steps, four states

Figure description

The program has four lines: x = 4, then y = x + 6, then print(y), and finally x = 0. After step 1, x is 4, y does not exist yet, and there is no output. In step 2 the right-hand side is evaluated first - 4 + 6 is 10 - and only then is y bound to it; x stays 4. In step 3, print writes 10 to the screen; the state does not change. In step 4, x becomes 0, y stays 10, and the output still shows 10, because printing recorded what was true at the moment of the print.

The table tells you three things at once, and each is useful on its own:

  • time - the order of the rows is the order of execution;
  • state change - in a row, usually exactly one cell changes;
  • output versus state - print writes to the output column, not to the state.

Writing execution down step by step like this is called an execution trace. It is not decoration: it is the tool that turns "I don't understand what is happening" into "it got a wrong value in step 3".

A complete trace, by hand

Worked example

Five steps of a stock count

stock = 40
arrival = 12
stock = stock + arrival
sale = 30
stock = stock - sale

Predict. Before you read on: what is stock at the end? Write the number down before you look at the table.

The trace:

Step Current statement stock arrival sale
start - none none none
1. stock = 40 40 none none
2. arrival = 12 40 12 none
3. stock = stock + arrival 52 12 none
4. sale = 30 52 12 30
5. stock = stock - sale 22 12 30

Three things are worth looking at separately.

One row, one change. Except in steps 3 and 5, a new name appears in every row; in steps 3 and 5 an existing value is replaced. If two columns change in one row, it usually means you rushed a step.

The old value disappears. After step 3, nothing keeps the earlier value 40 of stock. There is no "previous value" - only what is there now.

The right-hand side uses the old state. In step 3, when stock + arrival is computed, stock is still 40. A separate lesson builds on this step, because this is the point where most people slip.

Self-check

In which step, and why?

In the trace above: (1) after which steps did the value of stock change? (2) Did step 4 change the value of stock? (3) What would happen if lines 4 and 5 were swapped?

Solution and explanation

(1) After steps 1, 3 and 5. (2) No: step 4 only creates the name sale; stock stays 52. (3) It would stop with an error: in the swapped order the name sale would not exist yet when the subtraction refers to it. It is the same rule as in the previous lesson - Python does not look ahead.

Why the table, and not print straight away?

Printing variables (print("stock:", stock)) is faster, and later it will often be enough. Still, the table is worth learning first, for three reasons:

  • it works without running anything - while reading code, during a review or in an exam;
  • it forces you to predict - print tells you the answer, the table asks you first;
  • it shows what did not change - print only shows what you asked for; the table has every name, so you also see when something stubbornly keeps its initial value.

It is worth coming back to print when the number of steps runs away - say, on a list of a hundred elements. From then on the table is the model in your head, and print is the check.

What comes next

Row 3 of the table raised a question we have side-stepped so far: when the right-hand side of a statement uses the same name that it is about to overwrite on the left, which value goes into the calculation? The next lesson is about that.

Practice now

Two short exercises. Your browser checks the answer instantly; nothing is saved and no account is needed.

Predict the value

What is the value of price after all four lines have run?

price = 100
discount = 20
price = price - discount
discount = 50

Fill in the variable table

Fill in the variable table: what are the values of a and b after each line has run? If a name does not exist yet, leave it empty or type: none.

a = 3
b = a + 2
a = a * 2
b = a - b

All free learning

Publisher: Contour · Editorially reviewed: 2026-11-21