Python Tutorial: Else Clauses on Loops
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.
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?
What exactly prevents the loop’s `else` from running?
Does the loop’s `else` depend on the loop condition failing or the list being empty?
How does the “no break” mental model help?
How is this used in the `find_index` function?
Review Questions
- In your own words, what condition must be true for a loop’s `else` block to execute?
- How would the behavior change in a loop if you replaced `break` with `continue`?
- 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
A loop `else` block runs only when the loop terminates without executing `break`.
- 2
A `for` loop can run its `else` after iterating all items, as long as no `break` occurs inside the loop body.
- 3
A `while` loop runs its `else` when the condition becomes false naturally, but skips it if `break` executes.
- 4
Treat loop `else` as “no break”: it’s the path for “not exited early.”
- 5
In search patterns, loop `else` cleanly represents the “not found” case (commonly returning `-1`).
- 6
When a match is found and `break` executes, the loop’s `else` is bypassed and the function can return the found index instead.