DAY 6

Loops πŸ”βš‘β™ΎοΈ

Loops let computers repeat tasks automatically. No more copy-pasting code. Write it once, run it 1000 times.

⏱ 30 mins
⚑ +50 XP
 Loops πŸ”βš‘β™ΎοΈ

Day 6: Loops β€” Make the Computer Do the Hard Work!

What's a Loop?

Imagine your teacher asks you to write "I will not talk in class" 100 times. Boring right? Now imagine a robot does it for you in 1 second. That robot is a loop β€” it repeats work automatically!

Your First Loop


for i in range(5):
    print("Hello!")

This prints "Hello!" five times. One line does the work of five lines. That's loops!

Using i as a Counter


for i in range(5):
    print("Round", i)

i starts at 0 and goes up by 1 each round. So you see Round 0, Round 1, Round 2, Round 3, Round 4. Computers always start counting from zero!

Real World Connection

When Instagram sends a notification to 500 million users, it doesn't write 500 million lines. It loops! When YouTube loads your feed with 20 videos, it loops through 20 items. When PUBG checks all 100 players every second, it loops through all 100!

Common Mistakes

Missing the colon.


for i in range(3)     # WRONG
for i in range(3):    # CORRECT

Forgetting indentation.


for i in range(3):
print(i)       # WRONG β€” not indented!

for i in range(3):
    print(i)   # CORRECT

Mini Challenge

Mini Challenge

Print a countdown from 5 to 1 then print "Blast off!". Hint: use range(5, 0, -1) to count backwards. You're basically building a rocket launch system!

Quick Quiz

Q: What does range(4) give you? A: 0, 1, 2, 3 β€” four numbers starting from zero!

Q: How many times does this loop run: for i in range(10)? A: 10 times!

Q: What must every line inside a loop have? A: Indentation β€” spaces at the start!

Key Takeaways

Key Takeaways

  • Loops repeat code automatically β€” write once, run many times.
  • for i in range(n) runs n times.
  • i starts at 0 and counts up β€” computers start from zero!
  • Always add : after the loop and indent everything inside.
  • Loops power everything β€” notifications, feeds, game logic!

← Previous Lesson