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
NameErrorand 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.
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 /=.
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.
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.
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.
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.
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:
book2is fine,2booksisn't. - Capitals count:
Titleandtitleare different names. - A few words are taken by Python itself, like
if,foranddef. 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.
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.
- The Python Tutorial: First Steps Towards Programming: names and =
- The Python Language Reference: Assignment statements: what = does
- The Python Language Reference: Augmented assignment statements: += and -=
- Built-in Functions: type()
- Built-in Exceptions: NameError
- The Python Language Reference: Names (identifiers and keywords): which names are allowed
- PEP 8: Naming Conventions: snake_case
- The Rust Programming Language: Data Types: Rust fixes each value’s type before the program runs
- The Java Language Specification: Chapter 4, Types, Values, and Variables: Java is statically typed: every name has a type known when compiling