Get AI summaries of any video or article — Sign up free
Python Tutorial: Else Clauses on Loops thumbnail

Python Tutorial: Else Clauses on Loops

Corey Schafer·
4 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

A loop `else` block runs only when the loop terminates without executing `break`.

Briefing

An `else` clause attached to a Python loop runs only when the loop finishes normally—meaning it never encounters a `break`. That single rule resolves the biggest source of confusion for beginners, because unlike `if/else`, a loop’s `else` can appear to “also run” even when the loop body executed many times.

In Python, `else` after a `for` or `while` is not triggered by an empty list or a failed condition. Instead, it’s triggered by the absence of an early exit. With a `for` loop iterating over `[1, 2, 3, 4, 5]`, the loop body prints each number, and then the `else` block runs after the iteration completes—because nothing inside the loop executed `break`. If a `break` is added (for example, breaking when the loop variable equals `3`), the loop stops immediately and the `else` block does not execute.

A `while` loop behaves the same way. If the loop condition becomes false naturally (for instance, counting up until `i` is no longer less than `5`), the `else` block runs after the loop ends. But if `break` occurs inside the loop (such as breaking when `i` reaches `3`), the `else` block is skipped. A helpful mental model is to treat the loop’s `else` as “no break”: run the `else` code only if the loop never hit `break`.

That control-flow pattern becomes practical in search-style logic. The transcript walks through a `find_index` function that takes a list and a target value. The function loops through indices and values; when it finds a match, it `break`s out of the loop. The `else` clause then acts as the “not found” path: if no `break` happened, it returns `-1`. If a match did occur and the loop exited via `break`, the function skips the `else` and returns the found index.

When searching for `

Cornell Notes

In Python, an `else` attached to a `for` or `while` loop executes only if the loop ends without hitting `break`. This is why both the loop body and the loop’s `else` can run: the loop body can run for many iterations, and the `else` runs afterward as long as no early `break` occurred. If `break` executes inside the loop, control jumps out of the loop and the `else` block is skipped. A useful way to remember it is “loop `else` = no break.” This pattern is especially handy for search functions, where `else` naturally represents the “not found” case (returning `-1`).

Why does a loop’s `else` run even when the loop body executed?

Because the loop’s `else` is tied to whether `break` happened, not to whether the loop ran. In the example iterating over `[1, 2, 3, 4, 5]`, the loop prints every item and then finishes normally. Since no `break` executes inside the loop, the `else` block runs afterward.

What exactly prevents the loop’s `else` from running?

Any `break` executed inside the loop. When the code breaks on a condition like `if i == 3: break`, the loop exits immediately and the `else` block is skipped. The same rule applies to `while` loops: a `break` stops the loop and bypasses the `else`.

Does the loop’s `else` depend on the loop condition failing or the list being empty?

No. The transcript explicitly rejects the idea that an empty list or a failed condition triggers the `else`. Instead, the trigger is “no `break`.” A `for` loop over a non-empty list can still run its `else` after completing all iterations, and a `while` loop can run its `else` when the condition becomes false naturally.

How does the “no break” mental model help?

It reframes the `else` clause as a success/failure branch based on early termination. If the loop hits `break`, the “no break” condition is false, so the `else` doesn’t run. If the loop ends normally, “no break” is true, so the `else` runs.

How is this used in the `find_index` function?

The function loops through values looking for a target. On a match, it `break`s, which prevents the `else` from running. If the loop finishes without a match, no `break` occurs, so the loop’s `else` runs and returns `-1` as the “not found” result. If a match was found, it returns the index instead.

Review Questions

  1. In your own words, what condition must be true for a loop’s `else` block to execute?
  2. How would the behavior change in a loop if you replaced `break` with `continue`?
  3. In the `find_index` example, what return value happens when the target is present, and why does the loop’s `else` not run?

Key Points

  1. 1

    A loop `else` block runs only when the loop terminates without executing `break`.

  2. 2

    A `for` loop can run its `else` after iterating all items, as long as no `break` occurs inside the loop body.

  3. 3

    A `while` loop runs its `else` when the condition becomes false naturally, but skips it if `break` executes.

  4. 4

    Treat loop `else` as “no break”: it’s the path for “not exited early.”

  5. 5

    In search patterns, loop `else` cleanly represents the “not found” case (commonly returning `-1`).

  6. 6

    When a match is found and `break` executes, the loop’s `else` is bypassed and the function can return the found index instead.

Highlights

Loop `else` is not an “if/else” alternative; it’s a “no `break` happened” clause that runs after normal loop completion.
Adding a `break` inside the loop immediately prevents the loop’s `else` from running.
The `find_index` function uses loop `else` to return `-1` only when the target never appears in the list.

Topics

  • Loop Else
  • Control Flow
  • Break Semantics
  • Search Pattern
  • Python Syntax