Using ELSE with FOR loop in Python: A neat trick

🧩 Wait… you can use ELSE with a FOR loop in Python? Sounds weird, right? Because we all learned that else only works with if. But Python quietly lets you use else with loops too🫡 Sample code 👉 : numbers = [1, 3, 5, 7] for num in numbers:   if num == 4:     print("Found 4!")     break else:   print("4 not found in list") 💡 The else part runs only if the loop finishes normally — no break inside. 🔹 If we found 4 → break stops the loop → else is skipped 🔹 If 4 isn’t found → loop completes → else executes Neat trick, right? Came across this interesting feature while exploring Python basics — thought of sharing for those who didn’t know! #Python #CodingTips #Developers #LearnPython #Programming

To view or add a comment, sign in

Explore content categories