TIL that there is a else-clause in a Python for-loop

rndmh3ro - May 19 '23 - - Dev Community

In Python if you want to execute something after a for-loop has finished, you can use a else-clause.

This code:

for i in range(5):
    print(i)
else:
    print("Finished")

Enter fullscreen mode Exit fullscreen mode

Prints:

0
1
2
3
4
Finished

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player