Python break
Published Jun 17, 2025
Contribute to Docs
The break keyword in Python is used to exit a loop immediately. It’s commonly used to stop looping early based on a condition or to exit infinite loops.
Syntax
Here is the syntax for using break in while loop:
while condition:
if some_condition:
break # Exit the loop
Here is the syntax for using break in for loop:
for item in iterable:
if some_condition:
break # Exit the loop
Parameters:
- The
breakstatement does not take any parameters.
Return value:
The break statement does not return any value. It simply exits the nearest enclosing loop immediately.
Example: Exiting a Loop Early Using break
This example iterates through a list of numbers and stops printing when the number 58 is reached, demonstrating how break exits the loop immediately:
numbers = [14, 25, 36, 47, 58, 69, 70]for number in numbers:print(number)if number == 58:break
The output of this code will be:
1425364758
Codebyte Example: Using break to Stop Loop on a Condition
This codebyte example loops through numbers and prints their doubles until it encounters a number divisible by 29, where it exits the loop using break:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours