Loops

Why loops exist and which kind to reach for

Beginner 8 min

In this lesson

A loop runs a block of code repeatedly so you don’t have to copy and paste it — computers shine at doing the same thing many times without getting bored. This lesson is the map; the next two lessons dig into each loop type.

Explain it like I’m 5

A loop is ‘do this again’. Instead of writing the same instruction fifty times, you write it once and tell Python how many times — or how long — to keep doing it.

The two kinds of loop

Python has two loops. A for loop repeats once for each item in a collection — ‘for each name in this list, greet them’. A while loop repeats as long as a condition stays true — ‘keep asking until they type a valid answer’.

Rule of thumb: if you know what you’re looping over (a list, a range of numbers, the characters of a string), use for. If you’re looping until some condition changes and don’t know how many rounds that takes, use while.

Example
for number in [1, 2, 3]:
    print("Counting:", number)

countdown = 3
while countdown > 0:
    print(countdown)
    countdown = countdown - 1
print("Go!")
Output
Counting: 1
Counting: 2
Counting: 3
3
2
1
Go!
The same idea — repetition — expressed two ways.

Iteration, one step at a time

Iteration means going through items one at a time. Each pass through the loop body is one iteration. A loop variable usually holds the current item, and it’s updated automatically on each pass. Understanding ‘what does the loop variable hold right now?’ is the key to reading any loop.

Example
for letter in "cat":
    print("this pass:", letter)
Output
this pass: c
this pass: a
this pass: t
The loop variable holds a different value each pass.

Steering a loop: break and continue

Two keywords change the flow inside any loop. break stops the loop immediately and moves on to the code after it — useful when you’ve found what you were looking for. continue skips the rest of the current iteration and jumps to the next one — useful for ‘ignore this item and carry on’.

Example
for n in [1, 2, 3, 4, 5]:
    if n == 3:
        continue
    print(n)
Output
1
2
4
5
continue skips the rest of just this pass.

This loop prints the squares of 1–5. Add a break so it stops once the square passes 10.

for n in [1, 2, 3, 4, 5]:
    square = n * n
    print(n, "squared is", square)

The loop ‘else’ clause

A little-known feature: both for and while loops can have an else block. It runs only if the loop finished without hitting a break. That makes it a clean fit for search loops — break the moment you find what you want, and let the else handle the ‘never found it’ case.

It reads oddly at first (the else pairs with the loop, not an if), but it saves you from juggling a separate ‘found’ flag.

Example
names = ["Ada", "Bo", "Cy"]
target = "Di"

for name in names:
    if name == target:
        print("Found", target)
        break
else:
    print(target, "is not in the list")
Output
Di is not in the list
The else runs because the loop never broke.

Common mistake: Reaching for while when for is simpler

Why it happens:

Beginners often manually track a counter with while when they’re really just stepping through a known sequence.

How to fix it:

If you’re counting through items or numbers, use a for loop with range() — it’s shorter and can’t accidentally loop forever.

You want to do something once for each item in a list. Which loop fits best?

What does break do inside a loop?

Mini exercise (easy)

Using a loop, print the numbers 1 through 5, but skip 3. Decide whether for or while is cleaner.

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

kept = []
for n in range(1, 6):
    # TODO: use continue to skip 3, otherwise keep n
    kept.append(n)
print(kept)

What to learn next

You learned why loops exist, the difference between for and while, what iteration means, and how break and continue change a loop’s flow. You used continue to skip the number 3 while keeping the rest.

Now you’ll take each loop type in depth, starting with the one you’ll reach for most often. for Loops covers looping over lists and strings, counting with range(), and pairing each item with its position using enumerate(). When you’re ready, continue to the next lesson.