Most people think else is only for if. But in Python, loops can have else too!
for i in range(5):
if i == 10:
print("Found it!")
break
else:
print("Not found 😅")
Output: Not found 😅
Why it’s powerful:
Runs only if the loop didn’t break
Perfect for search algorithms
Cleaner than flags or extra variables
Use it to write more Pythonic, elegant code your loops just got smarter.
Top comments (0)