Variables

A free sample lesson from Python Fundamentals

The idea

Imagine a labeled box on a shelf - the label says score, but what's inside the box can change any time you want. In Python, writing score = 10 puts the number 10 inside the box labeled score. You can open the box again later, look at what's inside, or swap it for something new - the label never changes, only the contents do.

What it actually is

A variable is a name you give to a piece of data so your program can use it again later, the same way a person's name lets you refer to them instead of pointing every time. Once you create one, you can use its name anywhere in your code to get the value back, or replace it with something new.

How you write it

variable_name = value

A worked example

player_name = "Aanya"
lives = 3
print(player_name)
print(lives)

Line 1 creates a variable called player_name and stores the text "Aanya" inside it. Line 2 creates a second variable, lives, storing the number 3. The print lines then look up whatever is currently stored under each name and show it - the name never changes, only what's stored under it can.

score = 10
trophy = score
score = 20
print(trophy)

Line 2 copies the NUMBER 10 that score held at that moment into trophy - it does not link the two variables together. Changing score afterward (line 3) never affects trophy, so trophy still prints 10: each variable is its own independent box.

Mistakes children actually make

Mistake 1: thinking two variables set from each other stay linked. score = 10 then trophy = score copies the number into trophy - if score changes afterward, trophy does NOT change with it; they're independent from that point on. Mistake 2: misspelling a variable's name later in the code (e.g. creating plyer_name but printing player_name) - Python won't guess what you meant, it raises NameError: name 'player_name' is not defined, because as far as Python knows, that name was never created. Mistake 3: giving a variable an invalid name, like 1st_place = 10 (can't start with a digit) or class = "Wizard" (class is a reserved word Python already uses) - both crash with a SyntaxError before the program even runs, since Python needs every variable name to start with a letter or underscore and can't reuse its own keywords.

Then they try it

In the app this lesson continues with the animated explanation, spoken aloud, and then the practice: Change `score` to 10 instead of 0, then print it - same box, new contents. The editor runs your child’s real code and checks the result, with their coding buddy reacting to what they wrote.

Try it free