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 (→Types) |
|||
(20 intermediate revisions by 2 users not shown) | |||
Line 89:
In the example above, "type" was the name of the function. <code>type</code> takes one argument; we first gave <code>type</code> an argument of 1 and then gave it an argument of 1.0.
==== Diagram of "calling" a function ====
[[File:Function_diagram.png]]
===Command history===
Line 220 ⟶ 224:
<pre>
name = "Jessica"
</pre>
===Printing===▼
You can print strings using <code>print</code>:▼
<pre>▼
h = "Hello"▼
w = "World"▼
print h + w▼
</pre>▼
<pre>▼
my_string = "Alpha " + "Beta " + "Gamma " + "Delta"▼
print my_string▼
</pre>▼
▲How about printing different data types together?
<pre>
</pre>
Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened:
<code>>>>
<code>Traceback (most recent call last):</code><br />
<code> File "<stdin>", line 1, in <module></code><br />
Line 262 ⟶ 251:
<pre>
</pre>
Line 270 ⟶ 259:
<pre>
</pre>
Line 278 ⟶ 267:
<pre>
</pre>
Line 288 ⟶ 277:
<pre>
fish = "humuhumunukunukuapuaʻa"
</pre>
Line 300 ⟶ 289:
<pre>
</pre>
Line 309 ⟶ 298:
<pre>
</pre>
Line 321 ⟶ 310:
<pre>
</pre>
Line 327 ⟶ 316:
<pre>
h = "Happy"
b = "Birthday"
</pre>
Line 343 ⟶ 332:
<pre>
total = 1.5 - 1/2
type(total)
</pre>
Line 352 ⟶ 341:
b = "brown"
c = "fox jumps over the lazy dog"
</pre>
Line 360 ⟶ 349:
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
[[File:Fireworks.png|200px]]
So far we've been learning at the interactive <b>Python interpreter</b>. When you are working at the interpreter, any work that you do gets printed to the screen. For example:
▲<pre>
▲h = "Hello"
▲w = "World"
h + w
▲</pre>
will display "HelloWorld".
Another place that we will be writing Python code is in a file. When we run Python code from a file instead of interactively, we don't get work printed to the screen for free. We have to tell Python to print the information to the screen. The way we do this is with the <b>print</b> function. Here's how it works:
▲<pre>
h = "Hello"
w = "World"
▲</pre>
<pre>▼
▲my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
</pre>▼
The string manipulate is exactly the same as before. The only difference is that you need to use <b>print</b> to print results to the screen:
becomes
We'll see more examples of the print function in the next section.
==Python scripts==
Line 365 ⟶ 391:
[[File:Treasure_map.png|100px]]
Until now we've been
<ol>
<li>Download the file http://mit.edu/jesstess/www/
<li>Open a
<li>Once you are in your
<pre>
Line 376 ⟶ 402:
</pre>
at
<code>nobel.py</code> introduces two new concepts: comments and multiline strings.</li>
Line 390 ⟶ 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 477 ⟶ 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 483 ⟶ 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>
▲</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
</ol>
All together, it will look like this:
▲<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.
▲<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>
>>> if 6 > 5:
... print
...
Six is greater than five!
Line 519 ⟶ 548:
<pre>
if 0 > 2:
print
</pre>
<pre>
if "banana" in "bananarama":
print
</pre>
====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 535 ⟶ 566:
brother_age = 12
if sister_age > brother_age:
print
else:
print
</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 569 ⟶ 600:
temperature = 32
if temperature > 60 and temperature < 75:
print
else:
print
</pre>
Line 577 ⟶ 608:
hour = 11
if hour < 7 or hour > 23:
print
print
else:
print
print
</pre>
Line 588 ⟶ 619:
====even more choices: <code>elif</code>====
If you
<pre>
Line 594 ⟶ 625:
brother_age = 12
if sister_age > brother_age:
print
elif sister_age == brother_age:
print
else:
print
</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>
color = "orange"
if color == "green" or color == "red":
print
elif color == "black" or color == "orange":
print
elif color == "pink":
print
</pre>
|