Breaking and continuing
Two keywords control a loop mid-way:
break: leave the loop immediately.continue: skip to the next iteration.
for n in [1, 2, 3, 4, 5]:
if n == 3:
break # stop at 3
print(n) # 1, 2
for n in [1, 2, 3, 4]:
if n == 2:
continue # skip 2
print(n) # 1, 3, 4
Use them to skip work, but avoid nesting them too deeply.