A little-known Python feature โ a loop can have an else block, and it only skips when the loop was cut short by break.
Both for and while loops in Python can carry an optional else block. It's one of Python's more surprising features, because else here doesn't mean "if the loop's condition was false" the way you might expect from if/else โ it means "if the loop finished without hitting a break."
else block attached to a loop runs only if the loop completes normally โ i.e., it runs through all its iterations without a break.break, the else block is skipped entirely.for...else and while...else.while) or its sequence ran out (for for) still counts as "completing normally" โ the else still runs.continue does not affect whether else runs โ only break does.for item in sequence:
if condition:
break
else:
# runs only if the loop never hit break
print("Loop finished without breaking")
๐ก A useful way to remember it: "else runs unless you break."
for i in range(5):
print(i)
else:
print("Loop completed!")
Output:
0
1
2
3
4
Loop completed!
The loop ran through every value of i without ever hitting a break, so the else block executes right after.
for i in range(5):
if i == 3:
print("Found 3, stopping early")
break
print(i)
else:
print("Loop completed!")
Output:
0
1
2
Found 3, stopping early
As soon as i == 3, break fires and the loop exits immediately โ "Loop completed!" never prints, because the loop didn't finish on its own.
This is the classic real use case โ checking whether something was found during a search:
numbers = [4, 8, 15, 16, 23, 42]
target = 15
for n in numbers:
if n == target:
print(f"Found {target}!")
break
else:
print(f"{target} was not in the list.")
Output:
Found 15!
If target had been, say, 99 (not in the list), the loop would run to completion without ever hitting break, and the else block's "was not in the list" message would print instead.
count = 0
while count < 3:
print(count)
count += 1
else:
print("While loop finished normally")
Output:
0
1
2
While loop finished normally
Same rule applies to while: since the loop ended because its condition became False (not because of a break), the else block runs.
Without else, checking "did my loop find something or not" usually needs an extra flag variable:
# Without loop-else โ needs a manual flag
found = False
for n in numbers:
if n == target:
found = True
break
if not found:
print(f"{target} was not in the list.")
The for...else version in Example 3 achieves the same result without the extra found variable โ the loop's own completion state does the job.
else on a loop means "no break happened", not "the condition was false" in the everyday if/else sense.continue has no effect on the else block โ only break can skip it.while condition that's false from the start) still runs its else block, since it never hit a break.for...else and while...else both work the same way.else block runs โ loop completed without break.else block skipped โ loop was exited via break.A loop's else block runs only when the loop finishes without hitting a break โ think "else runs unless you break." It works on both for and while loops, and it's most commonly used for search patterns where you want to react differently depending on whether the item was found (break fired) or the whole sequence was searched with no match (else fired). continue doesn't affect it โ only break does.