Skip to content

Unclear paragraph in Section 4.5 (else Clauses on Loops) of Tutorial #162770

Backlog

Description

@alovicious72

Documentation URL

https://docs.python.org/3/tutorial/controlflow.html#else-clauses-on-loops

Documentation

I find the following, relatively-recent addition to the Tutorial's section on "else Clauses on Loops" (Section 4.5) to be a little unclear:

One way to think of the else clause is to imagine it paired with the if inside the loop. As the loop executes, it will run a sequence like if/if/if/else. The if is inside the loop, encountered a number of times. If the condition is ever true, a break will happen. If the condition is never true, the else clause outside the loop will execute.

The last three sentences of that paragraph are clear and correct - but I am unclear about what the author intended to clarify/illustrate using the first two sentences. For example, I do not see how paring the else with the "if" inside the loop would induce an execution sequence like if/if/if/else.

In my own opinion, the explanation of the prior code example is sufficient without this paragraph (or at least the first two sentences of it which I found troublesome to understand).

If I am simply missing something obvious, then I apologize for the nuisance of reviewing this issue.

Linked PRs

Activity

  1. added
    docs
    on Nov 27, 2026
  2. picnixz commented on Nov 28, 2026

    @picnixz
    Member

    I'm also confused. When was this changed?

  3. sirosen commented on Nov 28, 2026

    @sirosen
    Contributor

    This was part of the initial text for that section: #123946

    I suggest striking the second sentence:

    One way to think of the else clause is to imagine it paired with the if inside the loop. The if is inside the loop, encountered a number of times. If the condition is ever true, a break will happen. If the condition is never true, the else clause outside the loop will execute.

    The text is drawing a connection between for-else and if-else by pointing out that for-else typically has an inner "if". The "if/if/if/else" phrase is probably where that message gets lost.

    cc @nedbat , as the original author, do you feel that your meaning is preserved (and clarified) by that removal?

  4. nedbat commented on Nov 28, 2026

    @nedbat
    Member

    @sirosen that sounds fine to me.

  5. picnixz commented on Nov 28, 2026

    @picnixz
    Member

    I may be wrong here but I still find it hard to explain. One reason is that you can have for-else loops without any ifs inside (using the for construction just to iterate over things), stricly speaking. I think there is some example of that in the stdlib somewhere where the break is unconditional assuming you entered the loop, e.g.

    for x in iterator_yielding_at_most_1_item:
        do()
        break
    else:
        fallback

    But I don't remember if this was a pattern I saw in a discussion or if it's something that really exists.

  6. sirosen commented on Nov 28, 2026

    @sirosen
    Contributor

    I wouldn't be shocked by code like that existing. Or nested usages (which become dizzying quickly).

    for-else and while-else are hard to explain. But I think the cited text as it is does a decent job:

    One way to think of the else clause is to imagine it paired with the if inside the loop.

    That's not saying this is an exact explanation, but a way of helping beginners get used to for-else. And it comes after a more precise explanation.

    I'm actually more concerned about the following paragraph, which compares for-else and while-else with try-except-else -- to me, that's a valid comparison, but I'm not sure it's suitable for the tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions