break in Python — Python Technical Interview Questions & Answers
break in Python
break in Python
- break leaves the nearest loop immediately. No more rounds. Search: for n in nums: if n == target: print("found"); break. After you find it, don’t keep scanning. Nested loops: break only exits the inner one — the outer for still continues.
- Trace: for i in range(1, 6): if i == 3: break; print(i) → 1 then 2. 3 is not printed. 4 and 5 never run. Say that output before you run.
- break in Python — output: 1 then 2. When i is 3, break — 4 and 5 never print.
Technical Interview Tip: Trace break with a tiny for-loop.
Code Examples for break in Python
Example
# break
for i in range(1, 6):
if i == 3:
break
print(i)
break in Python — output: 1 then 2. When i is 3, break — 4 and 5 never print.
How to Explain break in Python to an Interviewer
Explain break as if you are teaching a junior — definition, then one tiny script.
Exit nearest loop now. Early stop when found.
What does the example print, and why?
break in Python — output: 1 then 2
What mistake do freshers make with break?
Nested — only inner loop exits.