a=[11,22,33,44,55]
for i in a:
print(i)
else:
print(99)
Result: 112233445599This is amazing Why does 99 appear How should I write for if else?
1.for ... else ... syntax (http://book.pythontips.com/en...)
for i in range(10): #如果没有break,会print所有i print(i) else: #如果上面的for循环没有break,这里会打印 print(99)
2.for if else should refer to the following:
for i in range(10): if i > 5: break else: print(i)
In addition to common conditional judgments in other languages, else in python can also be used in loops. If the break statement is not executed in the for loop, the branch statement under else will be executed
Because the loop is allowed to complete normally, the else branch is also executed
1.for ... else ... syntax (http://book.pythontips.com/en...)
2.for if else should refer to the following:
In addition to common conditional judgments in other languages, else in python can also be used in loops. If the break statement is not executed in the for loop, the branch statement under else will be executed
Because the loop is allowed to complete normally, the else branch is also executed