Lesson 2

Variables & Data Types

Variables let you label a value and reuse it. Types tell Python what kind of value it is — text, a number, a true/false, and so on.

Creating a variable

Use = to assign a value to a name:

name = "Sam"
age = 34
height_m = 1.78
is_member = True

print(name, age, height_m, is_member)
Sam 34 1.78 True

Notes on naming:

The four core types

You'll spend most of your time with these:

Strings — text

greeting = "Hello"
name = 'Pat'     # single or double quotes both work
note = "It's fine"  # use double quotes if your text has an apostrophe

Integers — whole numbers

year = 2026
score = -5

Floats — decimal numbers

price = 9.99
pi = 3.14159

Booleans — True or False

is_open = True
is_admin = False
Watch the capitals It's True and False with a capital letter. true and false will give you a NameError.

None — the "no value" value

Sometimes you want to say "this variable exists, but it doesn't have a value yet":

winner = None

Checking a type

Use the built-in type() function:

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

Converting between types

Wrap a value in the type's name to convert it:

age_text = "34"          # this is a string
age = int(age_text)      # now it's an integer (34)

price = float("9.99")   # 9.99 as a float
label = str(2026)        # "2026" as a string

print(age + 1, label + "!")
35 2026!
Why this matters When you ask the user for input (next lesson), what you get back is always a string — even if they typed a number. You'll need int() or float() to do maths on it.