Using else with Loops in Python


An else block after a loop is executed only if the loop completes normally (without hitting a break).

Source Code

for i in range(3):
    print(i)
else:
    print("Loop completed successfully!")
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments