CurriculumHow Python Runs Your Code, for KidsChanges stick — but only going forward

Why a Python Variable Change Only Affects Later Lines

A change made on one line is real for every line after it, and invisible to every line before it.

Lesson 3 of 4FreeAges 10+
Try this lesson interactively →

Changes stick — but only going forward

One more thing about order: a change only affects what comes AFTER it — never before.

The door starts closed. It's only 'open' for the lines written after the line that opens it.

print("CLOSED")
print("OPEN THE DOOR")
print("OPEN")

Output

CLOSED
OPEN THE DOOR
OPEN

Notice the FIRST line still says CLOSED — even though the door ends up open later. It ran before the change, so it can't know about it.

And whatever your code says most recently is what's actually true right now — the LAST change always wins.

Check your understanding

print("CLOSED") runs BEFORE print("OPEN THE DOOR"). What does the first line say?

print("CLOSED")
print("OPEN THE DOOR")
print("OPEN")
  • CLOSED — because that's what was true when it ran
  • OPEN — since the door ends up open eventually

Why: CLOSED. The first line runs before the door opens, so it reports exactly what was true then — not what happens afterward.

A torch gets lit, then put out, then lit again. What's actually true at the very end?

  • Lit — because that's the LAST thing that happened
  • Out — because that was mentioned too

Why: Lit. The most recent change always wins — the torch was lit last, so lit is its true final state.

What you'll practice

The door starts CLOSED. Print its state, then print the line OPEN THE DOOR, then print its new state. The door is only OPEN for the print that comes AFTER you open it.

Start this lesson for real →

More from this topic

Changes stick — but only going forward is one lesson inside How Python Runs Your Code, for Kids — see the full lesson order and what the whole topic covers.

View the full topic →