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 »
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 »
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.