The end of the loop might not be the end
The problem...
Your loop searched through every item. Didn't find what it was looking for. Now what?
You need to know if the loop completed without interruption — or if something stopped it early.
Right now you have no clean way to tell the difference.
The idea!
A for loop can have an else block. It runs when the loop finishes normally — all the way through, without being interrupted by break.
for item in sequence:
# loop body
else:
# runs when the loop completes without break
Your first for...else
word = "Bull"
for letter in word:
print(letter)
else:
print("Loop complete.")
# B
# u
# l
# l
# Loop complete.
No break — loop finishes normally — else runs.
When else does NOT run
word = "Bull"
for letter in word:
if letter == "u":
break
print(letter)
else:
print("Loop complete.") # never runs
# B
break interrupted the loop — else is skipped entirely.
A real use case — search pattern
word = input("Enter a word: ")
target = input("Enter a letter to find: ")
for letter in word:
if letter == target:
print(f"Found '{target}'.")
break
else:
print(f"'{target}' not found.")
If the target is found — break fires — else is skipped. If the loop finishes without finding it — else runs. No extra flag variable. No extra if after the loop. Clean.
Empty sequence
word = ""
for letter in word:
print(letter)
else:
print("Loop complete.") # still runs
If the sequence is empty — the loop body never runs — but else still runs. No iterations means no interruption.
Heads up!
elseruns when the loop completes withoutbreak- If
breakfires —elseis skipped - If the sequence is empty —
elsestill runs - It's not "else if nothing matched" — it's "else if not interrupted"
The mindset shift
Stop thinking: "else means nothing matched."
Start thinking: "else means the loop ran to completion — without interruption."
What you should understand now
for...else—elseruns when the loop completes normally- If
breakinterrupts —elseis skipped - If the sequence is empty —
elsestill runs - Useful for search patterns — "did I find it or exhaust all options?"