chapter 2.1

Variables & Types

Storing data and understanding what kind it is

A variable is a name that points to a value. Think of it like a label you stick on a box — the label is the variable name, the box holds the data.
name = "Ada"
age = 28
height = 5.7
is_student = True

print(name)
print(age)
Python has four basic data types you'll use constantly:
info
str — text (strings): "hello", 'world'
int — whole numbers: 42, -7, 0
float — decimal numbers: 3.14, -0.5
bool — True or False (always capitalized)
You can check a value's type using type():
print(type("hello"))   # <class 'str'>
print(type(42))        # <class 'int'>
print(type(3.14))      # <class 'float'>
print(type(True))      # <class 'bool'>
Type conversion lets you switch between types. This matters because "5" (a string) and 5 (an integer) are completely different to Python:
x = "10"
y = int(x)       # Convert string to int
print(y + 5)     # 15

z = str(100)     # Convert int to string
print("Score: " + z)
warning
You can't add a string and a number directly — "age: " + 25 will crash. Either convert with str() or use an f-string (you'll learn those next lesson).
Variable names have a few rules:
- Must start with a letter or underscore
- Can contain letters, numbers, underscores
- Case-sensitive (name and Name are different)
- Use snake_case by convention: my_variable, not myVariable
your turn
challenge

Create four variables — one for each type (str, int, float, bool). Print each variable along with its type using `type()`.

loading editor...

output
$ Press RUN to execute your code
AI Tutor

Ask about Variables & Types

Hey! I'm your AI tutor.

Ask me anything about this lesson, or paste code you're stuck on.

AI tutor is a premium feature