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:

  1. Make the countdown start from 5 instead of 10
  2. Add two more treasures to the list
  3. 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! ๐ŸŒŸ