Anonymous user
Boston Python Workshop 8/Friday/Tutorial: Difference between revisions
Boston Python Workshop 8/Friday/Tutorial (view source)
Revision as of 23:05, 12 July 2013
, 10 years ago→more choices: if and else
imported>Jesstess |
|||
(15 intermediate revisions by 2 users not shown) | |||
Line 281:
fish = "humuhumunukunukuapuaʻa"
name_length = len(fish)
fish + " is a Hawaiian fish whose name is " + str(name_length) + " characters long."
</pre>
Line 349:
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
[[File:Fireworks.png|200px]]
== Part 2: Printing==
Line 393 ⟶ 395:
<ol>
<li>Download the file http://mit.edu/jesstess/www/BostonPythonWorkshop8/nobel.py by right-clicking on it and saying to save it as a ".py" file to your Desktop. The ".py" extension hints that this is a Python script.</li>
<li>Open a terminal prompt, and use the navigation commands (<code>dir</code> and <code>cd</code> on Windows, <code>ls</code>, <code>pwd</code>, and <code>cd</code> on OS X and Linux) to navigate to your
<li>Once you are in your Desktop directory, execute the contents of <code>nobel.py</code> by typing
Line 414 ⟶ 416:
<li>How do you print a multi-line string so that whitespace is preserved?</li>
</ol>
==Booleans==
Please return to the interactive Python interpreter for the rest of the tutorial. And remember: type out the examples. You'll thank yourself tomorrow. :)
[[File:Scales.png|100px]]
Line 501 ⟶ 503:
</pre>
==
We can use these expressions that <i>evaluate</i> to booleans to make decisions and conditionally execute code
[[File:Fork.png|100px]]
Line 507 ⟶ 511:
====if statements====
The simplest way to make a choice in Python is with the <code>if</code> keyword. Here's an example (don't try to type this one, just look at it for now):
▲We can use these expressions that <i>evaluate</i> to booleans to make decisions and conditionally execute code:
<code>if 6 > 5:</code><br />
</pre>▼
That
<ol>
<li>First, type the<br />
<br />
<code>if 6 > 5:</code><br />
<br />
part, and press Enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>>>></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.</li>▼
<li>Press the spacebar 4 times to indent.</li>
<li>Type<br />
<br />
<code>print("Six is greater than five!")</code><br /><br /></li>
<li>Press Enter to end the line. The prompt will still be a <code>...</code></li>
<li>Press Enter
All together, it will look like this:
▲as a prompt, instead of the usual <code>>>></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.
▲<code> print("Six is greater than five!")</code>
▲Press Enter to end the line, and press Enter again to tell Python you are done with this code block. All together, it will look like this:
<pre>
Line 553 ⟶ 558:
====more choices: <code>if</code> and <code>else</code>====
Use the <b><code>else</code></b> keyword, together with <code>if</code>, to execute different code when the <code>if</code> condition isn't <code>True</code>. Try this:
<pre>
Line 564 ⟶ 571:
</pre>
Like with <code>if</code>, the code block under the <code>else</code>
====compound conditionals: <code>and</code> and <code>or</code>====
You can check multiple expressions together using the <b><code>and</code></b> and <b><code>or</code></b> keywords. If two expressions are joined by an <code>and</code>, they <b>both</b> have to be <code>True</code> for the overall expression to be <code>True</code>. If two expressions are joined by an <code>or</code>, as long as <b>at least one</b> is <code>True</code>, the overall expression is <code>True</code>.
Try typing these out and see what you get:
Line 612 ⟶ 619:
====even more choices: <code>elif</code>====
If you
<pre>
Line 625 ⟶ 632:
</pre>
You don't have to have an <code>else</code> block, if you don't need it. That just means there isn't default code to execute when none of the <code>if</code> or <code>elif</code> conditions are <code>True</code>:
<pre>
|