The else
keyword in a for
loop specifies a block of code to be executed when the loop is finished:
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!")
Note: The else
block will NOT be executed if the loop is stopped by a break
statement.
Break the loop when x
is 3, and see what happens with the else
block:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4