Lesson 4: Variables: Storing Things


What you'll learn in this lesson


A variable is a labeled box

A variable is a name that holds a value. Think of it as a labeled box: you put something inside, and whenever you use the name later, Python looks inside the box and uses what it finds there.

You create a variable by assigning a value to a name with the = sign. Read = here as "store", not as "equals" in the math sense:

name = "Mia"
age = 9
print(name)
print(age)

Output:

Mia
9

name now holds the text "Mia", and age holds the number 9. When you print(name), Python does not print the word "name" — it prints the value stored under that name.

Here is the payoff — the same name works as many times as you like:

name = "Mia"
print("Hello,", name)
print("Welcome back,", name)

Output:

Hello, Mia
Welcome back, Mia

That is the whole point of variables: one name, reusable wherever the value is needed.

Note the quotes rule from Lesson 3: name = "Mia" stores text, because of the quotes. age = 9 stores a number, because there are no quotes. If you wrote age = "9", Python would store the text "9", not the number — and as you will see in Lesson 5, text and numbers behave very differently in arithmetic.


Changing a value

A variable's value can change while the program runs. Each new assignment replaces what was there before:

score = 0
print(score)
score = 10
print(score)
score = score + 5
print(score)

Output:

0
10
15

The last line looks strange at first: score = score + 5. Read it as "take the current value of score (10), add 5, and store the result (15) back in score." This pattern — update a value from its old self — is one of the most common moves in all of programming. You will use it again in the guessing game, to count tries.


Naming rules

Variable names have a few rules. Some break your program right away; others fail quietly later. Learn them now:

Good names describe what is inside: name, age, score, price. Bad names describe nothing: x, a, thing. When you come back to your own code a week later, good names are what make it readable.


Name versus string: the mistake to watch

Beginners mix these up more than anything else:

name = "Mia"
print(name)
print("name")

Output:

Mia
name

print(name) prints the value stored under the name. print("name") prints the word itself, because quotes mean "this is text." If you ever see a program print a variable's name instead of its value, or give a NameError, check the quotes first.


Hands-on exercise

  1. Create two variables, one holding your name (text) and one holding your age (a number), and print both.
  2. Create a variable points set to 0, print it, then add 20 to it the way the lesson shows (points = points + 20), and print it again.
  3. Try printing a variable you never created, read the error message, and explain in one sentence what it means.

Sample answer for 3: Python says NameError, which means I used a name that has no value stored under it yet.


Quick quiz

  1. What is a variable?
  2. What does age = 9 store, and how is it different from age = "9"?
  3. What does score = score + 5 do, step by step?
  4. Which of these names are NOT allowed, and why: my_score, 2score, my score?
  5. What is the difference between print(name) and print("name")?

Answer key: 1. A name that holds a value you can reuse later. 2. The number 9; "9" stores text, which cannot be used in arithmetic the same way. 3. It reads the current value of score, adds 5, and stores the result back in score. 4. 2score (a name cannot start with a number) and my score (no spaces allowed). 5. The first prints the value stored under the name; the second prints the word itself.


Key takeaways

Next lesson: Numbers and Arithmetic.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

Download lesson04-practice.pyDownload the solution

# Lesson 4: Variables — Storing Things
# WHAT TO DO: A variable is a name that holds a value. This program
# stores a name and an age, then a points score that should start at 0
# and grow by 20. But one line is wrong. Fix line 14: change the
# starting points from 999 to 0.
# HINT: Only change the number 999. The next lines already add 20 and
# print the score, so a correct fix prints 20 at the end.
# Run with F5 in IDLE. You are done when the last line prints 20.

name = "Coder"
age = 12
print(name)
print(age)
points = 999  # <-- CHANGE THIS LINE: points should start at 0
print(points)
points = points + 20
print(points)

Save the file as lesson04-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.

Check yourself

Five quick questions on this lesson. Take the Lesson 4 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.