Use of else block with Python loops

An awesome feature that most programming languages do not offer

Sagun Raj Lage
The Startup
Published in
3 min readMay 16, 2020

--

“An else block can exist only when an if block exists before it.”

As a programmer not belonging to Python background, I was a strict follower of this rule. But I was left awestruck and confused after I saw one of my friends’ Python code that used an else block after a for loop. So, I performed some research and found that Python provides a feature, that allows us to use an else block with for and while loops. The else block after a for or…

--

--

Sagun Raj Lage
The Startup

Author of Getting Started with WidgetKit (2021) | Research Associate at UGA Savannah River Ecology Laboratory | iOS Engineer | Full Stack Engineer