Lab 3 of 10
Loops & Iteration
Making Python do the repetitive work for you
⏳ Loading Python... (first load takes ~10 seconds)
📖 Concept Recap
- for loop — iterates over a sequence (list, string, range)
- while loop — runs until a condition becomes False
- range(start, stop, step) — generates a sequence of numbers
- break — exits the loop immediately
- continue — skips to the next iteration
- enumerate() — gives you both the index and value:
for i, val in enumerate(list) - zip() — pairs two lists together:
for a, b in zip(list1, list2)
👀 Worked Example
Three different loop patterns with the same list:
fruits = ["apple", "banana", "cherry", "date"]
# Basic for loop
for fruit in fruits:
print(fruit)
print("---")
# enumerate — get index AND value
for i, fruit in enumerate(fruits):
print(f"{i+1}. {fruit}")
print("---")
# zip — pair two lists together
prices = [1.20, 0.50, 2.00, 3.50]
for fruit, price in zip(fruits, prices):
print(f"{fruit}: ${price}")
✏️ Guided
Exercise 1 — Multiplication Table Generator
Complete the multiplication table generator by filling in the blanks. The table should print lines like 7 × 1 = 7 through 7 × 12 = 84.
Output will appear here...
💡 Hint:
range(1, 13) gives 1 through 12. The result is number * i.
💪 Independent
Exercise 2 — Divisibility Finder
Write a loop that finds all numbers between 1 and 100 that are divisible by both 3 and 7. Print each one, and at the end print how many there are total.
Output will appear here...
💡 Hint: A number divisible by both 3 and 7 must be divisible by 21. Use
if n % 21 == 0 or if n % 3 == 0 and n % 7 == 0. Keep a counter variable.
🔥 Challenge
Exercise 3 — Number Guessing Game Simulation
Write a number guessing game simulation (no user input — use a preset list of guesses). Rules:
- Secret number is
42 - Guesses:
[10, 55, 30, 42, 67] - For each guess, print "Too low!", "Too high!", or "Correct! Found in X guesses!"
- Stop when the correct guess is found (use
break) - If not found, print "Not found in the list!"
Output will appear here...
💡 Hint: Use
enumerate(guesses, 1) to track the attempt number. Use break when you find the secret. Use a for...else clause to handle the not-found case.
🏆 Mini Project
Mini Project — Weekly Sales Summary
Analyze 8 weeks of sales data. Your loop should calculate and print:
- Total sales across all weeks
- The best week (highest sales) and which week number it was
- The worst week (lowest sales) and which week number it was
- The weekly average
- How many weeks beat the average (use a counter)
- A formatted week-by-week breakdown showing each week vs. average
Output will appear here...
💡 Hint: Calculate total and average first. Then loop through with
enumerate(weekly_sales, 1) to track week numbers. Use max()/min() or track them manually in the loop.✅ Lab 3 Complete!
You’ve practiced for loops, enumerate, zip, break, continue, and real data analysis with loops. Loops are the engine of data processing.
Continue to Lab 4: Functions →