for else

for ... else statement
else is executed only for is finished all range.

Pro:
Flag can be omitted.

Eg1

for i in range(1, 10):
    print i
    if i == 11:
        break
else:
    print "do else"


out put

>>> ================================ RESTART ================================
>>> 
1
2
3
4
5
6
7
8
9
do else


Eg2

for i in range(1, 10):
    print i
    if i == 8:
        break
else:
    print "do else"

output2

>>> 
1
2
3
4
5
6
7
8
>>> 


http://psung.blogspot.com/2007/12/for-else-in-python.html

Remove all ads