for i in range(3):
print("Loop", i)
else:
print("Else block!")Loop 0
Loop 1
Loop 2
Else block!
else Blocks After for and while Loopsfor and while loops support an optional else block
else in an if/elif/else construct only runs if neither preceding block doeselse in a try...except...else only runs if no exception is thrownfinally in a try...except...finally is clear in that it always runs after the block regardlessbreak out of a loopelse runs immediately if looping over empty sequenceelse also runs when loop condition is initially Falseelse blocks are designed for when you’re searching for something
break if you find the objectelse informs the user that the result is coprimeTesting 2
Testing 3
Testing 4
Coprime
else blocks after while and for loopsbreak