Python For Break
The break Statement
With the break statement we can stop the loop before it has looped through all the items:
Example
Exit the loop when x is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Try it Yourself »
for x in fruits:
print(x)
if x == "banana":
break
Example
Exit the loop when x is "banana",
but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Try it Yourself »
for x in fruits:
if x == "banana":
break
print(x)
Related Pages
Python For Loops Tutorial For Loop Through a String For Continue Looping Through a Range For Else Nested Loops For pass