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 breakelse executes
  • If the loop is stopped using breakelse is skipped

Basic Syntax


Simple Example


for–else with break

  • else does not run because of break

Searching Example


for–else with String


Key Point

Note

else in a loop is linked to the loop, not to the if.


Exercise