- If you're talking about starting over from the beginning of the for loop, there's no way to do that except "manually", for example by wrapping it in a while loop:
should_restart = True
while should_restart:
should_restart = False
for i in range(10):
print i
if i == 5:
should_restart = True
break
This restarts the loop over and over - anytime it reaches 5, it started over - leads to infinite loop.
- How does a while loop actually stop?
A while loop doesn't automatically exit in mid-loop as soon as its condition is no longer true; it just checks the condition at the start of each loop. If you want to get out early, you need to break explicitly (or do something else non-local, like return from the function or raise to an except handler outside the loop).