else essentially superfluous
<p>I don't have a background in programming, so this is probably really dumb, but I've never considered this before: it seems that the <code>else</code> statement is essentially superfluous because when the condition is False, Python just moves to the next unindented line .</p>

<p>For example, normally you would write: (if not using <code>elif</code>)</p>

<pre><code>x=2
if x == 1: 
    value = "one"
else:
    if x == 2:
        value = "two"
print value
</code></pre>

<p>But this works too:</p>

<pre><code>x=2
if x == 1: 
    value = "one"
if x == 2:
    value = "two"
print value
</code></pre>

<p>Could someone give an example that shows how and when the <code>else:</code> statement is essential?</p>

<p>The <code>else</code> clause is useful if you are looking for a specific case (using <code>if</code>) and then everything else that is not covered by the <code>if</code> (using <code>else</code>). </p>

<p>Often a <a href="https://docs.python.org/2.5/whatsnew/pep-308.html" rel="nofollow">conditional expression</a> is used:</p>

<pre><code>&gt;&gt;&gt; x=3
&gt;&gt;&gt; value='one' if x==1 else 'not one'
&gt;&gt;&gt; value
'not one'
</code></pre>

<p>If you did not use an <code>else</code> here (either as part of the conditional or as part of an <code>if/else</code> then <code>value</code> would not de defined:</p>

<pre><code>&gt;&gt;&gt; del value   # remove the name `value`
&gt;&gt;&gt; x
3
&gt;&gt;&gt; if x==1: value='one'
... 
&gt;&gt;&gt; value
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'value' is not defined
</code></pre>

<p>So you do need the <code>else</code> class to make sure that <code>value</code> received a assignment if x is not 1.</p>

<hr>

<p>BTW, In Python, rather than long cascade of <code>if/elif/elif/else</code> you can use a dictionary with a default:</p>

<pre><code>&gt;&gt;&gt; conditions={1:'one', 2:'two', 3:'three'}
&gt;&gt;&gt; conditions.get(2,'not one, two, or three...')
'two'
&gt;&gt;&gt; conditions.get(19,'not one, two, or three...')
'not one, two, or three...'
</code></pre>

python if-statement syntax