Using "else" with a loop in Python PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Python 3.8—3.12
Tags

You probably think of else as something you can put after an if statement. And it is!

But in Python, you can also put an else block after a loop!

A for loop with else

Here's a for loop with an else statement attached to it:

...

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.