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

turned pre tags into real wiki tags
imported>Mako
(new text and fixed link)
 
imported>Mako
(turned pre tags into real wiki tags)
Line 5:
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 ⟶ 19:
===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 74 ⟶ 58:
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:
 
type(1)
<pre>
type(1.0)
type(1.0)
</pre>
 
So now we've seen two data types: <b>integers</b> and <b>floats</b>.
 
Line 104 ⟶ 86:
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.
Line 130 ⟶ 110:
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 ⟶ 120:
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 201 ⟶ 163:
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 ⟶ 176:
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 250 ⟶ 202:
We saw above the we can concatenate strings:
 
"Hello" + "World"
<pre>
"Hello" + "World"
</pre>
 
works just fine.
Line 258 ⟶ 208:
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 ⟶ 214:
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 ⟶ 222:
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 ⟶ 232:
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 ⟶ 239:
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
Line 309 ⟶ 249:
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 ⟶ 266:
 
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 356 ⟶ 291:
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".
Line 366 ⟶ 299:
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:
Line 398 ⟶ 328:
<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 ⟶ 344:
<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 655 ⟶ 497:
 
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 <b>boolean</b> 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: <b>= is assignment, == is comparison</b>.
 
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 ⟶ 557:
 
[[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