For Else Construct
Multiple Choice Medium
Question
When does the else clause of a for loop run?
Hint
for-else is unique to Python.
Explanation
The else clause of a for loop runs when the loop completes normally (without hitting break). If break is encountered, else is skipped. This is useful for search loops: for item in items: if match(item): break; else: print("not found"). The else runs only if no match was found. This construct is unique to Python.
Related Resources
Related Lessons
- Conditions & Loops
Learn conditions and loops.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.