Fundamentals · Lesson 4 of 94

Variables, values, and types

Watch this lesson

View full playlist ↗

Read the lesson

What you'll learn

Concept

A variable is a name attached to a value. You create one with =: the name goes on the left, the value on the right. From then on, using the name means using the value. There is no separate step where you announce a variable exists; the first assignment creates it.

Every value has a type, and the type decides what you can do with it. Text is a str, whole numbers are int, numbers with a decimal point are float, and True or False are bool. Python works out the type from the value you wrote, so you never declare it, but it is always there and it always matters.

The mental model that saves beginners the most confusion is this: a variable is a label, not a box. When you write age = 29, Python creates the number 29 somewhere in memory and sticks the label age on it. When you later write age = 30, the label moves to a new number. Nothing inside the old value changes.

Variables, values, and types: age → previous
Variables, values, and types · Figure 1 · Open for full size

Because the type belongs to the value and not to the name, the same name can hold an int now and a str later. Python allows it, and it is almost always a bad idea, because anyone reading the code has to track what the name currently means. Keep each name to one kind of value for its whole life; when the data really changes kind, give it a new name.

Names follow a few rules. They can contain letters, digits, and underscores, they cannot start with a digit, and they are case-sensitive, so total and Total are different names. The convention in Python is lowercase words joined with underscores: first_name, items_sold. Good names describe what the value is for, not what type it is.

Code

Start by creating variables of each basic type and printing them. Notice that the strings need quotes and the numbers do not, and that True and False are capitalized.

# basics.py
name = "Mahmud"
age = 29
height_m = 1.75
is_student = False

print(name)
print(age)
print(height_m)
print(is_student)
Output
Mahmud
29
1.75
False

The type() function tells you what kind of value a name currently holds. Run it on each variable and Python answers with the class of the value.

# types.py
name = "Mahmud"
age = 29
height_m = 1.75
is_student = False

print(type(name))
print(type(age))
print(type(height_m))
print(type(is_student))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

Reassigning a name moves the label. Here age points at 29, then at 30, and a second name previous keeps pointing at the old value, which proves the 29 itself never changed.

# reassign.py
age = 29
previous = age
age = 30

print(previous)
print(age)
Output
29
30

Try it yourself

Create a file called about_me.py. Store your city as a str, the current year as an int, your height as a float, and whether you have programmed before as a bool. Print each one, then print its type. Check that the four types come out as you expect.

Common mistakes

What's next

You can now hold values under names. Next you will do arithmetic with the numeric ones and meet the places where integers and floats behave differently from what you expect.

Course overview