3 Remembering Things: Variables
3.1 The Concept First
Programs need memory. Not computer memory chips, but the ability to remember information from one moment to the next. Without this ability, a program would be like having a conversation with someone who forgets everything you say the instant you say it.
In programming, we call these memories “variables” - not because they’re complicated, but because the information they hold can vary (change) over time.
3.2 Understanding Through Real Life
Your Brain Uses Variables Constantly
Think about ordering coffee: - You remember your name when the barista asks - You remember what size you want - You remember if you want milk or sugar - The barista remembers your order while making it - The register remembers the total price
Each piece of information is stored in a mental “variable” that holds it until it’s needed.
Labels on Boxes
The simplest mental model: Variables are like labeled boxes. - The label is the variable’s name - The contents are the value it stores - You can change what’s in the box - But the label stays the same
Real-World Variables
Your phone uses variables constantly: - battery_level = 87 - current_time = “2:34 PM” - wifi_network = “Home_WiFi” - screen_brightness = 75
These values change, but the labels remain consistent.
3.3 Discovering Variables with Your AI Partner
Let’s explore how programs remember things.
Exploration 1: The Need for Memory
Ask your AI:
Why do programs need to remember information? Give me 3 simple examples without code.
You’ll see examples like: - A game needs to remember your score - A calculator needs to remember numbers before adding them - A chat app needs to remember your username
Exploration 2: Finding Variables in Life
Try this prompt:
List 5 things a food delivery app needs to remember while you're ordering
Notice how each piece of information needs a name and a value?
Exploration 3: The Concept of Change
Ask:
Explain why they're called "variables" using a real-world analogy
This helps you understand that the key feature is the ability to vary (change).
3.4 From Concept to Code
Now let’s see how Python implements this universal concept of memory.
The Simplest Expression
Ask your AI:
Show me the simplest possible Python example of creating a variable and using it. No functions, no complexity.
You’ll get something like:
name = "Alice"
print("Hello, " + name)That’s it! The = sign means “remember this.”
Understanding the Pattern
Let’s break down what happens:
age = 25This says: “Create a box labeled ‘age’ and put the number 25 in it.”
3.5 Mental Model Building
Model 1: The Sticky Note System
┌─────────────┐
│ name: Alice │ <- Sticky note with label and value
└─────────────┘
┌─────────────┐
│ age: 25 │ <- Another sticky note
└─────────────┘
Model 2: The Storage Room
Storage Room of Your Program:
┌────────┐ ┌────────┐ ┌────────┐
│ name │ │ age │ │ score │
│"Alice" │ │ 25 │ │ 100 │
└────────┘ └────────┘ └────────┘
Model 3: The Substitution Game
When Python sees a variable name, it substitutes the value:
greeting = "Hello"
name = "Bob"
print(greeting + " " + name)
# Python substitutes: print("Hello" + " " + "Bob")3.6 Prompt Evolution Exercise
Let’s practice getting the right level of complexity from AI.
Round 1: Too Vague
explain variables
You might get computer science theory about memory allocation!
Round 2: Better Direction
explain variables in Python for beginners
Closer, but might still include types, scope, and advanced concepts.
Round 3: Learning-Focused
I'm learning to store information in Python programs. Show me the simplest way to remember a user's name.
Now we’re getting useful learning material!
Round 4: Building Understanding
Using that example, show me how the variable changes if the user enters a different name
This demonstrates the “variable” nature of variables.
3.7 Common AI Complications
When you ask AI about variables, it often gives you:
class UserData:
def __init__(self):
self.name = None
self.age = None
self.email = None
def set_name(self, name: str) -> None:
if isinstance(name, str) and len(name) > 0:
self.name = name
else:
raise ValueError("Invalid name")
def get_name(self) -> str:
return self.name if self.name else "Unknown"
# Usage
user = UserData()
user.set_name("Alice")
print(f"User name: {user.get_name()}")Classes! Type hints! Validation! Methods! This is AI showing off object-oriented programming, not teaching variables.
3.8 The Learning Approach
Build understanding step by step:
Level 1: Single Variable
# Store one thing
favorite_color = "blue"
print("Your favorite color is " + favorite_color)Level 2: Variables Can Change
# Variables can vary!
score = 0
print("Starting score:", score)
score = 10
print("Current score:", score)
score = 25
print("Final score:", score)Level 3: Variables in Action
# Using variables with input/process/output
name = input("What's your name? ") # INPUT & STORE
greeting = "Welcome, " + name + "!" # PROCESS using stored value
print(greeting) # OUTPUTLevel 4: Multiple Variables Working Together
# A simple calculator memory
first_number = input("First number: ")
second_number = input("Second number: ")
total = int(first_number) + int(second_number)
print("The sum is", total)Variables can be used in expressions just like values: - int(first_number) + int(second_number) uses both variables - gold = gold + 10 updates a variable using its current value - Variables make expressions dynamic - they can change!
Try asking AI: “Show me how the same expression gives different results with different variable values”
3.9 Exercises
Exercise 2.1: Concept Recognition
Identifying Variables in Real Programs
Look at this program and identify all the variables:
player_name = "Hero"
health = 100
gold = 50
print(player_name + " has " + str(health) + " health")
gold = gold + 10
print("After finding treasure: " + str(gold) + " gold")Check Your Answer
Variables in this program: - player_name stores “Hero” - health stores 100 - gold stores 50, then changes to 60
gold demonstrates the “variable” nature - its value varies!
Exercise 2.2: Prompt Engineering
Getting Clear Examples
Start with: “variable examples”
Evolve this prompt to get AI to show you: 1. A program that remembers someone’s favorite food 2. Uses the variable twice 3. Shows the variable changing 4. Keeps it super simple
Document your prompt evolution journey.
Successful Prompt Example
“Show me a simple Python program that: 1. Stores someone’s favorite food in a variable 2. Prints it 3. Changes it to something else4. Prints the new value Keep it as simple as possible - just 4-5 lines”
Exercise 2.3: Pattern Matching
Finding the Core Pattern
Ask AI for a “professional shopping cart program”. In the complex code: 1. Find all the variables 2. Identify which ones are essential 3. Rewrite it using only 3-4 variables
Guidance
Essential variables might be: - items (what’s in cart) - total (running price) - customer_name (who’s shopping)
Exercise 2.4: Build a Model
Create Your Own Understanding
Design three different ways to explain variables to someone: 1. Using a physical metaphor (not boxes) 2. Using a story 3. Using a diagram
Test your explanations on someone. Which worked best? Why?
Exercise 2.5: Architect First
Design Before Code
Design programs that use variables for:
- Pizza Order Tracker
- What to remember: size, toppings, price
- How they change: add toppings, calculate price
- Simple Score Keeper
- What to remember: player name, current score
- How they change: score increases, name stays same
- Temperature Monitor
- What to remember: current temp, highest temp, lowest temp
- How they change: update with new readings
Write your design first, then ask AI:
Implement this exact design in simple Python: [your design]
Design Template
Pizza Order Design: - Variables needed: pizza_size, toppings, total_price - Start: size=“medium”, toppings=“cheese”, price=10 - Process: Add a topping, increase price by 2 - End: Show final order and price3.10 AI Partnership Patterns
Pattern 1: Memory Metaphors
Ask AI for different metaphors: - “Explain variables using a filing cabinet metaphor” - “Explain variables using a parking lot metaphor” - “Explain variables using a recipe metaphor”
Pattern 2: Progressive Examples
Guide AI through complexity levels: 1. “Show a variable holding a number” 2. “Now show it changing” 3. “Now show two variables interacting” 4. “Now show variables in a real task”
Pattern 3: Debugging Understanding
When confused, ask: - “Why is it called a variable?” - “What happens to the old value when I assign a new one?” - “Draw a diagram of what happens when x = 5”
3.11 Common Misconceptions
“Variables are boxes that hold things”
Better Understanding: Variables are names that point to values. When you change a variable, you’re pointing the name at a new value.
“= means equals”
Reality: In Python, = means “assign” or “remember as” - x = 5 means “remember 5 as x” - Not “x equals 5” (that’s == for comparison)
“Variable names don’t matter”
Reality: Good names make code readable:
# Bad
x = "John"
y = 25
z = x + " is " + str(y)
# Good
name = "John"
age = 25
message = name + " is " + str(age)3.12 Real-World Connection
Every app uses variables:
Social Media: - current_user = “your_username” - post_count = 47 - is_online = True - last_seen = “2 minutes ago”
Music Player: - current_song = “Favorite Track” - volume_level = 70 - is_playing = True - playlist_position = 3
Banking App: - account_balance = 1234.56 - account_holder = “Your Name” - last_transaction = -50.00
Variables are how programs model the world.
3.13 Chapter Summary
You’ve learned: - Variables are how programs remember information - The name stays the same, but the value can change - Python uses = to create and update variables - Good variable names make code understandable - Every program uses variables to track state
3.14 Reflection Checklist
Before moving to Chapter 3, ensure you:
3.15 Your Learning Journal
For this chapter, record:
- Real-World Variables: List 10 “variables” in your daily life
- Metaphor Creation: What’s your favorite way to think about variables?
- AI Experiments: What happened when you asked for “simple” vs “complex” examples?
- Naming Practice: Create good names for variables that store:
- Someone’s hometown
- The current temperature
- Whether it’s raining
- The number of messages
Well-named variables make code self-documenting. Instead of remembering what x means, user_age tells you exactly what it stores. This is more important than any syntax rule.
3.16 Next Steps
In Chapter 3, we’ll explore how to get information from users with the input() function. You’ll see how variables become essential for remembering what users tell us, and how this completes the Input→Process→Output pattern with memory!
Remember: Variables aren’t about syntax. They’re about giving programs the ability to remember and track the changing state of the world.