Unit 2 of 2

Variables and data types

4 min Updated Jun 2026

Before a program can do anything useful, it needs to remember things.

A phone app remembers your username. A game remembers your score. A weather app remembers the city you searched. All of that — every piece of information a program holds onto — lives in a variable.

So… what is a variable?

A variable is a name you give to a piece of information so you can use it later.

name = "Jordan"
age = 17

That’s it. name now holds the text "Jordan". age holds the number 17. Whenever you write name later in your code, Python knows what you mean.

You can think of a variable like a labelled box. The label is the name. The contents are the value. You can open the box, read what’s inside, or swap it for something new.

Naming variables

Python is flexible, but there are a few rules:

  • Names can contain letters, numbers, and underscores
  • Names cannot start with a number
  • Names are case-sensitive — score and Score are different variables

The following examples are valid:

player_name = "Alex"
level2 = 5
totalScore = 100

The following examples are not valid:

2fast = "too fast"   # starts with a number
my-name = "Jordan"   # hyphens aren't allowed

Convention in Python is to use lowercase with underscores: player_name.

Data types

Not all values are the same kind of thing. A name is text. An age is a whole number. A price might have decimal places. Python keeps track of these differences using data types.

The four you’ll use most often:

str — strings

Text. Anything wrapped in quotes is a string.

city = "Montreal"
message = "Hello, world!"
digit = "9"   # this is text, not a number

The quotes are what make it a string. 9 is a number. "9" is the character nine — you can’t do math with it.

int — integers

Whole numbers. No decimal point.

score = 0
lives = 3
year = 2026

float — floating point numbers

Numbers with a decimal point.

temperature = 36.6
price = 4.99
pi = 3.14159

bool — booleans

A value that is either True or False. Nothing else.

is_logged_in = True
has_premium = False

Booleans don’t look like much now, but they power every decision your code makes. You’ll use them constantly once you reach conditionals.

Checking the type

Not sure what type something is? Ask Python.

print(type("Montreal"))   # <class 'str'>
print(type(42))           # <class 'int'>
print(type(3.14))         # <class 'float'>
print(type(True))         # <class 'bool'>

This is useful when something behaves unexpectedly. If a number isn’t doing math correctly, it might secretly be a string.

Changing values

Variables aren’t permanent. You can update them any time.

score = 0
print(score)   # 0

score = 10
print(score)   # 10

score = score + 5
print(score)   # 15

That last line reads: take the current value of score, add 5, and store the result back into score. This kind of update — a variable referencing itself — comes up constantly.

Mixing types

Python won’t quietly combine different types for you. This breaks:

age = "17"
next_year = age + 1   # TypeError

age is a string. 1 is an integer. Python doesn’t assume you want to treat them the same way. You have to convert explicitly:

age = "17"
next_year = int(age) + 1   # works — converts "17" to 17 first
print(next_year)            # 18

The built-in conversion functions are int(), float(), str(), and bool(). You’ll reach for them often.

Putting it together

name = "Jordan"
age = 17
height = 1.75
is_student = True

print(name, "is", age, "years old.")
print("Height:", height, "m")
print("Student:", is_student)
Jordan is 17 years old.
Height: 1.75 m
Student: True

Four variables. Four types. One program that actually says something.

What just happened?

Variables are named containers for values. Every value has a type — str, int, float, or bool — and the type determines what you can do with it. Python won’t mix types silently, which means errors are usually caught early. When they happen, type() tells you what you’re actually working with.

In the next unit you’ll put variables to work with operators — arithmetic, comparison, and logic.