🐍 Lesson 2: Variables & Data Types

Labeled boxes that remember things for you

← Back to Python Basics

📦 What is a variable?

A variable is a labeled box where you can store a piece of information and use it later. You create one with an = sign:

score = 100 name = "Maya"

Read this as: "Put 100 into a box labeled score." Now whenever you write score, Python remembers it means 100.

score = 100 print(score) # shows 100 print(name) # shows Maya

📌 Notice the difference

print("score") prints the word score. print(score) (no quotes) prints what's inside the box — 100.

🔁 Variables can change

The value in a box can be replaced any time. Python always remembers the most recent value.

lives = 3 print(lives) # 3 lives = lives - 1 print(lives) # 2

🧩 The main data types

Every value in Python has a type. The three you'll use most are:

1. String (text) — str

Words and letters, always inside quotes.

pet = "dog" greeting = "Good morning!"

2. Integer (whole number) — int

Counting numbers, no quotes, no decimal point.

age = 11 coins = -5

3. Float (decimal number) — float

Numbers with a decimal point.

price = 2.50 temperature = 98.6

Bonus: Boolean (true/false) — bool

Only two values: True or False (capital first letter!).

game_over = False is_winner = True

🔍 Checking a type

Ask Python what type something is with type():

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

🏷️ Naming rules

Good variable names

  • Use letters, numbers, and underscores: high_score, player2
  • Must start with a letter or underscore, not a number: 2cool
  • No spaces — use underscores instead: my name ❌ → my_name
  • Choose names that describe the value: x 😐 vs player_health 😍

✅ What you learned

  • A variable stores a value with name = value.
  • The big data types: str, int, float, bool.
  • Use type() to check a value's type.
  • Variables can be changed at any time.