Keywords and Control Flow
else — Alternative or completion clause
The meaning of else depends on its enclosing statement. In an if chain it handles unmatched conditions. In a for or while loop it runs on ordinary completion, even if the body never ran; break bypasses it. In try/except it runs after the try suite completes without an exception or an early return, break or continue. Exceptions from the try else suite are not handled by that same try’s preceding except clauses. A conditional expression also uses else to separate its alternative expression.
Formal Syntax & Structure
python syntaxpython
if condition: suiteelse: suite
for target in iterable: suiteelse: suite
while condition: suiteelse: suite
try: suiteexcept Exception: handlerelse: suite
value_if_true if condition else value_if_falseWorking Example
Example Snippetpython
for item in [2, 4, 6]: if item == 5: print("found") breakelse: print("not found")