Quest 4: Loops - The Power of Repetition
Master the Art of Repeating Actions in Python
๐ QUEST 4 | Difficulty: Beginner | Time: 5 minutes
๐ Complexity Level: Beginner โญ
Perfect for students with no programming experience. This lesson introduces fundamental concepts that are essential for all programming. Great for college fair visitors and first-time coders!
๐ป Interactive Options:
- ๐ Open in JupyterLite - Full Jupyter environment in your browser
- โถ๏ธ Run code directly below - All code cells on this page are editable and runnable
- ๐ฅ Download Notebook - For use in local Jupyter or Google Colab
๐ Introduction: The Repetitive Task
Imagine you need to knock on 100 doors to find the magic door. Would you write:
knock_on_door_1()
knock_on_door_2()
knock_on_door_3()
# ... 97 more lines ... ๐ฑ
knock_on_door_100()Thatโs ridiculous! Thereโs a better way: loops! Loops let you repeat actions without writing the same code over and over.
๐ช Story Time: Youโre searching for a magic portal in a hallway with 100 doors. Instead of checking each door one by one in your code, you can tell your program: โCheck all doors until you find the magic one!โ Thatโs the power of loops!
๐ก Explanation: Two Types of Loops
Python has two main types of loops:
๐ For Loops
Use when you know how many times to repeat (or when iterating through a collection).
# Repeat exactly 5 times
for i in range(5):
print(f"This is repetition {i}")๐ While Loops
Use when you repeat until a condition becomes False.
# Repeat while condition is True
count = 0
while count < 5:
print(f"Count is {count}")
count += 1๐ฏ The range() Function:
range() generates a sequence of numbers:
range(5) # 0, 1, 2, 3, 4 (5 numbers starting from 0)
range(1, 6) # 1, 2, 3, 4, 5 (from 1 to 5)
range(0, 10, 2) # 0, 2, 4, 6, 8 (from 0 to 8, step by 2)Remember: range(5) starts at 0 and stops BEFORE 5!
๐ฎ Activity: Countdown to Launch!
Letโs create a rocket launch countdown:
๐ฏ Challenge: Modify the code to:
- Make the countdown start from 5 instead of 10
- Add two more treasures to the list
- Make the countdown say โGet ready!โ when it reaches 3
๐จโ๐ป Code Example: While Loop Adventure
While loops are great when you donโt know exactly how many times to repeat:
๐ก Avoid Infinite Loops!
Be careful with while loops. If the condition never becomes False, your loop runs forever!
Bad example (donโt run this!):
# while True: # Never ends!
# print("Forever...")Always make sure your loop has a way to stop!
๐งฉ Puzzle Time!
What will this code print? Predict it, then run:
๐ Solution Explained:
The output is:
Final total: 15
Final count: 6
Letโs trace through each iteration:
| Iteration | count | total before | total after |
|---|---|---|---|
| Start | 1 | 0 | 0 |
| 1st loop | 1 | 0 | 0+1 = 1 |
| 2nd loop | 2 | 1 | 1+2 = 3 |
| 3rd loop | 3 | 3 | 3+3 = 6 |
| 4th loop | 4 | 6 | 6+4 = 10 |
| 5th loop | 5 | 10 | 10+5 = 15 |
| Loop ends | 6 | 15 | 15 |
Why count is 6: After the 5th iteration, count becomes 6 (count = 5 + 1), which makes the condition count <= 5 False, so the loop stops.
Pattern recognized: The code calculates 1+2+3+4+5 = 15!
๐ฎ Bonus: Nested Loops
You can put loops inside loops! This is great for grids and patterns:
๐ฏ Key Takeaways
โจ Quest 4 Complete! โจ
Youโve learned:
โ
for loops repeat a specific number of times
โ
while loops repeat while a condition is True
โ
range() generates sequences of numbers
โ
Loops can iterate through lists and other collections
โ
Avoid infinite loops by ensuring your condition can become False
โ
Nested loops create patterns and grids
Next Quest: Ready to organize data? Try Quest 5: Lists!
๐ Try This at Home!
Create a simple times table generator:
# Times table for any number
number = 7 # Change this to any number
print(f"Times table for {number}:")
for i in range(1, 11):
result = number * i
print(f"{number} ร {i} = {result}")Or make a guessing game hint system:
๐ฏ Try This: Change the numbers in the guesses_to_try list to see different hints!
๐ฑ Awesome Work! Loops are one of the most powerful tools in programming. Youโre doing great! ๐