title: How can I make sense of the `else` clause of Python loops?
body: <p>Many Python programmers are probably unaware that the syntax of <code>while</code> loops and <code>for</code> loops includes an optional <code>else:</code> clause:</p>
  <pre><code>for val in iterable:
      do_something(val)
  else:
      clean_up()
  </code></pre>
  <p>The body of the <code>else</code> 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 <code>return</code> or <code>break</code> skips the <code>else</code> clause; exiting after a <code>continue</code> executes it. I know this only because I just <a href="https://docs.python.org/3/reference/compound_stmts.html#the-for-statement" rel="nofollow noreferrer">looked it up</a> (yet again), because  I can never remember <em>when</em> the <code>else</code> clause is executed.</p>
  <p>Always? On &quot;failure&quot; of the loop, as the name suggests? On regular termination? Even if the loop is exited with <code>return</code>? I can never be entirely sure without looking it up.</p>
  <p>I blame my persisting uncertainty on the choice of keyword: I find <code>else</code> incredibly unmnemonic for this semantics. My question is not &quot;why is this keyword used for this purpose&quot; (which I would probably vote to close, though only after reading the answers and comments), but <strong>how can I think about the <code>else</code> keyword so that its semantics make sense, and I can therefore remember it?</strong></p>
  <p>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 <code>try</code> statement's <code>else:</code> 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 <code>else</code> will clarify its function and make it more memorable, but I'm after connecting name to function, not after historical explanation per se.</p>
  <p>The answers to <a href="https://stackoverflow.com/q/9979970/699305">this question</a>, 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 <code>else</code> with the keyword choice), but I feel there should be a link to this question somewhere.</p>