FWIW I stumbled upon a StackOverflow question today where as usual someone new confused the else clause for a for loop with one for an if statement.
And it was marked as a duplicate to this question, which received hundreds of upvotes and which is really a rant about the poor choice of this keyword.
Not to beat a dead horse, but I find the best explanation of for/else is that it is naturally used where there is a condition being evaluated:
for thing in container:
if something_about(thing):
# Found it!
do_something(thing)
break
else:
# Didn't find it..
no_such_thing()
I think of the else clause as pairing with the if in the loop. If you conceptually unroll the loop, you have an if/if/if/if/else structure. The if is in the loop, and the else is outside it.
The implicit condition is that the iterator used by the for loop has not raised StopIteration. Something like
# There are lots of ways this could be written...
_iterator = iter(iterable)
_raised = False
while not _raised:
try:
x = next(_iterator)
except StopIteration:
_raised = True
else:
<body>
else:
<stuff>
Thanks for the PR. As a tutorial though I think the concept of “loop unrolling” may sound too foreign to novices. The paragraph should serve the purpose just as well without this sentence.