The for ... else ... statement

Using the else clause after the for loop only allows us to execute a block of code if the loop ended naturally, without terminating with the break statement:

>>> for number in range(1):
...     break
... else:
...     print("no break")
...
>>> for number in range(1):
...     pass
... else:
...     print("no break")
...
no break

This comes in handy in some situations, because it helps in removing some sentinel variables that may be required if the user wants to store information if a break statement occurred. This makes the code cleaner, but can confuse programmers who are not familiar with such syntax. Some say that such meaning of the else clause is counterintuitive, but here is an easy tip that will help you remember how does it work – memorizing that else clause after the for loop simply means no break.