Lesson 16 min read

Variables & Data Types

Give your data a name and a personality

What Are Variables?

Think of a variable like a labeled jar on a shelf. The label is the variable's name, and whatever you put inside is its value. You can swap out the contents anytime — put in a number today, a word tomorrow.

In Python, you create a variable just by picking a name and using the = sign. No special keywords needed — Python figures out the type for you.

There are a few rules for naming your jars:

  • Names can use letters, numbers, and underscores — but can't start with a number.
  • Names are case-sensitive: Score and score are different jars.
  • You can't use Python's reserved words like if, for, or class.
  • By convention, use snake_case for variable names: player_name, not playerName.

Creating Variables

player_name = "Mario"
lives = 3
health = 97.5
is_alive = True
print(player_name)
print(lives)
print(health)
print(is_alive)
Output
Mario
3
97.5
True

The Four Core Data Types

Every value in Python has a type — it tells Python what kind of thing it is and what you can do with it.

  • int — Whole numbers like 42, -7, 0. Use these for counting things.
  • float — Decimal numbers like 3.14, -0.5. Use these for measurements, money, percentages.
  • str — Text wrapped in quotes: "hello" or 'hello'. Even "123" is a string if it's in quotes!
  • bool — Only two values: True or False. Perfect for yes/no decisions.

There's also a special value called None — it means "nothing here yet." It's like an empty jar with a label on it.

Checking Types with type()

age = 10
temperature = 98.6
favorite_color = "blue"
is_hungry = True
mystery = None
print(type(age)) # <class 'int'>
print(type(temperature)) # <class 'float'>
print(type(favorite_color)) # <class 'str'>
print(type(is_hungry)) # <class 'bool'>
print(type(mystery)) # <class 'NoneType'>
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>

Type Conversion (Casting)

# Converting between types
num_str = "42"
num_int = int(num_str) # str → int
num_float = float(num_str) # str → float
print(num_int + 8) # Now we can do math!
print(num_float)
# Be careful — not everything converts!
price = 9.99
print(int(price)) # Chops off the decimal (doesn't round)
count = 7
print(str(count) + " apples") # int → str so we can glue text together
Output
50
42.0
9
7 apples
Note: Variables in Python are like sticky notes, not boxes. When you write b = a, you're sticking a second label on the same value — not copying it into a new box. This matters a lot later when you work with lists and objects!

Multiple Assignment & Swapping

Python has a neat trick: you can assign several variables in one line. You can also swap two variables without needing a temporary one — something most other languages can't do this cleanly.

Multi-Assignment & Swapping

# Assign multiple variables at once
x, y, z = 1, 2, 3
print(x, y, z)
# Swap two variables — no temp needed!
a = "left"
b = "right"
a, b = b, a
print(a, b)
# Same value for multiple variables
red = green = blue = 0
print(red, green, blue)
Output
1 2 3
right left
0 0 0

Quick check

What is the type of the value "100"?
Operators & Expressions