What else is there in Python?

jieforest發表於2012-07-25

[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部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-736618/,如需轉載,請註明出處,否則將追究法律責任。

相關文章