CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 03 / BEGINNER PYTHON

Python variables: names, values and state

A variable is a name bound to a value. Assignment with = binds a name; it does not ask whether two things are equal.

The idea

Think of a label on an item in the case file. The label clues lets you refer to its current value later. In clues = clues + 1, Python first reads the old value, adds one and then binds the name to the result. This is a step in time, not an algebra equation that must be true forever. The current values a program remembers are part of its state.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
clues = 2
old_count = clues
clues = clues + 1
print(clues)
print(old_count)
Run and change this example ↗
Reveal the expected output
3
2

Read it one step at a time

  1. clues begins with the integer 2. Integer means a whole number.
  2. old_count also refers to 2. It is not a formula that will automatically follow clues.
  3. After reassignment, clues refers to 3. old_count still refers to 2.

Make one change

Add another clues = clues + 1 before the prints. What should old_count show?

Check your thinking

clues becomes 4 and old_count remains 2.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.