Why does python use 'else' after for and while loops?

前端 未结 21 2311
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 06:57

I understand how this construct works:

for i in range(10):
    print(i)

    if i == 9:
        print(\"Too big - I\'m         


        
21条回答
  •  后悔当初
    2020-11-21 07:45

    I think documentation has a great explanation of else, continue

    [...] it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement."

    Source: Python 2 docs: Tutorial on control flow

提交回复
热议问题