🔁 Why Repeat Ourselves?
Imagine you need to greet 5 friends online. Writing print("Hello!")
five times works, but what if it's 100 friends? That's tedious and error-prone!
Programming gives us loops to handle repetitive tasks efficiently. We tell the computer what to do and how many times to do it.
⚙️ The Robot Task Analogy
Think of a `for` loop like instructing a robot:
The Robot (Computer)
Ready to follow instructions.
The Instruction (`for` loop)
"Repeat the 'Package Item' task 5 times."
The Action (Code inside loop)
Robot packages 5 items, one after the other.
The `for` loop automates the repetition for us!
🐍 The `for` loop with `range()`
This is how we tell Python to repeat something a specific number of times.
for i in range(5):
# Code to repeat goes here
print(i) # Example: Print the current count
🚀 Let's See it in Action!
This code will demonstrate a simple loop.
🧠 Loops & Computational Thinking
Using loops strengthens these key skills:
Iteration
This is the core idea of loops – repeating a set of instructions multiple times. You define the process once and let the loop handle the repetition.
Pattern Recognition
Identifying tasks that need repeating is recognizing a pattern. Loops allow you to abstract that pattern into code, making it reusable and efficient.
🧠 Quick Check!
Module 12 Theory Complete!
Awesome! You've learned the basics of fixed repetition using `for` loops. Time to practice!
Head to the Practice Zone or try the Advanced Practice to master loops.