Variables: Storing Things

What you’ll learn in this lesson

  • What a variable is, and why programs need one
  • How to store text and numbers in variables
  • How to change a variable’s value
  • The rules for naming variables
  • The difference between a name and a string

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:

  • Use letters, numbers, and underscores only: my_name, score2.
  • A name cannot start with a number: 2nd_score is not allowed, but score2 is fine.
  • No spaces: use an underscore instead of a space, my_name not my name.
  • Do not use a word Python already uses, like if or for — Python stops with a SyntaxError as soon as it sees one.
  • Do not store a value in a built-in name like 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.
  • Names are case-sensitive: 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.


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.


Variables: Storing Things

Python for Absolute Beginners · Lesson 4

What you’ll learn

  • What a variable is, and why programs need one
  • How to store text and numbers in variables
  • How to change a variable's value
  • The rules for naming variables
  • The difference between a name and a string
1 / 13

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
2 / 13

Storing text and a number

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

Output:
Mia
9

name holds the text “Mia”, and age holds the number 9. print(name) prints the value stored under that name — not the word “name”. The same name works as many times as you like — print(“Hello,”, name) prints Hello, Mia again.

3 / 13

The quotes rule, again

  • 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
  • As you will see in Lesson 5, text and numbers behave very differently in arithmetic
4 / 13

Changing a value

  • A variable's value can change while the program runs
  • Each new assignment replaces what was there before
5 / 13

Updating a score

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

Output:
0
10
15

Read the last line as: take the current value of score (10), add 5, and store the result (15) back in score.

6 / 13

Naming rules

  • Use letters, numbers, and underscores only: my_name, score2
  • A name cannot start with a number: 2nd_score is not allowed, but score2 is fine
  • No spaces: use an underscore instead, my_name not my name
  • Do not use a word Python already uses, like if or for — Python stops with a SyntaxError right away
  • Do not store a value in a built-in name like print or list — Python lets you do it, but print stops working afterward
  • Names are case-sensitive: Score and score are two different variables, and Python will not warn you — pick one style and stay consistent. Python programmers use lowercase words joined by underscores, like my_score
7 / 13

Good names, bad names

  • 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
8 / 13

Name versus string

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.”

9 / 13

Hands-on exercise

  • Create two variables, one holding your name (text) and one holding your age (a number), and print both
  • 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
  • Try printing a variable you never created, read the error message, and explain in one sentence what it means
10 / 13

Key takeaways

  • A variable is a labeled box: a name holding a value
  • = means “store”, and a new assignment replaces the old value
  • score = score + 5 updates a value from its old self
  • Names use letters, numbers, and underscores; no spaces, no leading number, and case matters
  • Quoted text is text; an unquoted name means “look up the stored value”
11 / 13

Take the quiz

Take the Lesson 4 quiz — 5 quick questions to check what you learned.

12 / 13

Next lesson

Numbers and Arithmetic

13 / 13

Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.

Download the Lesson 4 slides (PowerPoint, with narration)

Download the Lesson 4 slides (PDF)

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.


Lesson 04: Variables: Storing Things

A variable stores a value under a name. Change the stored name and run again to see the stored value change.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

Try this: Change the name to your own name and press Run again — the first line changes, and 9 stays the same.

The code that runs



Output

Press Run to see the output here.

Practice file

Save this as lesson04-practice.py and run it with F5 in IDLE.

# 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)

Key takeaways

  • A variable is a labeled box: a name holding a value.
  • = means "store", and a new assignment replaces the old value.
  • score = score + 5 updates a value from its old self.
  • Names use letters, numbers, and underscores; no spaces, no leading number, and case matters.
  • Quoted text is text; an unquoted name means "look up the stored value."

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.

Next lesson: Numbers and Arithmetic.