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.
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.
Variable names have a few rules. Some break your program right away; others fail quietly later. Learn them now:
my_name, score2.2nd_score is not allowed,
but score2 is fine.my_name not
my name.if or for — Python
stops with a SyntaxError as soon as it sees one.print or list.
Python lets you do it, with no error at the time — but the print
instruction stops working afterward, and the confusing error shows
up somewhere else entirely.Score and score are two different
variables, and Python will not warn you. Pick one style and stay
consistent. Python programmers conventionally use lowercase words
joined by underscores, like my_score.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.
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.
points set to 0, print it, then add 20
to it the way the lesson shows (points = points + 20), and print
it again.Sample answer for 3: Python says NameError, which means I used a name that has no value stored under it yet.
age = 9 store, and how is it different from age = "9"?score = score + 5 do, step by step?my_score, 2score, my score?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.
= means "store", and a new assignment replaces the old value.score = score + 5 updates a value from its old self.Next lesson: Numbers and Arithmetic.
Work through the slides with Helio's narration, or download them:
Open the slidesDownload PowerPointDownload PDF
Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).
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.
Five quick questions on this lesson. Take the Lesson 4 quiz
Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.