Python order of conditions
<p>When I enter this code, the program never throws an error, even if I enter a non-number:</p>

<pre><code>user_input = input("Enter a number.")
if user_input.isdigit() and 0 &lt;= float(user_input) &lt;= 10:
    print("A number between 0 and 10.")
else:
    print("Not a number between 0 and 10.")
</code></pre>

<p>But, if I enter this code, the program throws an error if I enter a non-number:</p>

<pre><code>user_input = input("Enter a number.")
if 0 &lt;= float(user_input) &lt;= 10 and user_input.isdigit():
    print("A number between 0 and 10.")
else:
    print("Not a number between 0 and 10.")
</code></pre>

<p>Does anyone know why? Does it really make a difference in which order I type conditions?</p>

<p>Of course it matters.</p>

<pre><code>if 0 &lt;= float(user_input) &lt;= 10 and user_input.isdigit():
</code></pre>

<p>First tries to evaluate <code>float(user_input)</code>. If <code>user_input</code> is a non-number string it would raise a <code>ValueError</code>.</p>

<p><br/></p>

<pre><code>if user_input.isdigit() and 0 &lt;= float(user_input) &lt;= 10:
</code></pre>

<p>First tries to evaluate <code>user_input.isdigit()</code>. If it returns <code>False</code> then <code>0 &lt;= float(user_input) &lt;= 10</code> isn't evaluated at all.</p>

<p>This behavior is called "short-circuiting". 
In the predicate <code>A AND B</code>, <code>B</code> will be evaluated only if <code>A</code> is <code>True</code>.
Likewise, in the predicate <code>A OR B</code>, <code>B</code> will be evaluated only if <code>A</code> is <code>False</code>.</p>

python condition