Community Data Science Workshops (Spring 2014)/Friday April 4th Tutorial: Difference between revisions

moved to wiki.communitydata.cc
imported>Mako
(new text and fixed link)
 
imported>Jtmorgan
(moved to wiki.communitydata.cc)
 
(4 intermediate revisions by one other user not shown)
Line 1:
{{CDSW Moved}}
Welcome to the Friday tutorial!
 
Line 5 ⟶ 6:
This is an interactive tutorial! As you go through this tutorial, any time you see something that looks like this:
 
a = "Hello"
<pre>
a = "Hello"
</pre>
 
you should type the expression at a Python prompt, hitting Return after every line and noting the output.
Line 21 ⟶ 20:
===Addition===
 
2 + 2
<pre>
2 1.5 + 2.25
1.5 + 2.25
</pre>
 
===Subtraction===
 
4 - 2
<pre>
4 100 - 2.5
100 0 - .52
0 - 2
</pre>
 
===Multiplication===
 
2 * 3
<pre>
2 * 3
</pre>
 
===Division===
 
4 / 2
<pre>
4 1 / 2
1 / 2
</pre>
 
Hey now! That last result is probably not what you expected. What's going on here is that integer divison produces an integer. You need a number that knows about the decimal point to get a decimal out of division:
 
1.0 / 2
<pre>
1.0 / 2
</pre>
 
This means you have to be careful when manipulating fractions. If you were doing some baking and needed to add 3/4 of a cup of flour and 1/4 of a cup of flour, we know in our heads that 3/4 + 1/4 = 1 cup. But try that at the Python prompt:
 
3/4 + 1/4
<pre>
3/4 + 1/4
</pre>
 
What do you need to do to get the right answer? Use data types that understand decimals for each of the divisions:
 
3.0/4 + 1.0/4
<pre>
3.0/4.0 + 1.0/4.0
3.0/4.0 + 1.0/4.0
</pre>
 
The two previous expressions produce the same result. You only need to make one of the numbers in each fraction have a decimal. When the Python interpreter goes to do the division, it notices that one of the numbers in the fraction cares about decimals and says "that means I have to make the other number care about decimals too".
Line 72 ⟶ 57:
[[File:Geometry.png|150px]]
 
