Python tutorial on the break keyword, covering loop termination, nested loops, and practical usage examples.
last modified February 25, 2025
The break statement in Python terminates the nearest enclosing loop prematurely. This tutorial explains how to use break to exit loops, demonstrates nested loop scenarios, and provides practical examples of flow control.
When executed, break immediately stops loop iteration and transfers execution to the first statement after the loop. It works with both for and while loops, providing a way to exit early when specific conditions are met.
This example shows how break terminates a while loop when a value reaches 5.
simple_break.py
count = 0
while True: print(count) count += 1 if count == 5: break
print(“Loop exited”) # Output: 0 1 2 3 4 Loop exited
The loop runs indefinitely until count reaches 5. The break statement exits the loop immediately, skipping any remaining iterations.
This example demonstrates using break to exit a for loop early when a target value is found.
for_loop_break.py
fruits = [“apple”, “banana”, “cherry”, “date”]
for fruit in fruits: if fruit == “cherry”: break print(fruit)
The loop stops iterating when “cherry” is encountered. The break statement prevents “date” from being processed.
This example shows break exiting only the innermost loop in nested structures.
nested_break.py
for i in range(3): for j in range(5): print(f"i: {i}, j: {j}") if j == 2: break
The inner loop breaks when j reaches 2, but the outer loop continues executing. Each iteration of the outer loop triggers a new inner loop.
This example demonstrates how break affects the else clause in loops.
while_else_break.py
num = 7
while num > 0: print(num) num -= 1 if num == 3: break else: print(“Loop completed normally”)
print(“After loop”) # Output: 7 6 5 4 3 After loop
The else clause only executes if the loop completes without hitting a break. Here, the break skips the else block.
This example uses break to exit a search loop when a matching item is found.
search_break.py
temperatures = [22.5, 24.8, 19.3, 28.4, 31.0] threshold = 30.0
for temp in temperatures: if temp > threshold: print(f"Temperature alert: {temp}°C") break else: print(“All temperatures within safe range”)
The loop checks each temperature and triggers an alert upon finding the first value exceeding the threshold. Without exceeding values, the else clause executes.
This example shows how break safely exits an otherwise infinite loop.
infinite_break.py
import time
start = time.time()
while True: print(“Processing…”) time.sleep(1) if time.time() - start > 5: print(“Timeout reached”) break
The loop runs indefinitely until 5 seconds elapse. break provides controlled exit from what would otherwise be an infinite loop.
Clarity Over Cleverness: Use break only when it improves code readability
Avoid Deep Nesting: Excessive break statements can complicate logic flow
Combine with Flags: Use boolean flags for complex exit conditions in nested loops
Document Exit Points: Comment non-obvious break conditions for future maintainers
Python Break Statement Documentation
This tutorial has explored the Python break statement through practical examples demonstrating loop control and early termination scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.