Python for break Statement | Example code

  • by

Python For Break is used to stop the loop before it has looped through all the items. For that, you have to use the if statement to match the break loop condition.

Python for break example

Simple example code exit for a loop if the value is “apple“.

fruits = ["apple", "banana", "cherry"]

for x in fruits:
    print(x)
    if x == "apple":
        print("Break Loop")
        break

Output:

Python for break Statement

Another Example

for x in range(1, 10):

    print(x)
    if x == 4:
        break

Output:

1
2
3
4

Do comment if you have any doubts and suggestions on this Python break keyword.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.