[size=10.5pt]We all use the else keyword in Python,usually accompanying if statement:
CODE:
if x > 0:
print 'positive'
elif x < 0:
print 'negative'
else:
print 'zero'
[size=10.5pt]but Python has a few other uses to the else keyword that mostpeople are unfamiliar with.
for .. else
I bet you didn’t know that you can put an else clause after a for loop! What does it do? When the items you iterator over are exhausted, the else clause is executed. When aren’t the items exhausted? When you use a break statement.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/301743/viewspace-736618/
,如需转载,请注明出处,否则将追究法律责任。