for else in python



"else" block will be executed after the termination of the while loop. If the while loop gets break else block will not be executed.








Example-1
for i in range(1,10):
    
    print(i)

else:
    
    print("For loop completed")


Output



1
2
3
4
5
6
7
8
9
For loop completed







Example-2
for i in range(1,10):
    
    if(i==5):
        break
    
    print(i)

else:
    
    print("For loop completed")


Output



1
2
3
4