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 No edit summary |
|||
(23 intermediate revisions by 2 users not shown) | |||
Line 83:
By the way, what is a "function"? Here are the important ideas about functions:
* A function encapsulates a useful bit of work and gives that work a name.
*
* To use a function, write the name of the function, followed by an open parenthesis, then what the function needs as input (we call that input the <b>arguments</b> to the function), and then a close parenthesis.
* Programmers have a lot of slang around functions. They'll say that functions "take" arguments, or that they "give" or "pass" arguments to a function. "call" and "invoke" are both synonyms for using a function.
==== Diagram of "calling" a function ====
[[File:Function_diagram.png]]
===Command history===
Line 218 ⟶ 224:
<pre>
name = "Jessica"
</pre>
How about concatenating different data types?
<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 260 ⟶ 251:
<pre>
</pre>
Line 268 ⟶ 259:
<pre>
</pre>
Line 276 ⟶ 267:
<pre>
</pre>
Line 286 ⟶ 277:
<pre>
fish = "humuhumunukunukuapuaʻa"
</pre>
Line 298 ⟶ 289:
<pre>
</pre>
Line 307 ⟶ 298:
<pre>
</pre>
Line 319 ⟶ 310:
<pre>
</pre>
Line 325 ⟶ 316:
<pre>
h = "Happy"
b = "Birthday"
</pre>
Line 341 ⟶ 332:
<pre>
total = 1.5 - 1/2
type(total)
</pre>
Line 350 ⟶ 341:
b = "brown"
c = "fox jumps over the lazy dog"
</pre>
Line 358 ⟶ 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==
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"
print(h + w)
</pre>
<pre>
my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
print(my_string)
</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:
<code>h + w</code>
becomes
<code>print(h + w)</code>
We'll see more examples of the print function in the next section.
==Python scripts==
Line 363 ⟶ 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 374 ⟶ 402:
</pre>
at
<code>nobel.py</code> introduces two new concepts: comments and multiline strings.</li>
Line 388 ⟶ 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 475 ⟶ 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 481 ⟶ 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):
<code>if 6 > 5:</code><br />
<code>print("Six is greater than five!")</code>
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 one more time to tell Python you are done with this code block. The code block will now execute.</li>
</ol>
All together, it will look like this:
<pre>
>>> if 6 > 5:
... print
...
Six is greater than five!
Line 517 ⟶ 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 533 ⟶ 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 567 ⟶ 600:
temperature = 32
if temperature > 60 and temperature < 75:
print
else:
print
</pre>
Line 575 ⟶ 608:
hour = 11
if hour < 7 or hour > 23:
print
print
else:
print
print
</pre>
Line 586 ⟶ 619:
====even more choices: <code>elif</code>====
If you
<pre>
Line 592 ⟶ 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>
|