title: Else clause on Python while statement
body: <p>I've noticed the following code is legal in Python. My question is why? Is there a specific reason?</p>
  <pre><code>n = 5
  while n != 0:
      print n
      n -= 1
  else:
      print &quot;what the...&quot;
  </code></pre>
  <hr />
  <p><sub>Many beginners accidentally stumble on this syntax when they try to put an <code>if</code>/<code>else</code> block inside of a <code>while</code> or <code>for</code> loop, and don't indent the <code>else</code> properly. The solution is to make sure the <code>else</code> block lines up with the <code>if</code>, assuming that it was your intent to pair them. This question explains <em>why it didn't cause a syntax error</em>, and <em>what the resulting code means.</em> See also <a href="https://stackoverflow.com/questions/45621722">I&#39;m getting an IndentationError. How do I fix it?</a>, for the cases where there <em>is</em> a syntax error reported.</sub></p>