For Loop with else
1 min read ·
In Python, a
for loop can have an else block.
The else block runs only when the loop completes normally.- If the loop finishes without
break→elseexecutes - If the loop is stopped using
break→elseis skipped
Basic Syntax
Simple Example
for–else with break
elsedoes not run because ofbreak
Searching Example
for–else with String
Key Point
Note
else in a loop is linked to the loop, not to the if.