Open In App

Python break statement

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Python break is used to terminate the execution of the loop. 

Python break statement Syntax:

Loop{
    Condition:
        break
    }

Python break statement

break statement in Python is used to bring the control out of the loop when some external condition is triggered. break statement is put inside the loop body (generally after if condition).  It terminates the current loop, i.e., the loop in which it appears, and resumes execution at the next statement immediately after the end of that loop. If the break statement is inside a nested loop, the break will terminate the innermost loop.

Break-statement-python 

Example of Python break statement

Example 1: 

Python3




for i in range(10):
    print(i)
    if i == 2:
        break


Output:

0
1
2

Example 2: 

Python3




# Python program to
# demonstrate break statement
  
s = 'geeksforgeeks'
# Using for loop
for letter in s:
  
    print(letter)
    # break the loop as soon it sees 'e'
    # or 's'
    if letter == 'e' or letter == 's':
        break
  
print("Out of for loop"    )
print()
  
i = 0
  
# Using while loop
while True:
    print(s[i])
  
    # break the loop as soon it sees 'e'
    # or 's'
    if s[i] == 'e' or s[i] == 's':
        break
    i += 1
  
print("Out of while loop ")


Output:

g
e
Out of for loop

g
e
Out of while loop

In the above example, both the loops are iterating the string ‘geeksforgeeks’ and as soon as they encounter the character ‘e’ or ‘s’, if the condition becomes true and the flow of execution is brought out of the loop.

Example 3:

Python3




num = 0
for i in range(10):
    num += 1
    if num == 8:
        break
    print("The num has value:", num)
print("Out of loop")


Output

The num has value: 1
The num has value: 2
The num has value: 3
The num has value: 4
The num has value: 5
The num has value: 6
The num has value: 7
Out of loop

In the above example, after iterating till num=7, the value of num will be 8 and the break is encountered so the flow of the execution is brought out of the loop.

Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore some statements of the loop before continuing further in the loop. These can be done by loop control statements called jump statements. Loop control or jump statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control/jump statements.



Similar Reads

Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements. Python While Loop Until a specified criterion is true, a block of statements will be continuously executed in a Python while loop. And the line in the program that follows the loop is run when the condition changes to false. Syntax of Python Whilewhile
4 min read
Break a list into chunks of size N in Python
In this article, we will cover how we split a list into evenly sized chunks in Python. Below are the methods that we will cover: Using yieldUsing for loop in PythonUsing List comprehensionUsing NumpyUsing itertoolMethod 1: Break a list into chunks of size N in Python using yield keyword The yield keyword enables a function to come back where it lef
5 min read
Break a long line into multiple lines in Python
Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Example: Breaking a long line of Python code into multiple lines Long Line: a = 1 + 2 + 3 + 4 - 5 * 2
4 min read
Python | Group elements on break positions in list
Many times we have problems involving and revolving around Python grouping. Sometimes, we might have a specific problem in which we require to split and group N element list on missing elements. Let's discuss a way in which this task can be performed. Method : Using itemgetter() + map() + lambda() + groupby() This task can be performed using the co
2 min read
break, continue and pass in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from their normal sequence. When execution leaves a sc
5 min read
Create a Python Script Notifying to take a break
We generally do not take breaks when we are using our laptop or PC. It might affect our eyesight as well as mind. So with Python, we can make a program that can notify us that we have to take a break start again after sometime when the user again starts working on the laptop. Modules neededpyttsx3 - It is a text-to-speech conversion library in Pyth
4 min read
Working with Page Break - Python .docx Module
Prerequisites: docx Word documents contain formatted text wrapped within three object levels. Lowest level- run objects, middle level- paragraph objects and highest level- document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. Python doc
2 min read
How to Break out of multiple loops in Python ?
In this article, we will see how to break out of multiple loops in Python. For example, we are given a list of lists arr and an integer x. The task is to iterate through each nested list in order and keep displaying the elements until an element equal to x is found. If such an element is found, an appropriate message is displayed and the code must
6 min read
Break a list comprehension Python
Python's list comprehensions offer a concise and readable way to create lists. While list comprehensions are powerful and expressive, there might be scenarios where you want to include a break statement, similar to how it's used in loops. In this article, we will explore five different methods to incorporate the 'break' statement in Python list com
2 min read
How To Break Up A Comma Separated String In Pandas Column
Pandas library is a Python library which is used to perform data manipulation and analysis. It offers various 2D data structures and methods to work with tables. Some times, the entire data can be in the format of string, which needed to be broken down in-order to organize the information in the pandas data structures. In this article, let us under
3 min read
Article Tags :
Practice Tags :