Anonymous user
Boston Python workshop 2/Friday tutorial: Difference between revisions
Boston Python workshop 2/Friday tutorial (view source)
Revision as of 00:14, 27 May 2011
, 12 years ago→Flow Control
Line 413:
<pre>
if
print "
</pre>
That was our first multi-line piece of code, and the way to enter it at a Python prompt is a little different. First, type the <code>if
Type 4 spaces, and then type <code>print "
<pre>
>>> if
... print "
...
"I'm True!"
1 is truthy▼
</pre>
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 "
<pre>
if False:
print "I'm True!"
</pre>
<pre>
if 1:
▲ print "1 is truthy"
</pre>
<pre>
|