Break

Python · Loops

Theory

break immediately ends the nearest enclosing for or while loop. The rest of the current pass is skipped, no further passes happen, and execution jumps to the first statement after the loop. In the example, the loop is meant to run for n = 0 to 9, but when n reaches 3 the condition n == 3 is true, break runs, and the loop stops, so the output is only 0, 1 and 2. The 3 is never printed, because break comes before print in the body.

break is most useful in searches: stop scanning as soon as you find what you need instead of checking the remaining items. It also exits deliberate infinite loops such as while True:. It affects only the innermost loop, so in nested loops the outer loop keeps going. If a loop has an else clause, break skips that else. Using break outside a loop is a SyntaxError.

Why we use it

Continuing to loop after you already have your answer wastes time, especially on large data. break lets the loop stop the moment a match is found, an error appears, or a limit is reached.

When to use it

Use break to stop a search on the first match or to exit a while True loop. If you just need to know whether any item matches, any() or the in operator is shorter. To skip a single item instead of stopping, use continue.

How it works

  1. for n in range(10): starts
  2. Take the next n
  3. Is n == 3? Yes: break: leave the loop immediately. No: print(n) and continue looping.
  4. Repeat for the next n
  5. After the loop: printed 0, 1, 2

Syntax

for item in iterable:
    if condition:
        break

Example

for n in range(10):
    if n == 3:
        break
    print(n)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic