How to Use Break in Python
What you’ll build or solve
You’ll stop a loop early using break in Python.
When this approach works best
break works well when you:
Learn Python on Mimo
- Search for a match and want to stop once you find it, like locating a user by ID.
- Read input until a
"quit"command appears, then stop the loop. - Retry something until it succeeds, then stop early instead of finishing all attempts.
Avoid break when a loop already has a clean condition that ends naturally. In that case, put the condition in the while or for header instead.
Prerequisites
- Python installed
- You know what a loop is (
fororwhile)
Step-by-step instructions
1) Stop a loop with break
Place break inside a loop. When Python reaches it, the loop ends immediately.
numbers= [3,7,10,2]
forninnumbers:
ifn>8:
break
print(n)
What to look for:
break exits the loop, then execution continues after the loop.
2) Use break in a while loop with an exit condition
This is common for input-driven loops where the exit condition happens inside the body.
CSS
whileTrue:
text=input("Type something (or quit): ").strip()
iftext.casefold()=="quit":
break
print("You typed:",text)
print("Loop ended")
What to look for:
Keep the exit check near the top of the loop so it is easy to spot.
3) Break out of nested loops
break only exits the nearest loop. If you have nested loops, decide how you want to exit.
grid= [
[1,2,3],
[4,5,6],
[7,8,9],
]
found=False
forrowingrid:
forvalueinrow:
ifvalue==5:
found=True
break
iffound:
break
print("Found 5:",found)
What to look for:
The found flag lets you stop the outer loop after the inner loop breaks.
Examples you can copy
Example 1: Stop searching once you find a match
users= [{"id":"u1"}, {"id":"u2"}, {"id":"u3"}]
target="u2"
found_user=None
foruserinusers:
ifuser["id"]==target:
found_user=user
break
print(found_user)
Example 2: Stop after the first valid input
CSS
whileTrue:
text=input("Enter a number: ").strip()
iftext.isdigit():
value=int(text)
break
print("Try again")
print("You entered:",value)
Example 3: Retry up to N times, stop early on success
max_attempts=3
attempts=0
success=False
whileattempts<max_attempts:
attempts+=1
code=input("Code: ").strip()
ifcode=="1234":
success=True
break
ifsuccess:
print("Accepted")
else:
print("Too many attempts")
Example 4: Exit nested loops when you find a value
grid= [[10,20], [30,40]]
target=30
found=False
forrowingrid:
forvalueinrow:
ifvalue==target:
found=True
break
iffound:
break
print(found)
Example 5: Stop reading lines on a sentinel value
lines= []
whileTrue:
line=input("Line (type END to stop): ").strip()
ifline=="END":
break
lines.append(line)
print(lines)
Common mistakes and how to fix them
Mistake 1: Using break and missing the item you actually wanted
What you might do
numbers= [1,2,3]
forninnumbers:
ifn==2:
break
print(n)
Why it breaks
break exits before you process the matching item, so you never reach the code that handles 2.
Fix
Process the item first, then break:
numbers= [1,2,3]
forninnumbers:
ifn==2:
print(n)
break
Mistake 2: Assuming break exits all nested loops
What you might do
LUA
grid= [[1,2], [3,4]]
forrowingrid:
forvalueinrow:
ifvalue==3:
break
print("row done")
Why it breaks
break exits only the inner loop, so the outer loop continues.
Fix
Use a flag, then break the outer loop too:
grid= [[1,2], [3,4]]
found=False
forrowingrid:
forvalueinrow:
ifvalue==3:
found=True
break
iffound:
break
Troubleshooting
If your loop never stops, confirm your break condition can become true. Print key values while debugging.
If break triggers too early, move the condition lower in the loop, or tighten the condition.
If you break the inner loop but the program keeps looping, add logic to stop the outer loop too.
If you use while True, keep the break condition obvious and close to the top.
If you use a flag, set it right before break so the intent is clear.
Quick recap
- Use
breakto stop a loop early. breakexits only the nearest loop.- In
while Trueloops, put the exit check near the top. - For nested loops, use a flag to stop the outer loop after breaking the inner loop.
- Use
continuewhen you want to skip an iteration, not stop the loop.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot