mailwal

Python From Scratch #29 - else With Loops

Shefali Jangid

Python From Scratch #29 - else With Loops

Learn how else works with for and while loops in Python.

Hey 👋

So far, we’ve learned:

  • for loops

  • while loops

  • break

  • continue

We’ve also used else with if statements.

But did you know that Python also allows you to use else with loops?

Let’s see how it works.

else With a for Loop

Here’s a simple example:

for number in range(5):
    print(number)
else:
    print("Loop finished")

# Output:

# 0
# 1
# 2
# 3
# 4
# Loop finished

The else block runs after the loop finishes normally.

In this example, the loop goes through all the numbers, finishes, and then the else block runs.

What Happens With break?

This is where loop else becomes more interesting.

Remember, break completely stops a loop.

If a loop is stopped using break, its else block does not run.

For example:

for number in range(1, 6):
    if number == 3:
        break

    print(number)
else:
    print("Loop finished")

# Output:

# 1
# 2

The else block doesn’t run because the loop was stopped by break.

The Easy Way to Remember It

Think of loop else like this:

  • The loop finished normally → else runs.

  • The loop stopped because of break → else doesn’t run.

This is different from the else used with if.

else With a while Loop

You can also use else with a while loop.

number = 1

while number <= 3:
    print(number)
    number += 1
else:
    print("Loop finished")

# Output:

# 1
# 2
# 3
# Loop finished

The condition eventually becomes False, so the loop ends normally and the else block runs.

while + break + else

Now let’s combine what we’ve learned.

number = 1

while number <= 5:
    if number == 3:
        break

    print(number)
    number += 1
else:
    print("Loop finished")

# Output:

# 1
# 2

The loop reaches 3 and break stops it.

Because the loop didn’t finish normally, the else block is skipped.

A Practical Example

Loop else can be useful when you’re searching for something.

For example:

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 7:
        print("Found it!")
        break
else:
    print("Number not found")

# Output:

# Number not found

The loop checks every number.

Since 7 isn’t found, the loop finishes normally, so the else block runs.

Now let’s search for 3:

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 3:
        print("Found it!")
        break
else:
    print("Number not found")

# Output:

# Found it!

This time, break runs when 3 is found.

Because the loop stopped with break, the else block doesn’t run.

continue Doesn’t Skip the else

There’s one more important thing to remember.

continue does not prevent the loop’s else block from running.

For example:

for number in range(5):
    if number == 2:
        continue

    print(number)
else:
    print("Loop finished")

# Output:

# 0
# 1
# 3
# 4
# Loop finished

The continue only skips the current iteration.

The loop still finishes normally, so else runs.

break vs continue vs else

Here’s a quick comparison:

Statement                What happens
break              Stops the loop completely
continue           Skips the current iteration
Loop else          Runs when the loop finishes normally

The important connection is:

Loop finishes normally → else runs
Loop stops with break   → else doesn't run
continue                → loop keeps going

Do You Need Loop else?

You don’t have to use loop else in every program.

It’s a Python feature that can be useful, especially when you’re searching for something and want to do something when the search finishes without finding a match.

If you’re just starting out, the most important thing to remember is: Loop else runs when the loop finishes without hitting break.

Try It Yourself

Now, try these:

  • Write a for loop from 1 to 5 and print "Done!" using else.

  • Modify the loop so it stops when the number reaches 3. Check whether the else block runs.

  • Write a loop from 1 to 5 and skip the number 3 using continue. Check whether the else block runs.

That’s all for today!

Tomorrow, we’ll put our loop concepts together with a small project.


If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️


Want to sponsor my work? Let’s talk!

See you tomorrow ✨

- Shefali

Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.

If you enjoyed this edition of DevBits, you can support my work here ☕

Buy Me a Coffee

Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.

© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104