6 Doing Things Over and Over: Loops
6.1 The Concept First
Imagine if you had to write a separate line of code for every item in a list, every user in a system, or every second in a countdown. Programs would be impossibly long and inflexible.
The power of repetition lets programs handle any amount of data with the same few lines of code. This is what transforms programs from rigid scripts into flexible tools.
6.2 Understanding Through Real Life
Repetition Is Everywhere
Think about repetitive tasks in your day: - Brushing teeth: Brush each tooth (repeat for all teeth) - Climbing stairs: Step up (repeat until you reach the top) - Reading: Read word (repeat until end of page) - Washing dishes: Clean dish (repeat until sink is empty)
Each follows a pattern: 1. Start with something to process 2. Do an action 3. Move to the next item 4. Stop when done
Natural Stopping Points
Every repetition needs to know when to stop: - Counting: Stop at a specific number - Lists: Stop when no items left - Conditions: Stop when something becomes true/false - User says: Stop when user wants
The Power of Patterns
Once you define a pattern, it works for any amount: - Recipe for 1 cookie → Recipe for 100 cookies - Greeting for 1 student → Greeting for whole class - Check 1 password → Check million passwords - Process 1 photo → Process entire album
6.3 Discovering Loops with Your AI Partner
Let’s explore how programs repeat intelligently.
Exploration 1: Finding Repetition
Ask your AI:
Give me 5 examples of repetitive tasks a music player app performs, without using code
You’ll see patterns like: - Play each song in playlist - Update progress bar every second - Check for next song continuously
Exploration 2: Different Types of Repetition
Try this prompt:
What's the difference between "repeat 10 times" vs "repeat while music playing" vs "repeat for each song"?
This reveals the three main types of loops: counting, conditional, and collection-based.
Exploration 3: The Magic of Loops
Ask:
Show how a loop can replace 100 lines of code with just 3 lines, using a simple example
This demonstrates the power of repetition patterns.
6.4 From Concept to Code
Let’s see how Python expresses repetition.
The Simplest Expression
Ask your AI:
Show me the absolute simplest Python loop that prints "Hello" 5 times. No functions, no complexity.
You’ll get something like:
for i in range(5):
print("Hello")That’s it! - for - the repetition keyword - i in range(5) - repeat 5 times - Indented lines - what to repeat
Understanding the Flow
Let’s trace through:
for number in range(3):
print(f"Count: {number}")
print("Done!")Output:
Count: 0
Count: 1
Count: 2
Done!
The loop runs the indented code once for each number.
6.5 Mental Model Building
Model 1: The Assembly Line
Items: [📦, 📦, 📦, 📦, 📦]
↓
For each box:
[Process] → ✅
↓
All done!
Model 2: The Track Runner
Start line → Lap 1 → Lap 2 → Lap 3 → Finish!
↺ ↺ ↺
(same track each time)
Model 3: The Playlist
Songs: [🎵, 🎵, 🎵, 🎵]
For each song:
▶️ Play
⏭️ Next
When no more songs: ⏹️ Stop
6.6 Prompt Evolution Exercise
Let’s practice getting loop examples from AI.
Round 1: Too Vague
show me loops
You’ll get while loops, for loops, nested loops, infinite loops - overwhelming!
Round 2: More Specific
show me Python for loops
Better, but might include complex iterations and list comprehensions.
Round 3: Learning-Focused
I'm learning repetition in programming. Show me a simple for loop that counts from 1 to 10.
Perfect for understanding!
Round 4: Concept Reinforcement
Now show the same counting without a loop, to see why loops are useful
This shows the power of loops vs manual repetition.
6.7 Common AI Complications
When you ask AI about loops, it often gives you:
def process_data_pipeline(data_sources, transformations, validators):
"""Complex data processing with multiple loop types"""
results = []
for source in data_sources:
try:
# Nested loop with enumeration
for idx, item in enumerate(source.fetch_items()):
# Validation loop
for validator in validators:
if not validator.validate(item):
logger.warning(f"Item {idx} failed validation")
continue
# Transformation pipeline
transformed = item
for transform in transformations:
transformed = transform.apply(transformed)
# While loop for retry logic
retry_count = 0
while retry_count < 3:
try:
results.append(transformed)
break
except Exception as e:
retry_count += 1
except Exception as e:
logger.error(f"Source processing failed: {e}")
# List comprehension alternative
return [r for r in results if r is not None]Nested loops! Enumerations! While loops! Exception handling! This is data pipeline architecture, not learning loops!
6.8 The Learning Approach
Build understanding step by step:
Level 1: Simple Counting
# Count to 5
for i in range(5):
print(i)
# Prints: 0, 1, 2, 3, 4Level 2: Counting with Purpose
# Countdown
for seconds in range(5, 0, -1):
print(f"{seconds} seconds left")
print("Blast off! 🚀")Level 3: Looping Through Collections
# Process each item
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(f"I like {fruit}")Level 4: Loops with Decisions
# Combining loops and if statements
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")The % operator (modulo) gives the remainder after division: - 5 % 2 = 1 (5 ÷ 2 = 2 remainder 1) - 4 % 2 = 0 (4 ÷ 2 = 2 remainder 0) - Even numbers: num % 2 == 0 (no remainder) - Every third: num % 3 == 0
Ask AI: “Show me creative uses of the modulo operator in loops”
Level 5: User-Controlled Loops
# Keep going until user stops
while True:
answer = input("Continue? (yes/no): ")
if answer == "no":
break
print("Okay, continuing...")
print("Thanks for playing!")6.9 Exercises
Exercise 5.1: Concept Recognition
Identifying Repetition Patterns
For each scenario, identify: 1. What repeats 2. How many times (or what condition) 3. When it stops
Scenario A: Watering plants in a garden Scenario B: Checking email for new messages Scenario C: Vending machine dispensing change
Check Your Analysis
Scenario A - Watering Plants: - Repeats: Water each plant - How many: For each plant in garden - Stops: When all plants watered
Scenario B - Checking Email: - Repeats: Check inbox - How many: While new messages exist - Stops: When no new messages
Scenario C - Dispensing Change: - Repeats: Give coin - How many: Until correct change given - Stops: When change equals zeroExercise 5.2: Prompt Engineering
Getting Clear Loop Examples
Start with: “multiplication table”
Evolve this prompt to get AI to show you: 1. A loop that prints 5 x 1 through 5 x 10 2. Uses a simple for loop 3. Shows the calculation clearly 4. No functions or complex formatting
Document your prompt evolution.
Effective Final Prompt
“Show me a simple Python for loop that prints the 5 times table from 5x1 to 5x10. Just use print statements, no functions or formatting.”Exercise 5.3: Pattern Matching
Finding Core Loop Patterns
Ask AI for a “professional inventory management system”. In the complex code: 1. Find all loops 2. Identify what each loop does 3. Rewrite the essential logic using simple loops
Core Patterns to Find
- Loop through all items
- Count total quantity
- Check each item’s stock level
- Update prices for each item
- Generate report for each category
Exercise 5.4: Build a Model
Visualizing Loop Flow
Create three different models showing how loops work: 1. A circular diagram showing repetition 2. An analogy using a non-computer activity 3. A before/after comparison (without loop vs with loop)
Test your models by explaining loops to someone.
Exercise 5.5: Architect First
Design Loop-Based Programs
Design these programs before coding:
- Class Greeting System
- Task: Greet each student by name
- Data: List of student names
- Pattern: For each name, print personalized greeting
- Exercise Counter
- Task: Count exercises (jumping jacks, etc.)
- Pattern: Count from 1 to target number
- Extra: Encourage at halfway point
- Password Attempt Limiter
- Task: Give user 3 chances for password
- Pattern: Keep asking while attempts left and not correct
- Stop: When correct or out of attempts
Write your design as: - What needs repeating - What controls the repetition - When to stop - What happens each time
Then ask AI: “Implement this exact loop design: [your design]”
Design Example
Class Greeting Design: - Store names: [“Alice”, “Bob”, “Charlie”] - For each name in the list: - Print “Good morning, [name]!” - After all names: Print “Welcome, everyone!”6.10 AI Partnership Patterns
Pattern 1: Loop Comparison
Ask AI to show different loop types: - “Show counting to 10 with for vs while” - “Show processing a list three different ways” - “Compare loop vs manual repetition”
Pattern 2: Incremental Complexity
Build up understanding: 1. “Show a loop that prints one word 5 times” 2. “Now make it print different numbers” 3. “Now make it process a list” 4. “Now add a condition inside”
Pattern 3: Real-World Mapping
Connect loops to life: - “Explain for loops using a cooking recipe” - “Show while loops using a game example” - “Compare nested loops to organizing drawers”
6.11 Common Misconceptions
“Loops are only for counting”
Reality: Loops process any collection or repeat any action:
# Not just counting - processing data
for word in sentence.split():
print(word.upper())“You need to know the count beforehand”
Reality: Loops can run until a condition is met:
while user_input != "quit":
user_input = input("Command: ")“Loop variables are just throwaway counters”
Reality: Loop variables can be meaningful:
for student in class_roster:
print(f"Assignment for {student}")
# 'student' has meaning, not just 'i' or 'x'6.12 Real-World Connection
Every app uses loops constantly:
Social Media Feed:
for post in recent_posts:
display_post(post)
check_for_likes(post)
load_comments(post)Music Player:
for song in playlist:
play_song(song)
update_progress_bar()
check_skip_button()Game Engine (60 times per second!):
while game_running:
check_input()
update_positions()
detect_collisions()
draw_screen()6.13 Chapter Summary
You’ve learned: - Loops let programs repeat actions efficiently - for loops work with collections and counts - while loops continue until conditions change - Loops eliminate repetitive code - Repetition patterns make programs flexible
6.14 Reflection Checklist
Before moving to Week 1 Project, ensure you:
6.15 Your Learning Journal
For this chapter, record:
- Repetition Mapping: List 10 repetitive tasks in your daily life
- Loop Visualization: Draw your mental model of how loops work
- Power of Loops: Write 10 print statements, then replace with 2-line loop
- Design Practice: How would loops improve your previous programs?
You now have all the fundamental building blocks: - Input/Output: Communicate with users - Variables: Remember information - Decisions: Respond intelligently - Loops: Handle any amount of data
With just these four concepts, you can build surprisingly powerful programs!
6.16 Next Steps
Congratulations! You’ve completed the fundamental concepts of programming. In the Week 1 Project, you’ll combine everything you’ve learned to build your first complete program: a Fortune Teller that uses input, variables, decisions, and loops to create an interactive experience.
Remember: Loops aren’t about memorizing for and while syntax. They’re about recognizing repetition patterns and making programs that can handle anything from 1 to 1 million items with the same elegant code!