Boston Python Workshop 3/Friday/Tutorial: Difference between revisions

imported>Jesstess
imported>Jesstess
Line 479:
So what is going on here? When Python encounters the <code>if</code> keyword, it <i>evaluates</i> the <i>expression</i> following the keyword and before the colon. If that expression is <b>True</b>, Python executes the code in the indented code block under the <code>if</code> line. If that expression is <b>False</b>, Python skips over the code block.
 
In this case, because <code>1</code> is truthy, Python executes the code block under the if statement, and we see "I'm True!" printed to the screen. What do you think will happen with thisthese? oneTry them out:
 
<pre>
Line 505:
print "empty strings are truthy"
</pre>
 
In summary, things that represent emptyness or zeroness, including the boolean <code>False</code>, empty string, and 0, will evaluate to <code>False</code> in a conditional expression. Everything else, including the boolean <code>True</code>, non-empty strings, and numbers other than 1, is <code>True</code>
 
You can use the <code>else</code> keyword to conditionally execute code when the expression for the <code>if</code> block isn't true:
Anonymous user