Variables & Assignment
Variables are the foundation of every program. They let you store data, give it a name, and use it whenever you need it — like labeled containers for your information.
Creating Variables
In Python, you create a variable by choosing a name, using the = sign (called the assignment operator), and providing a value. Think of it like putting a label on a box — the label is the name, the box holds the value.
# Creating variables is simple: player_name = "Kira" lives = 3 high_score = 9750.5 is_playing = True # Use them by their name: print(player_name) # → Kira print(lives) # → 3 print(high_score) # → 9750.5
The = sign in Python doesn't mean "equals" like in math. It means "assign the value on the right to the name on the left." So score = 100 means "store 100 in a container labeled score."
Naming Rules & Conventions
Python has strict rules about what you can name a variable, plus some conventions (best practices) that make your code easier to read.
The Rules (Python enforces these)
| Rule | Valid Example | Invalid Example |
|---|---|---|
| Must start with a letter or underscore | name _secret | 2fast |
| Can only contain letters, numbers, underscores | player_1 | my-score total$ |
| Case-sensitive | Score ≠ score | — |
| Can't be a Python keyword | my_class | class for if |
The Conventions (Experienced devs follow these)
Use snake_case for variable names — all lowercase, with underscores separating words. Choose names that describe what the variable holds. Future-you will thank present-you.
# ✅ Good — descriptive, snake_case student_name = "Alex" total_score = 95 is_enrolled = True # ❌ Works, but hard to read x = "Alex" ts = 95 TotalScore = 95 # This is CamelCase — reserved for classes
Challenge 1: Label It
Create variables to describe a game character. The print statements at the bottom will display your character's stats.
• character_name — a string (any name you like)
• level — an integer (any whole number)
• health — a float (a number with a decimal)
"Aria"), integers are whole numbers (5), and floats have a decimal point (92.5).
Reassignment
Variables aren't permanent — you can change what's inside them at any time by assigning a new value. The old value is simply replaced.
score = 10 print(score) # → 10 score = 25 print(score) # → 25 (the 10 is gone!) # You can even use the old value to compute the new one: score = score + 5 print(score) # → 30
What if you want to swap the values of two variables? You can't just do a = b then b = a — the original value of a would be lost! You need a temporary variable to hold one value while you swap:
a = "left" b = "right" # The classic 3-step swap: temp = a # 1. Save a's value in temp a = b # 2. Overwrite a with b's value b = temp # 3. Put the saved value into b print(a) # → right print(b) # → left ✓ Swapped!
Without the temp variable, the first assignment destroys the original value. Always save before overwriting!
Challenge 2: Swap Meet
Two players are on the wrong teams! Swap their values so team_a holds "Zara" and team_b holds "Marcus".
temp = team_a, then team_a = team_b, then team_b = temp. The order matters!
Challenge 3: Mini Mad Libs
Fill in the variables to complete a short story. The story template at the bottom will use your variables to build the tale.
• hero — a string (a character name)
• creature — a string (an animal or monster)
• weapon — a string (a tool or weapon)
• number — an integer (any whole number)
hero = "Luna". The integer does not: number = 10. Make sure you spell the variable names exactly as shown — hero, creature, weapon, number.
Challenge 4: Name Detective
Time to test your knowledge of Python's naming rules! For each variable name below, decide whether it's valid or invalid.
Think carefully — some of these are tricky!
CLASS is tricky — class (lowercase) is a keyword, but CLASS (uppercase) is technically a different name since Python is case-sensitive. And for is definitely a keyword!