Elena' s AI Blog

Looping the Loop: Doing Things Over and Over

05 Jan 2026 / 9 minutes to read

Elena Daehnhardt


Lesson 5 of 11 45%


TL;DR: A loop repeats code. `for i in range(5):` means 'Do this 5 times'. It saves you from typing the same thing over and over.

Only Boring People Get Bored

Loops repeat actions automatically

Imagine if your teacher asked you to write โ€œI will not talk in classโ€ 100 times on the blackboard. You would hate it. Your hand would hurt. You would make mistakes.

A computer? It would love it. It can do it in a blink of an eye. โšก

Human way (terrible!):
print("I will not talk")
print("I will not talk")
print("I will not talk")
...98 more times... ๐Ÿ˜ซ

Computer way (brilliant!):
for i in range(100):
    print("I will not talk")
Done in 0.001 seconds! ๐ŸŽ‰

The for Loop

In Python, when we want to repeat something, we use a loop.

for i in range(3):
    print("Python is fun!")

Output:

Python is fun!
Python is fun!
Python is fun!

Breaking it Down

  1. for: Start the loop.
  2. i: A variable that counts for us (0, 1, 2โ€ฆ).
  3. in range(3): How many times? 3 times.
  4. :: Donโ€™t forget the colon!
  5. Indentation: The lines pushed in are the ones that get repeated.

๐Ÿš€ Try This NOW!

What will this print?

for i in range(5):
    print("Hooray!")
Click to check! It will print "Hooray!" exactly 5 times!

Using the Counter

The variable i changes every time the loop runs. You can use it!

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

Output:

Countdown: 0
Countdown: 1
Countdown: 2
Countdown: 3
Countdown: 4

Wait, why does it start at 0? Computer scientists count from 0. Itโ€™s just a quirk we love. (0, 1, 2, 3, 4) โ€” that is still 5 numbers!

๐ŸŽฎ Real-World Connection

This is how TikTok shows you videos one after another! It has a list of videos and loops through them: for video in video_list: show the video! Every social media app uses loops to display content!

Making a Pattern

Loops are great for art.

Creating patterns with loops

text = "*"
for i in range(5):
    print(text)
    text = text + "*"

Output:

*
**
***
****
*****

Cool, right? We made a triangle!

Challenge: Make it Backwards!

Can you make the triangle upside down? (Start with 5 stars, end with 1)

Loops with Variables

You can do math inside a loop! Letโ€™s count how many steps we take.

steps = 0
for i in range(5):
    steps = steps + 1
    print("I have taken", steps, "steps")

Output:

I have taken 1 steps
I have taken 2 steps
I have taken 3 steps
I have taken 4 steps
I have taken 5 steps

Or we can add up numbers:

total = 0
for i in range(1, 6):  # Numbers 1 to 5
    total = total + i
    print("Adding", i, "Total is now:", total)
    
print("Final total:", total)

Output:

Adding 1 Total is now: 1
Adding 2 Total is now: 3
Adding 3 Total is now: 6
Adding 4 Total is now: 10
Adding 5 Total is now: 15
Final total: 15

Stopping Early (The Emergency Brake)

Sometimes you want to stop the loop before it finishes. We use the command break.

Imagine you are looking for a lost key. Once you find it, you stop looking!

for i in range(100):
    print("Looking in box", i)
    if i == 5:
        print("FOUND IT!")
        break

Output:

Looking in box 0
Looking in box 1
Looking in box 2
Looking in box 3
Looking in box 4
Looking in box 5
FOUND IT!

The computer will stop at box 5, even though the range was 100!

Try It Yourself

Challenge 1: Write a loop that prints โ€œHappy Birthday!โ€ 10 times.

Challenge 2: Write a loop that counts down from 10 to 1. (Hint: range(10, 0, -1) counts backwards!)

Challenge 3: Write a loop that adds up all numbers from 1 to 10. Print the final total.

Solution to Challenge 2:

for i in range(10, 0, -1):
    print(i)
print("Blast off! ๐Ÿš€")

Common Mistakes & Fixes

โŒ Wrong: for i in range(5)
              print("Hi")
Why it fails: Missing the : at the end!

โœ… Right: for i in range(5):
              print("Hi")
Always add the colon!

โŒ Wrong: for i in range(5):
         print("This")
Why it fails: Not indented!

โœ… Right: for i in range(5):
              print("This")
Indent the code you want to repeat!

โŒ Wrong: for i in range(0):
Why it fails: Range of 0 means zero times!

โœ… Right: for i in range(5):
The number should be how many times you want!

๐Ÿ” Bug Hunter Betty Says:

โ€œIf your loop runs forever and wonโ€™t stop, press Ctrl+C (or Command+C on Mac) to stop it! Then check: did you forget to use range()? Did you accidentally create an infinite loop?โ€

๐ŸŽ‰ Achievement Unlocked: Loop Master! Wizard Level: Sorcerer (5/10 complete)

๐Ÿ“ Todayโ€™s Spell Book (Quick Reference)

for i in range(5):      # Repeat 5 times (0,1,2,3,4)
    print("Hi")
    
for i in range(1, 11):  # Count from 1 to 10
    print(i)
    
for i in range(10, 0, -1):  # Count backwards from 10 to 1
    print(i)
    
# Breaking out early:
for i in range(100):
    if i == 10:
        break  # Stop the loop!

๐Ÿ’ก Parent/Teacher Tip

If your learner creates an infinite loop by accident (one that never stops), calmly show them how to stop it with Ctrl+C. Explain that even professional programmers do this sometimes! Then help them find why itโ€™s looping foreverโ€”usually itโ€™s a while True: without a break, or forgetting to change the counting variable.

In the next lesson, weโ€™ll learn how to handle errors like a pro!


This post is part of the Python Basics for Kids series, based on โ€œPython for Kids from 8 to 88โ€.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2026) 'Looping the Loop: Doing Things Over and Over', daehnhardt.com, 05 January 2026. Available at: https://daehnhardt.com/courses/book0/05-loops/
Course Library