There's a helpful <b>'''function</b>''' (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what <b>'''data type</b>''' -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types:
 
<pre>
type(1)
type(1.0)
</pre>
 
type(1)
So now we've seen two data types: <b>integers</b> and <b>floats</b>.
type(1.0)
So now we've seen two data types: '''integers''' and '''floats'''.
 
By the way, what is a "function"? Here are the important ideas about functions:
Line 85 ⟶ 68:
* A function encapsulates a useful bit of work and gives that work a name.
* You provide input to a function and it produces output. For example, the <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output.
* 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.
 
Line 96 ⟶ 79:
===Command history===
 
Stop here and try hitting the Up arrow on your keyboard a few times. The Python <b>'''interpreter</b>''' saves a history of what you've entered, so you can arrow up to old commands and hit Return to re-run them!
 
==Variables==
Line 104 ⟶ 87:
A lot of work gets done in Python using variables. Variables are a lot like the variables in math class, except that in Python variables can be of any data type, not just numbers.
 
type(4)
<pre>
x = 4
type(4)
x
x = 4
type(x)
x
2 * x
type(x)
2 * x
</pre>
 
Giving a name to something, so that you can refer to it by that name, is called <b>'''assignment</b>'''. Above, we assigned the name 'x' to 4, and after that we can use <code>x</code> wherever we want to use the number 4.
 
Variables can't have spaces or other special characters, and they need to start with a letter. Here are some valid variable names:
Line 130 ⟶ 111:
out:
 
4
<pre>
4
</pre>
 
But if you assign 4 to a variable, nothing is printed:
 
x = 4
<pre>
x = 4
</pre>
 
You can think of it as that something needs to get the output. Without an assignment, the winner is the screen. With assignment, the output goes to the variable.
Line 144 ⟶ 121:
You can reassign variables if you want:
 
x = 4
<pre>
x
x = 4
x = 5
x
x
x = 5
x
</pre>
 
Sometimes reassigning a variable is an accident and causes bugs in programs.
 
x = 3
<pre>
x y = 34
y x =* 4y
x * yx
x 2 * x - 1 * y
2 * x - 1 * y
</pre>
 
Order of operations works pretty much like how you learned in school. If you're unsure of an ordering, you can add parentheses like on a calculator:
 
(2 * x) - (1 * y)
<pre>
(2 * x) - (1 * y)
</pre>
 
Note that the spacing doesn't matter:
 
x = 4
<pre>
x = 4
</pre>
 
and
 
x=4
<pre>
x=4
</pre>
 
are both valid Python and mean the same thing.
 
(2 * x) - (1 * y)
<pre>
(2 * x) - (1 * y)
</pre>
 
and
 
(2*x)-(1*y)
<pre>
(2*x)-(1*y)
</pre>
 
are also both valid and mean the same thing. You should strive to be consistent with whatever spacing you like or a job requires, since it makes reading the code easier.
Line 199 ⟶ 162:
[[File:Letter.png|100px]]
 
So far we've seen two data types: <b>'''integers</b>''' and <b>'''floats</b>'''. Another useful data type is a <b>'''string</b>''', which is just what Python calls a bunch of characters (like numbers, letters, whitespace, and punctuation) put together. Strings are indicated by being surrounded by quotes:
 
"Hello"
<pre>
"Python, I'm your #1 fan!"
"Hello"
"Python, I'm your #1 fan!"
</pre>
 
Like with the math data types above, we can use the <code>type</code> function to check the type of strings:
 
type("Hello")
<pre>
type("Hello"1)
type("1")
type("1")
</pre>
 
===String Concatenation===
Line 218 ⟶ 177:
You can smoosh strings together (called "concatenation") using the '+' sign:
 
"Hello" + "World"
<pre>
"Hello" + "World"
</pre>
 
name = "Jessica"
<pre>
"Hello " + name
name = "Jessica"
"Hello " + name
</pre>
 
How about concatenating different data types?
 
"Hello" + 1
<pre>
"Hello" + 1
</pre>
 
Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened:
Line 240 ⟶ 193:
<code>TypeError: cannot concatenate 'str' and 'int' objects</code>
 
Python is giving us a <b>'''traceback</b>'''. A traceback is details on what was happening when Python encountered an Exception or Error -- something it doesn't know how to handle.
 
There are many kinds of Python errors, with descriptive names to help us humans understand what went wrong. In this case we are getting a <code>TypeError</code>: we tried to do some operation on a data type that isn't supported for that data type.
Line 250 ⟶ 203:
We saw above the we can concatenate strings:
 
"Hello" + "World"
<pre>
"Hello" + "World"
</pre>
 
works just fine.
Line 258 ⟶ 209:
However,
 
"Hello" + 1
<pre>
"Hello" + 1
</pre>
 
produces a <code>TypeError</code>. We are telling Python to concatenate a string and an integer, and that's not something Python understands how to do.
Line 266 ⟶ 215:
We can convert an integer into a string ourselves, using the <code>str</code> function:
 
"Hello" + str(1)
<pre>
"Hello" + str(1)
</pre>
 
Like the <code>type</code> function from before, the <code>str</code> function takes 1 argument. In the above example it took the integer 1. <code>str</code> takes a Python object as input and produces a string version of that input as output.
Line 276 ⟶ 223:
There's another useful function that works on strings called <code>len</code>. <code>len</code> returns the length of a string as an integer:
 
len("Hello")
<pre>
len("Hello")
fish = "humuhumunukunukuapuaʻa"
len("")
name_length = len(fish)
fish = "humuhumunukunukuapuaʻa"
fish + " is a Hawaiian fish whose name is " + str(name_length) + " characters long."
name_length = len(fish)
fish + " is a Hawaiian fish whose name is " + str(name_length) + " characters long."
</pre>
 
===Quotes===
Line 288 ⟶ 233:
We've been using double quotes around our strings, but you can use either double or single quotes:
 
'Hello'
<pre>
' "Hello'"
"Hello"
</pre>
 
Like with spacing above, use whichever quotes make the most sense for you, but be consistent.
Line 297 ⟶ 240:
You do have to be careful about using quotes inside of strings:
 
'I'm a happy camper'
<pre>
'I'm a happy camper'
</pre>
 
This gives us another <b>'''traceback</b>''', for a new kind of error, a <code>SyntaxError</code>. When Python looks at that expression, it sees the string 'I' and then
 
<code>m a happy camper'</code>
Line 309 ⟶ 250:
We can use double quotes to avoid this problem:
 
"I'm a happy camper"
<pre>
"I'm a happy camper"
</pre>
 
One fun thing about strings in Python is that you can multiply them:
 
"A" * 40
<pre>
"AABC" * 4012
h = "Happy"
"ABC" * 12
h b = "HappyBirthday"
(h + b) * 10
b = "Birthday"
(h + b) * 10
</pre>
 
==Part 1 Practice==
Line 330 ⟶ 267:
 
1.
 
<pre>
total = 1.5 - 1/2
total
type(total)
 
</pre>
 
2.
 
<pre>
a = "quick"
b = "brown"
c = "fox jumps over the lazy dog"
"The " + a * 3 + " " + b * 3 + " " + c
</pre>
 
==End of Part 1==
Line 354 ⟶ 290:
== 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:
 
h = "Hello"
<pre>
h w = "HelloWorld"
h + w
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:
 
h = "Hello"
<pre>
h w = "HelloWorld"
print(h + w)
w = "World"
 
print(h + w)
my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
</pre>
print(my_string)
 
<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>
Line 394 ⟶ 325:
 
<ol>
<li>Download the file http://mako.cc/teaching/2014/cdwscdsw/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 Desktop directory. See [[Boston Python Workshop 8/Friday#Goal_.234:_practice_navigating_the_computer_from_a_terminal|navigating from a terminal]] for a refresher on those commands.</li>
<li>Once you are in your Desktop directory, execute the contents of <code>nobel.py</code> by typing
 
python nobel.py
<pre>
python nobel.py
</pre>
 
at the terminal prompt.
Line 416 ⟶ 345:
<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]]
 
So far, the code we've written has been <i>unconditional</i>: no choice is getting made, and the code is always run. Python has another data type called a <b>boolean</b> that is helpful for writing code that makes decisions. There are two booleans: <code>True</code> and <code>False</code>.
 
<pre>
True
</pre>
 
<pre>
type(True)
</pre>
 
<pre>
False
</pre>
 
<pre>
type(False)
</pre>
 
You can test if Python objects are equal or unequal. The result is a boolean:
 
<pre>
0 == 0
</pre>
 
<pre>
0 == 1
</pre>
 
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>.
 
This is an important idea and can be a source of bugs until you get used to it: <b>= is assignment, == is comparison</b>.
 
Use <code>!=</code> to test for inequality:
 
<pre>
"a" != "a"
</pre>
 
<pre>
"a" != "A"
</pre>
 
<code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code> have the same meaning as in math class. The result of these tests is a boolean:
 
<pre>
1 > 0
</pre>
 
<pre>
2 >= 3
</pre>
 
<pre>
-1 < 0
</pre>
 
<pre>
.5 <= 1
</pre>
 
You can check for containment with the <code>in</code> keyword, which also results in a boolean:
 
<pre>
"H" in "Hello"
</pre>
 
<pre>
"X" in "Hello"
</pre>
 
Or check for a lack of containment with <code>not in</code>:
 
<pre>
"a" not in "abcde"
</pre>
 
<pre>
"Perl" not in "Boston Python Workshop"
</pre>
 
==Making choices==
Line 523 ⟶ 366:
&nbsp;&nbsp;&nbsp;&nbsp;<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>&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.</li>
 
<li>Press the spacebar 4 times to indent.</li>
Line 542 ⟶ 385:
</pre>
 
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 6 really is greater than 5, Python executes the code block under the if statement, and we see "Six is greater than five!" printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct:
Line 558 ⟶ 401:
====more choices: <code>if</code> and <code>else</code>====
 
<b>'''<code>if</code></b>''' lets you execute some code only if a condition is <code>True</code>. What if you want to execute different code if a condition is <code>False</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 575 ⟶ 418:
====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 619 ⟶ 462:
====even more choices: <code>elif</code>====
 
If you need to execute code conditional based on more than two cases, you can use the <b>'''<code>elif</code></b>''' keyword to check more cases. You can have as many <code>elif</code> cases as you want; Python will go down the code checking each <code>elif</code> until it finds a <code>True</code> condition or reaches the default <code>else</code> block.
 
<pre>
Line 646 ⟶ 489:
If color had been "purple", that code wouldn't have printed anything.
 
<b>'''Remember that '=' is for assignment and '==' is for comparison.</b>'''
 
====In summary: the structure of if/elif/else====
Line 655 ⟶ 498:
 
Do you understand the difference between <code>elif</code> and <code>else</code>? When do you indent? When do you use a colon? If you're not sure, talk about it with a neighbor or staff member.
 
==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]]
 
So far, the code we've written has been <i>unconditional</i>: no choice is getting made, and the code is always run. Python has another data type called a '''boolean''' that is helpful for writing code that makes decisions. There are two booleans: <code>True</code> and <code>False</code>.
 
True
 
type(True)
 
False
 
type(False)
 
You can test if Python objects are equal or unequal. The result is a boolean:
 
0 == 0
 
0 == 1
 
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>.
 
This is an important idea and can be a source of bugs until you get used to it: '''= is assignment, == is comparison'''.
 
Use <code>!=</code> to test for inequality:
 
"a" != "a"
 
"a" != "A"
 
<code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code> have the same meaning as in math class. The result of these tests is a boolean:
 
1 > 0
 
2 >= 3
 
-1 < 0
 
.5 <= 1
 
You can check for containment with the <code>in</code> keyword, which also results in a boolean:
 
"H" in "Hello"
 
"X" in "Hello"
 
Or check for a lack of containment with <code>not in</code>:
 
"a" not in "abcde"
 
"Perl" not in "Python Workshop"
 
==End of Part 2==
Line 661 ⟶ 558:
 
[[File:Champagne.png|100px]][[File:Party.png|125px]]
 
 
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
Anonymous user