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

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -