Skip to content

[Term Entry] Python Keyword: break #6962 #6994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 17, 2025
78 changes: 78 additions & 0 deletions content/python/concepts/keywords/terms/break/break.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
Title: 'break'
Description: 'Immediately exits the nearest enclosing `for` or `while` loop.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Break'
- 'For'
- 'Loops'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`break`** keyword in Python is used to exit a [loop](https://codecademy.com/resources/docs/python/loops) 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:

```pseudo
while condition:
if some_condition:
break # Exit the loop
```

Here is the syntax for using `break` in `for` loop:

```pseudo
for item in iterable:
if some_condition:
break # Exit the loop
```

**Parameters:**

- The `break` statement 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:

```py
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:

```shell
14
25
36
47
58
```

## 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`:

```codebyte/python
numbers = [14, 25, 36, 47, 58, 69, 70]
for number in numbers:
if number % 29 == 0:
break
print(number * 2)
```