Basics · lesson 3 of 10
Parts in this lesson
Aname = valuegives a value a name
B+= -=change by an amount
Ctype()what kind of value
Df"{name}"from 1.2, now with names

Variables

Last updated

Programs keep track of things: a book's title, how many copies are on the shelf. In this lesson you'll give values names, change them, see that a name points at a value rather than holding it, and read the error Python gives when a name has no value yet.

By the end of this lesson you can

  • Give a value a name with =, and use the name instead of the value
  • Change what a name points at, including with +=
  • Use type() to see what kind of value a name points at
  • Read a NameError and fix it

Give a value a name

= gives a value a name. The name goes on the left and the value on the right. After line 1, the program can write title wherever it means "Dune".

A name for a value is called a variable. Inside an f-string, a name in braces is replaced by its value, which is how you'll use f-strings most of the time. Press Watch it run and step through: the Values table shows each name as it's made.

Step 1 code main.py
Edit the code, then run it again

In Python, = doesn't mean "is equal to" the way it does in maths. It means make this name point at this value. You'll see how Python checks whether two things are equal in lesson 1.6.

Change it

A name can be given a new value at any time, with another =. Line 3 works out copies - 1 using the value copies has now, then points copies at the answer.

Changing a value by an amount is so common that Python has a short way to write it. copies += 5 means the same as copies = copies + 5. There's also -=, *= and /=.

Step 2 code main.py
Edit the code, then run it again

Watch it run, and see the value of copies change in the table on lines 3 and 5.

Names point at values

Here's a puzzle. Line 2 says favourite = first. Then line 3 changes first. Does favourite change too? Guess, then run it.

Step 3 code main.py
Edit the code, then run it again

It doesn't. A Python name is like a label tied to a value, not a box with the value inside. Line 2 ties the label favourite to the same value first points at: "Dune". Line 3 moves the label first to a new value, "Emma", and leaves favourite where it was.

This idea, names pointing at values, matters more and more as the course goes on. In Stage 3 you'll see what happens when two names point at a value that can change.

What does this program print?
a = 5
b = a
a = 9
print(b)

Any value, any time

Every value has a type, its kind: whole numbers are int, text is str (short for string, as in a string of characters). type() tells you a value's type.

In Python the value has a type, but the name doesn't. The same name can point at a number on one line and at text on the next. Run it and compare the two lines.

Step 4 code main.py
Edit the code, then run it again

This is called dynamic typing. It makes small programs quick to write, but it also means Python only finds a mix-up, like adding text to a number, when that line runs. (Languages like Rust and Java fix each name's type when you write the program. If you've done Simple Rust, compare this with its lesson 1.3.)

A name with no value

Python runs from the top down, so a name exists only after the line that gives it a value has run. This program uses title on line 1, a line too early. Press Run and read the error from the bottom up.

Step 5 code main.py
Edit the code, then run it again

The last line is NameError: name 'title' is not defined. Python stopped at line 1, so line 2 never ran. To fix it, swap the two lines, so the value is given before it's used. Run it again to check.

A misspelt name gives the same error. Try changing {title} to {titel} in the fixed version: Python 3.14 even suggests the name you probably meant.

Choosing good names

Python's rules for names are short:

  • Letters, digits and _ only, and no spaces.
  • It can't start with a digit: book2 is fine, 2books isn't.
  • Capitals count: Title and title are different names.
  • A few words are taken by Python itself, like if, for and def. Those can't be names.

Beyond the rules, Python programmers have a habit, written down in a guide called PEP 8: names are lower case, with _ between words, like days_late or books_out. This is called snake_case. A name in capitals, like MAX_FINE, means a value that's meant to stay the same. You'll use one in the Stage 1 project.

Good names say what the value is. days_late tells the next reader far more than d or x.

Check what you learned

Four questions. Pick or type an answer, then check it.

After copies = 4 and then copies -= 1, what is copies?
Which of these is a name Python allows, written the usual Python way?
shelf = 12 then shelf = "12B". What happens?
This program stops with an error. On which line?
books = 3
print(f"{book} books")

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python