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 36: Line 36:
</syntaxhighlight>
</syntaxhighlight>
===Division===
===Division===
First, lets try some basic fractions:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
4 / 2
  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:
<syntaxhighlight lang="python">
1.0 / 2
</syntaxhighlight>
</syntaxhighlight>
 
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:
Sure enough, Python says that the answer is 0.5 which is the same as 1/2.
<syntaxhighlight lang="python">
 
3/4 + 1/4
Let's try the same thing:
</syntaxhighlight>
 
What do you need to do to get the right answer? Use data types that understand decimals for each of the divisions:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  4 / 2
  3.0/4 + 1.0/4
3.0/4.0 + 1.0/4.0
</syntaxhighlight>
</syntaxhighlight>
 
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".
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'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.
 
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.
 
 
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 196: Line 191:
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 337: Line 330:


<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 340:
<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 572: Line 565:


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)