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

imported>Jesstess
imported>Paulproteus
 
(5 intermediate revisions by one other user not shown)
Line 539:
 
<pre>
if True6 > 5:
print "I'mSix Trueis greater than five!"
</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 True6 > 5:</code> part, and hit enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>&gt;&gt;&gt;</code>. This is Python telling us that we are in the middle of a <b>code block</b>, and so long as we indent our code it should be a part of this code block.
 
Type 4 spaces, and then type <code>print "I'mSix Trueis greater than five!"</code>. Hit enter to end the line, and hit enter again to tell Python you are done with this code block. All together, it will look like this:
 
<pre>
>>> if True6 > 5:
... print "I'mSix is greater than Truefive!"
...
Six is greater than five!
"I'm True!"
</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>True</code>6 really is truthygreater than 5, Python executes the code block under the if statement, and we see "I'mSix is greater than Truefive!" printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct:
 
<pre>
if False0 > 2:
print "I'mZero Trueis greater than two!"
</pre>
 
<pre>
if "banana" in "bananarama":
if 1:
print "1I ismiss truthythe 80s."
</pre>
 
<pre>
if 0:
print "0 is truthy"
</pre>
 
<pre>
if "test":
print "non-empty strings are truthy"
</pre>
 
<pre>
if "":
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 0, is <code>True</code>.
 
====more choices: <code>if</code> and <code>else</code>====
Line 602 ⟶ 585:
====compound conditionals: <code>and</code> and <code>or</code>====
 
You can check multiple expressions together using the <code>and</code> and <code>or</code> keywords. If two expressions are joined by an <code>and</code>, they <b>both</b> have to be True for the overall expression to be True. If two expressions are joined by an <code>or</code>, as long as <b>at least one</b> is True, the overall expression is True.
 
Try typing these out and see what you get:
 
<pre>
Line 613 ⟶ 598:
 
<pre>
"a" in "hello" or "e" in "hello"
</pre>
 
Line 670 ⟶ 655:
 
If color had been "purple", that code wouldn't have printed anything.
 
<b>Remember! = is for assignment, == is for comparison.</b>
 
==Writing Functions==
Anonymous user