syntax - Python for-else relationship -


this question has answer here:

i can run below python script without errors.

for n in range(3):     print n else:     print "done" 

but puzzled else without matching if.
not make sense.
can 1 explain why works ?

the else clause of for , while only executes if loop exits normally, i.e. break never run.

for in range(20):   print   if == 3:     break else:   print 'haha!' 

and else clause of try executes if no exception happened.

try:   = 1 / 2 except zerodivisionerror:   do_something() else:   print '/golfclap' 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -