alexis alexis - 5 months ago 8
Python Question

How can I make sense of the `else` statement in Python loops?

Many Python programmers are probably unaware that the syntax of

while
loops and
for
loops includes an optional
else:
clause:

for val in iterable:
do_something(val)
else:
clean_up()


The body of the
else
clause is a good place for certain kinds of clean-up actions, and is executed on normal termination of the loop: I.e., exiting the loop with
return
or
break
skips the
else
clause; exiting after a
continue
executes it. I know this only because I just looked it up (yet again), because I can never remember when the
else
clause is executed.

Always? On "failure" of the loop, as the name suggests? On regular termination? Even if the loop is exited with
return
? I can never be entirely sure without looking it up.

I blame my persisting uncertainty on the choice of keyword: I find
else
incredibly unmnemonic for this semantics. My question is not "why is this keyword used for this purpose" (which I would probably vote to close, though only after reading the answers and comments), but how can I think about the
else
keyword so that its semantics make sense, and I can therefore remember it?


I'm sure there was a fair amount of discussion about this, and I can imagine that the choice was made for consistency with the
try
statement's
else:
clause (which I also have to look up), and with the goal of not adding to the list of Python's reserved words. Perhaps the reasons for choosing
else
will clarify its function and make it more memorable, but I'm after connecting name to function, not after historical explanation per se.

The answers to this question, which my question was briefly closed as a duplicate of, contain a lot of interesting back story. My question has a different focus (how to connect the specific semantics of
else
with the keyword choice), but I feel there should be a link to this question somewhere.

Answer

(This is inspired by @Mark Tolonen's answer.)

An if statement runs its else clause if its condition evaluates to false. Identically, a while loop runs the else clause if its condition evaluates to false.

This rule matches the behavior you described:

  • In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs the else clause.
  • When you execute a break statement, you exit out of the loop without evaluating the condition, so the condition cannot evaluate to false and you never run the else clause.
  • When you execute a continue statement, you evaluate the condition again, and do exactly what you normally would at the beginning of a loop iteration. So, if the condition is true, you keep looping, but if it is false you run the else clause.
  • Other methods of exiting the loop, such as return, do not evaluate the condition and therefore do not run the else clause.

for loops behave the same way. Just consider the condition as true if the iterator has more elements, or false otherwise.