Module 1 · Lesson 2 of 5

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.

✓ Create variables and assign values
✓ Follow Python naming rules
✓ Reassign variables to new values
✓ Swap values between two variables

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.

variables.py
# 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
💡 Key Concept

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.

naming.py
# ✅ 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

Easy

Create variables to describe a game character. The print statements at the bottom will display your character's stats.

Instructions: Create three variables:
• character_name — a string (any name you like)
• level — an integer (any whole number)
• health — a float (a number with a decimal)
Assign each variable on its own line above the print statements. Remember: strings need quotes ("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.

reassign.py
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
💡 Key Concept — Swapping Values

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:

swap.py
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!
⚠️ Common Mistake

Without the temp variable, the first assignment destroys the original value. Always save before overwriting!

🔄

Challenge 2: Swap Meet

Easy

Two players are on the wrong teams! Swap their values so team_a holds "Zara" and team_b holds "Marcus".

Instructions: Use a temporary variable to swap the values of team_a and team_b. Don't just reassign them directly — use the 3-step swap pattern from the lesson.
Follow the 3-step pattern: temp = team_a, then team_a = team_b, then team_b = temp. The order matters!
📝

Challenge 3: Mini Mad Libs

Medium

Fill in the variables to complete a short story. The story template at the bottom will use your variables to build the tale.

Instructions: Create these four variables with values of your choice:
• 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)
Each variable goes on its own line. Strings need quotes: 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

Stretch

Time to test your knowledge of Python's naming rules! For each variable name below, decide whether it's valid or invalid.

Instructions: Set each answer to the string "valid" or "invalid" based on whether the name would work as a Python variable.

Think carefully — some of these are tricky!
Remember the rules: must start with a letter or underscore, can only contain letters/numbers/underscores, and can't be a Python keyword. 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!