Skip to content

gh-162770: Improve explanation of (for|while)-else in tutorial - #162895

Open
sirosen wants to merge 1 commit into
python:mainfrom
sirosen:docs/tutorial/clarify-for-else
Open

sirosen wants to merge 1 commit into
python:mainfrom
sirosen:docs/tutorial/clarify-for-else

Conversation

@sirosen

@sirosen sirosen commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor
  • Remove the sentence that sparked some confusion, which starts "As the loop executes ..."

  • The opening of "else Clauses on Loops" now states that the else is paired with the for or while loop keyword, not with an internal break -- this small change better prepares readers for the possibility of while-else and for-else without a break (which is rare but valid).

  • The final paragraph of this doc section has been reworded, so that it is clear that it, and the section above it, are both analogies which can help readers understand for-else and while-else. The precise rules for what the else clause does are properly stated above.

@picnixz, let me know if you think we should make more changes. :)
I think that if you look at the whole section with these updates, it reads pretty well now.

- Remove the sentence that sparked some confusion, which starts
  "As the loop executes ..."

- The opening of "else Clauses on Loops" now states that the `else`
  is paired with the `for` or `while` loop keyword, not with an
  internal `break` -- this small change better prepares readers for the
  possibility of `while-else` and `for-else` without a `break` (which is
  rare but valid).

- The final paragraph of this doc section has been reworded, so that it
  is clear that it, and the section above it, are both analogies which
  can help readers understand `for-else` and `while-else`. The precise
  rules for what the `else` clause does are properly stated above.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36280429 | 📁 Comparing f6f18ea against main (846acf1)

  🔍 Preview build  

1 file changed
± tutorial/controlflow.html

occurs, and a loop's ``else`` clause runs when no ``break`` occurs. For more on
the ``try`` statement and exceptions, see :ref:`tut-handling`.
The :keyword:`!else` clause of a :keyword:`!for` loop can also be understood
similarly to the :keyword:`!else` clause of a :keyword:`try` statement: a

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now staring at the fact that this sentence is using the passive voice.
An alternative:

An alternative way to understand the else clause of a for loop is to compare it to the else clause of a try statement: ...

I'm not sure which is better. (The passive voice was used. But was it used well? 😛 )

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

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant