Editing Community Data Science Workshops (Spring 2015)/Day 0 tutorial

From CommunityData

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 5: Line 5:
This is an interactive tutorial! As you go through this tutorial, any time you see something that looks like this:
This is an interactive tutorial! As you go through this tutorial, any time you see something that looks like this:


<syntaxhighlight lang="python">
  a = "Hello"
  a = "Hello"
</syntaxhighlight>


you should type the expression at a '''Python''' prompt, hitting Return after every line and noting the output.
you should type the expression at a '''Python''' prompt, hitting Return after every line and noting the output.
Line 21: Line 19:
===Addition===
===Addition===


<syntaxhighlight lang="python">
  2 + 2
  2 + 2
  1.5 + 2.25
  1.5 + 2.25
</syntaxhighlight>
 
===Subtraction===
===Subtraction===
<syntaxhighlight lang="python">
 
  4 - 2
  4 - 2
  100 - .5
  100 - .5
  0 - 2
  0 - 2
</syntaxhighlight>
 
===Multiplication===
===Multiplication===
<syntaxhighlight lang="python">
 
  2 * 3
  2 * 3
</syntaxhighlight>
 
===Division===
===Division===


First, lets try some basic fractions:
4 / 2
 
<syntaxhighlight lang="python">
  1 / 2
  1 / 2
</syntaxhighlight>
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:


Sure enough, Python says that the answer is 0.5 which is the same as 1/2.
1.0 / 2


Let's try the same thing:
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:


<syntaxhighlight lang="python">
  3/4 + 1/4
  4 / 2
</syntaxhighlight>


Hey now! That last result is a little strange. When you divide a number in Python, even if the answer doesn't need a decimal place (like <code>4/2</code> which equals 2), you get an answer with a decimal point!
What do you need to do to get the right answer? Use data types that understand decimals for each of the divisions:


What's going on here is that in Python, division produces an what's called a <code>float</code> which essentially means a number with a decimal point.
3.0/4 + 1.0/4
3.0/4.0 + 1.0/4.0


When the Python interpreter goes to do the division, it knows that (unlike multiplication for example) division can lead to numbers that aren't whole numbers (like <code>1/2</code>) so it makes sure that the result will always include a decimal place just in case it's needed.
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".
 
 
This shouldn't be important for this workshop but it might be worth knowing that older versions of Python (before version 3) would always round down and return integers instead of giving numbers with decimal places.


==Types==
==Types==
Line 65: Line 57:


There's a helpful '''function''' (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what '''data type''' -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types:
There's a helpful '''function''' (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what '''data type''' -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types:
<syntaxhighlight lang="python">
 
  type(1)
  type(1)
  type(1.0)
  type(1.0)
</syntaxhighlight>
So now we've seen two data types: '''integers''' and '''floats'''.
So now we've seen two data types: '''integers''' and '''floats'''.


Line 93: Line 85:


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.
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.
<syntaxhighlight lang="python">
 
  type(4)
  type(4)
  x = 4
  x = 4
Line 99: Line 91:
  type(x)
  type(x)
  2 * x
  2 * x
</syntaxhighlight>
 
Giving a name to something, so that you can refer to it by that name, is called '''assignment'''. 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.
Giving a name to something, so that you can refer to it by that name, is called '''assignment'''. 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:
Variables can't have spaces or other special characters, and they need to start with a letter. Here are some valid variable names:
<syntaxhighlight lang="python">
 
magic_number = 1500
<code>magic_number = 1500</code><br />
amountOfFlour = .75
<code>amountOfFlour = .75</code><br />
my_name = "Jessica"
<code>my_name = "Jessica"</code>
</syntaxhighlight>


Projects develop naming conventions: maybe multi-word variable names use underscores (like <code>magic_number</code>), or "camel case" (like <code>amountOfFlour</code>). The most
Projects develop naming conventions: maybe multi-word variable names use underscores (like <code>magic_number</code>), or "camel case" (like <code>amountOfFlour</code>). The most
Line 118: Line 109:
Notice how if you type a 4 and hit enter, the Python interpreter spits a 4 back
Notice how if you type a 4 and hit enter, the Python interpreter spits a 4 back
out:
out:
<syntaxhighlight lang="python">
 
  4
  4
</syntaxhighlight>
 
But if you assign 4 to a variable, nothing is printed:
But if you assign 4 to a variable, nothing is printed:
<syntaxhighlight lang="python">
 
  x = 4
  x = 4
</syntaxhighlight>
 
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.
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.


You can reassign variables if you want:
You can reassign variables if you want:
<syntaxhighlight lang="python">
 
  x = 4
  x = 4
  x
  x
  x = 5
  x = 5
  x
  x
</syntaxhighlight>
 
Sometimes reassigning a variable is an accident and causes bugs in programs.
Sometimes reassigning a variable is an accident and causes bugs in programs.
<syntaxhighlight lang="python">
 
  x = 3
  x = 3
  y = 4
  y = 4
Line 141: Line 132:
  x * x
  x * x
  2 * x - 1 * y
  2 * x - 1 * y
</syntaxhighlight>
 
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:
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:
<syntaxhighlight lang="python">
 
  (2 * x) - (1 * y)
  (2 * x) - (1 * y)
</syntaxhighlight>
 
Note that the spacing doesn't matter:
Note that the spacing doesn't matter:
<syntaxhighlight lang="python">
 
  x = 4
  x = 4
</syntaxhighlight>
 
and
and
<syntaxhighlight lang="python">
 
  x=4
  x=4
</syntaxhighlight>
 
are both valid Python and mean the same thing.
are both valid Python and mean the same thing.
<syntaxhighlight lang="python">
 
  (2 * x) - (1 * y)
  (2 * x) - (1 * y)
</syntaxhighlight>
 
and
and
<syntaxhighlight lang="python">
 
  (2*x)-(1*y)
  (2*x)-(1*y)
</syntaxhighlight>
 
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.
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 171: Line 162:


So far we've seen two data types: '''integers''' and '''floats'''. Another useful data type is a '''string''', 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:
So far we've seen two data types: '''integers''' and '''floats'''. Another useful data type is a '''string''', 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:
<syntaxhighlight lang="python">
 
  "Hello"
  "Hello"
  "Python, I'm your #1 fan!"
  "Python, I'm your #1 fan!"
</syntaxhighlight>
 
Like with the math data types above, we can use the <code>type</code> function to check the type of strings:
Like with the math data types above, we can use the <code>type</code> function to check the type of strings:
<syntaxhighlight lang="python">
 
  type("Hello")
  type("Hello")
  type(1)
  type(1)
  type("1")
  type("1")
</syntaxhighlight>
 
===String Concatenation===
===String Concatenation===


You can smoosh strings together (called "concatenation") using the '+' sign:
You can smoosh strings together (called "concatenation") using the '+' sign:
<syntaxhighlight lang="python">
 
  "Hello" + "World"
  "Hello" + "World"


  name = "Jessica"
  name = "Jessica"
  "Hello " + name
  "Hello " + name
</syntaxhighlight>
 
How about concatenating different data types?
How about concatenating different data types?
<syntaxhighlight lang="python">
 
  "Hello" + 1
  "Hello" + 1
</syntaxhighlight>
 
Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened:
Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened:


<pre>
<code>>>> "Hello" + 1</code><br />
>>> "Hello" + 1
<code>Traceback (most recent call last):</code><br />
Traceback (most recent call last):
<code>  File "<stdin>", line 1, in <module></code><br />
  File "<stdin>", line 1, in <module>
<code>TypeError: cannot concatenate 'str' and 'int' objects</code>
TypeError: cannot concatenate 'str' and 'int' objects
</pre>


Python is giving us a '''traceback'''. A traceback is details on what was happening when Python encountered an Exception or Error -- something it doesn't know how to handle.
Python is giving us a '''traceback'''. A traceback is details on what was happening when Python encountered an Exception or Error -- something it doesn't know how to handle.
Line 244: Line 233:


We've been using double quotes around our strings, but you can use either double or single quotes:
We've been using double quotes around our strings, but you can use either double or single quotes:
<syntaxhighlight lang="python">
 
  'Hello'
  'Hello'
  "Hello"
  "Hello"
</syntaxhighlight>
 
Like with spacing above, use whichever quotes make the most sense for you, but be consistent.
Like with spacing above, use whichever quotes make the most sense for you, but be consistent.


Line 265: Line 254:


One fun thing about strings in Python is that you can multiply them:
One fun thing about strings in Python is that you can multiply them:
<syntaxhighlight lang="python">
 
  "A" * 40
  "A" * 40
  "ABC" * 12
  "ABC" * 12
Line 271: Line 260:
  b = "Birthday"
  b = "Birthday"
  (h + b) * 10
  (h + b) * 10
</syntaxhighlight>
 
==Part 1 Practice==
==Part 1 Practice==


Line 279: Line 268:


1.
1.
<syntaxhighlight lang="python">
 
  total = 1.5 - 1/2
  total = 1.5 - 1/2
  total
  total
  type(total)
  type(total)
</syntaxhighlight>
 


2.
2.
<syntaxhighlight lang="python">
 
  a = "quick"
  a = "quick"
  b =  "brown"
  b =  "brown"
  c = "fox jumps over the lazy dog"
  c = "fox jumps over the lazy dog"
  "The " +  a * 3 + " " +  b * 3 + " " + c
  "The " +  a * 3 + " " +  b * 3 + " " + c
</syntaxhighlight>
 
==End of Part 1==
==End of Part 1==


Line 303: Line 292:


So far we've been learning at the interactive '''Python interpreter'''. When you are working at the interpreter, any work that you do gets printed to the screen. For example:
So far we've been learning at the interactive '''Python interpreter'''. When you are working at the interpreter, any work that you do gets printed to the screen. For example:
<syntaxhighlight lang="python">
 
  h = "Hello"
  h = "Hello"
  w = "World"
  w = "World"
  h + w
  h + w
</syntaxhighlight>
 
will display "HelloWorld".
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 '''print''' function. Here's how it works:
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 '''print''' function. Here's how it works:
<syntaxhighlight lang="python">
 
  h = "Hello"
  h = "Hello"
  w = "World"
  w = "World"
Line 318: Line 307:
  my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
  my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
  print(my_string)
  print(my_string)
</syntaxhighlight>
 


The string manipulate is exactly the same as before. The only difference is that you need to use '''print''' to print results to the screen:
The string manipulate is exactly the same as before. The only difference is that you need to use '''print''' to print results to the screen:
Line 337: Line 326:


<ol>
<ol>
<li>Download the file http://mako.cc/teaching/2015/cdsw-spring/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>Download the file http://mako.cc/teaching/2014/cdsw/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 [[Community_Data_Science_Workshops_(Spring_2015)/Day_0_setup_and_tutorial#Goal_.234:_Practice_navigating_the_computer_from_a_terminal|navigating from a terminal]] for a refresher on those commands.</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 [[Community Data Science Workshops (Fall 2014)/Day 0 setup and tutorial#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
<li>Once you are in your Desktop directory, execute the contents of <code>nobel.py</code> by typing


Line 347: Line 336:
<code>nobel.py</code> introduces two new concepts: comments and multiline strings.</li>
<code>nobel.py</code> introduces two new concepts: comments and multiline strings.</li>


<li>Open <code>nobel.py</code> in your text editor (see [[Community_Data_Science_Workshops_(Spring_2015)/Day_0_setup_and_tutorial#Goal_.232:_Prepare_a_text_editor|preparing your text editor]] for a refresher on starting the editor).</li>
<li>Open <code>nobel.py</code> in your text editor (see [[Community Data Science Workshops (Fall 2014)/Day 0 setup and tutorial#Goal_.232:_prepare_a_text_editor|preparing your text editor]] for a refresher on starting the editor).</li>
<li>Read through the file in your text editor carefully and check your understanding of both the comments and the code.</li>
<li>Read through the file in your text editor carefully and check your understanding of both the comments and the code.</li>
</ol>
</ol>
Line 417: Line 406:
Use the '''<code>else</code>''' keyword, together with <code>if</code>, to execute different code when the <code>if</code> condition isn't <code>True</code>. Try this:
Use the '''<code>else</code>''' keyword, together with <code>if</code>, to execute different code when the <code>if</code> condition isn't <code>True</code>. Try this:


<syntaxhighlight lang="python">
<pre>
sister_age = 15
sister_age = 15
brother_age = 12
brother_age = 12
Line 424: Line 413:
else:
else:
     print("brother is older")
     print("brother is older")
</syntaxhighlight>
</pre>


Like with <code>if</code>, the code block under the <code>else</code> condition must be indented so Python knows that it is a part of the <code>else</code> block.
Like with <code>if</code>, the code block under the <code>else</code> condition must be indented so Python knows that it is a part of the <code>else</code> block.
Line 434: Line 423:
Try typing these out and see what you get:
Try typing these out and see what you get:


<syntaxhighlight lang="python">
<pre>
1 > 0 and 1 < 2
1 > 0 and 1 < 2
</syntaxhighlight>
</pre>


<syntaxhighlight lang="python">
<pre>
1 < 2 and "x" in "abc"
1 < 2 and "x" in "abc"
</syntaxhighlight>
</pre>


<syntaxhighlight lang="python">
<pre>
"a" in "hello" or "e" in "hello"
"a" in "hello" or "e" in "hello"
</syntaxhighlight>
</pre>


<syntaxhighlight lang="python">
<pre>
1 <= 0 or "a" not in "abc"
1 <= 0 or "a" not in "abc"
</syntaxhighlight>
</pre>


Guess what will happen when you enter these next two examples, and then type them out and see if you are correct. If you have trouble with the indenting, call over a staff member and practice together. It is important to be comfortable with indenting for tomorrow.
Guess what will happen when you enter these next two examples, and then type them out and see if you are correct. If you have trouble with the indenting, call over a staff member and practice together. It is important to be comfortable with indenting for tomorrow.


<syntaxhighlight lang="python">
<pre>
temperature = 32
temperature = 32
if temperature > 60 and temperature < 75:
if temperature > 60 and temperature < 75:
Line 458: Line 447:
else:
else:
     print("Too extreme for me.")
     print("Too extreme for me.")
</syntaxhighlight>
</pre>


<syntaxhighlight lang="python">
<pre>
hour = 11
hour = 11
if hour < 7 or hour > 23:
if hour < 7 or hour > 23:
Line 468: Line 457:
     print("Welcome to the cheese shop!")
     print("Welcome to the cheese shop!")
     print("Can I interest you in some choice gouda?")
     print("Can I interest you in some choice gouda?")
</syntaxhighlight>
</pre>


You can have as many lines of code as you want in <code>if</code> and <code>else</code> blocks; just make sure to indent them so Python knows they are a part of the block.
You can have as many lines of code as you want in <code>if</code> and <code>else</code> blocks; just make sure to indent them so Python knows they are a part of the block.
Line 476: Line 465:
If you need to execute code conditional based on more than two cases, you can use the '''<code>elif</code>''' 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.
If you need to execute code conditional based on more than two cases, you can use the '''<code>elif</code>''' 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.


<syntaxhighlight lang="python">
<pre>
sister_age = 15
sister_age = 15
brother_age = 12
brother_age = 12
Line 485: Line 474:
else:
else:
     print("brother is older")
     print("brother is older")
</syntaxhighlight>
</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>:
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>:


<syntaxhighlight lang="python">
<pre>
color = "orange"
color = "orange"
if color == "green" or color == "red":
if color == "green" or color == "red":
Line 497: Line 486:
elif color == "pink":
elif color == "pink":
   print("Valentine's Day color!")
   print("Valentine's Day color!")
</syntaxhighlight>
</pre>


If color had been "purple", that code wouldn't have printed anything.
If color had been "purple", that code wouldn't have printed anything.
Line 518: Line 507:


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>.
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>.
<syntaxhighlight lang="python">
 
  True
  True


Line 526: Line 515:


  type(False)
  type(False)
</syntaxhighlight>
 
You can test if Python objects are equal or unequal. The result is a boolean:
You can test if Python objects are equal or unequal. The result is a boolean:
<syntaxhighlight lang="python">
 
  0 == 0
  0 == 0


  0 == 1
  0 == 1
</syntaxhighlight>
 
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>.
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>.


Line 538: Line 527:


Use <code>!=</code> to test for inequality:
Use <code>!=</code> to test for inequality:
<syntaxhighlight lang="python">
 
  "a" != "a"
  "a" != "a"


  "a" != "A"
  "a" != "A"
</syntaxhighlight>
 
<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:
<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:
<syntaxhighlight lang="python">
 
  1 > 0
  1 > 0


Line 552: Line 541:


  .5 <= 1
  .5 <= 1
</syntaxhighlight>
 
You can check for containment with the <code>in</code> keyword, which also results in a boolean:
You can check for containment with the <code>in</code> keyword, which also results in a boolean:
<syntaxhighlight lang="python">
 
  "H" in "Hello"
  "H" in "Hello"


  "X" in "Hello"
  "X" in "Hello"
</syntaxhighlight>
 
Or check for a lack of containment with <code>not in</code>:
Or check for a lack of containment with <code>not in</code>:
<syntaxhighlight lang="python">
 
  "a" not in "abcde"
  "a" not in "abcde"


  "Perl" not in "Python Workshop"
  "Perl" not in "Python Workshop"
</syntaxhighlight>
 
==End of Part 2==
==End of Part 2==


Line 572: Line 561:


Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
[[Category:Spring_2015_series]]
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)