Loop Else

Python · Loops

Theory

Both for and while loops can have an else block, and it has a specific meaning: the else runs only if the loop finished normally, that is, the for loop ran out of items or the while condition became false. If the loop was stopped by break, the else is skipped. In the example, no item in [1, 2, 3] equals 5, so break never runs and 'Completed without break' is printed. With [1, 5, 3], break fires on 5 and nothing is printed.

The name confuses many people: it is best read as 'no break'. It is not related to whether the loop ran at all; a for loop over an empty list goes straight to the else. Its classic use is a search: loop over the items, break when you find a match, and put the 'not found' handling in else, which removes the need for a found = False flag variable. Keep it for that pattern; if the meaning is not obvious to readers, a flag or any() can be clearer.

Why we use it

Search code often needs to know afterwards whether the loop found something. Loop else gives that answer directly, without an extra flag variable that you set inside the loop and check afterwards.

When to use it

Use for/while ... else for 'search, and handle not-found' logic with break. If the loop has no break, the else always runs and adds nothing. For simple yes/no checks, any(x == 5 for x in items) or 5 in items is clearer.

How it works

  1. Start the loop
  2. Take the next item
  3. Does the item trigger break? Yes: Leave the loop and SKIP the else block. No: Keep looping.
  4. Items left? Yes: Take the next one. No: Loop ended normally.
  5. Run the else block: print('Completed without break')
  6. Continue after the loop

Syntax

for item in iterable:
    ...
else:
    ...

Example

for n in [1,2,3]:
    if n == 5:
        break
else:
    print("Completed without break")

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