You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unclear paragraph in Section 4.5 (else Clauses on Loops) of Tutorial #162770
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.
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?
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.
I wouldn't be shocked by code like that existing. Or nested usages (which become dizzying quickly).
for-else and while-elseare 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.
Activity
picnixz commentedon Nov 28, 2026
I'm also confused. When was this changed?
sirosen commentedon Nov 28, 2026
This was part of the initial text for that section: #123946
I suggest striking the second sentence:
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?
nedbat commentedon Nov 28, 2026
@sirosen that sounds fine to me.
picnixz commentedon Nov 28, 2026
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 theforconstruction 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.But I don't remember if this was a pattern I saw in a discussion or if it's something that really exists.
sirosen commentedon Nov 28, 2026
I wouldn't be shocked by code like that existing. Or nested usages (which become dizzying quickly).
for-elseandwhile-elseare hard to explain. But I think the cited text as it is does a decent job: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-elseandwhile-elsewithtry-except-else-- to me, that's a valid comparison, but I'm not sure it's suitable for the tutorial.