For-else loop in Python 🐍

Python Learning
Mar 17, 2021

In Python, there is an else conditional statement with for/while loop unlike any other programming language, which denotes or executes when for loop is fully completed.

But else statement doesn’t execute in the case for loop terminated in between.

When for loop executes:

In the below case, for loop is executed without break in the loop so the “else” statement also gets executed.

Scenario-1: When for loop executes

When for loop terminated in between

else statement never executes in case if there is the termination of for-loop

Scenario-2: When for loop terminated

Conclusion- else statement is executed only when for loop does not terminate.

--

--

Python Learning

Bloggers, Spreading the love for Python Community, 1M+ Followers on Instagram