https://wiki.communitydata.science/index.php?title=Community_Data_Science_Course_(Spring_2019)/Day_2_Lecture&feed=atom&action=historyCommunity Data Science Course (Spring 2019)/Day 2 Lecture - Revision history2024-03-29T07:07:34ZRevision history for this page on the wikiMediaWiki 1.38.4https://wiki.communitydata.science/index.php?title=Community_Data_Science_Course_(Spring_2019)/Day_2_Lecture&diff=135221&oldid=prevGuyrt: /* Exercise: */2019-04-11T02:11:35Z<p><span dir="auto"><span class="autocomment">Exercise:</span></span></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 03:11, 11 April 2019</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l54">Line 54:</td>
<td colspan="2" class="diff-lineno">Line 54:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>1) Copy the code to a file and run it. </div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>1) Copy the code to a file and run it. </div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>2) Modify the file to take the user's age and do the following:</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>2) Modify the file to take the user's age and do the following:</div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>a) If the age entered is younger than your age, print <code>You are younger than me!</code></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>a) If the age entered is younger than your age, print <code>You are younger than me!</code></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>b) If the age entered is older than your age, print <code>You are older than me!</code></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>b) If the age entered is older than your age, print <code>You are older than me!</code></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>c) If the age entered is your age, print <code>You are the same age as me!</code></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>c) If the age entered is your age, print <code>You are the same age as me!</code></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l62">Line 62:</td>
<td colspan="2" class="diff-lineno">Line 66:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> input_age = input("Enter your age in years: ")</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> input_age = input("Enter your age in years: ")</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> print("User entered " + input_age)</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> print("User entered " + input_age)</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2" class="diff-side-added"></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== Lists ===</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== Lists ===</div></td></tr>
<!-- diff cache key wiki_communitydata:diff::1.12:old-134393:rev-135221 -->
</table>Guyrthttps://wiki.communitydata.science/index.php?title=Community_Data_Science_Course_(Spring_2019)/Day_2_Lecture&diff=134393&oldid=prevGuyrt: lecture notes2019-04-07T00:43:05Z<p>lecture notes</p>
<p><b>New page</b></p><div>== Resources ==<br />
<br />
* [[Python data types cheat sheet]]<br />
* [[Python loops cheat sheet]]<br />
<br />
== Lecture outline ==<br />
<br />
=== Review: ways to access python ===<br />
<br />
* python<br />
* python file.py<br />
<br />
=== Review Week 1 python material ===<br />
<br />
* math: using python as a calculator<br />
**addition, subtraction, multiplication, division<br />
**division shows something different: <code>8/2</code> versus <code>2*2</code><br />
* <tt>type()</tt><br />
** there are different types of things in python (called objects)<br />
** variables that "know about the decimal place" (int) and variables that don't (floats)<br />
** variables that are strings.<br />
* variables<br />
** assignment of variables<br />
** e.g., math with variables: scale up a recipe, into an assignment<br />
** you can assign to a variable and it will replace the old value<br />
* strings<br />
** things within quotation marks<br />
** adding strings with "concatenation" (smushing things together)<br />
** e.g., <code>print("Hello" + name)</code><br />
** concatenating strings and integers doesn't work (e.g., <code>print(1 + "tommy")</code>)<br />
** 1 is different than "1"; name is different than "name"<br />
** convert "1" to 1 with the <code>int()</code> function<br />
** single quotes versus double quotes (python doesn't care)<br />
** you can also multiply strings! (although it's not clear why you want to)<br />
** '''The type of a variable tells you what you can do with it'''<br />
* Booleans<br />
** comparisons (e.g., <code>1 == 1</code> or <code>1 == 0</code>)<br />
*** you can compare strings (case sensitive!)<br />
*** also >, <, and !=<br />
** type() shows that the output of True or False is <code>bool</code><br />
** e.g., <code>"i" in "team"</code><br />
** e.g., <code>"i" not in "team"</code><br />
* <tt>if</tt>/<tt>elif</tt>/<tt>else</tt> ('''move to external file''')<br />
** if, something that evaluates to a Boolean, and then colon<br />
** e.g., <code>if "tom" in "tommyguy"</code><br />
** e.g., adding else example: <code>if brother_age > sister_age</code><br />
** e.g., temperature range<br />
** e.g., adding elif: fix the bug in the previous program if they were the same age<br />
** indent with spaces (we use 4 spaces!)<br />
<br />
===Exercise:===<br />
<br />
The code below lets a user enter their age, and it prints their age back to the screen.<br />
<br />
1) Copy the code to a file and run it. <br />
2) Modify the file to take the user's age and do the following:<br />
a) If the age entered is younger than your age, print <code>You are younger than me!</code><br />
b) If the age entered is older than your age, print <code>You are older than me!</code><br />
c) If the age entered is your age, print <code>You are the same age as me!</code><br />
<br />
<br />
input_age = input("Enter your age in years: ")<br />
print("User entered " + input_age)<br />
<br />
<br />
=== Lists ===<br />
<br />
* purpose<br />
** Stores things ''in order''<br />
* initialization<br />
** making a list called my list: <code>my_list = ["a", "b", "c"]</code><br />
** comma separated elements. in python they can be a mix of any kind of types<br />
** <code>type(my_list)</code><br />
* <tt>len()</tt> review<br />
* accessing elements<br />
** indexing like my_list[0]<br />
** indexing starts from the front and we ''start counting at 0''<br />
** we go from the end with negative numbers<br />
** what happens if we try to move outside of the range? ('''error!'')<br />
* adding elements<br />
** using the the <code>my_list.append()</code> function<br />
** the <code>.append()</code> function is a special kind of function that lists know about<br />
** compare to <code>print</code> which is a global function.<br />
* changing elements<br />
** replacing elements like <code>my_list[0] = "foo"</code><br />
* finding elements in list<br />
** e.g., <code>"z" in my_list</code><br />
* slicing lists<br />
** the colon inside the [] is the ''slicing syntax''<br />
** e.g., <code>my_list[0:2]</code> is 0th up to, but not including, the 2nd<br />
** e.g., <code>my_list[2:]</code> <br />
** e.g., <code>my_list[:2]</code><br />
** e.g., <code>my_list[:]</code><br />
* strings are like lists<br />
** we can slice lists<br />
** len()<br />
*** <code>len("")</code> length of the empty string<br />
* many other interesting functions for lists<br />
** e.g., <code>min()</code> and <code>max()</code><br />
** e.g., create a list of names and sort it <code>names.sort()</code><br />
** search "python lists" in any search engine to see many more functions that list knows about.<br />
<br />
===Exercise===<br />
<br />
Figure out what the list function <code>pop</code> does. Confirm with your neighbor. Write an example to be sure.<br />
<br />
=== loops and more flow control ===<br />
<br />
* <tt>for</tt> loops<br />
** e.g., <code>for name in names: print name</code><br />
** e.g., <code>for name in names: print 'hello ' + name</code><br />
** Super powerful because it can do something many many times. Data science is about doing tedious things very quickly. <code>for</code> is the workhorse that makes this possible.<br />
** Look and see what name is after we're done looping.<br />
** ''Move to editor.''<br />
* <tt>if</tt> statements inside <tt>for</tt> loops<br />
** e.g., <code>if name[0] in "AEIOU"</code> then print "starts with a vowel"<br />
** show we can test things outside the loop to show how the comparisons are working<br />
** add an else statement to capture words that start with a consonant<br />
** append to a list within a for loop<br />
** create a counter within a for loop (keep track)<br />
* nested <tt>for</tt> loops<br />
* <tt>range()</tt><br />
* <tt>while</tt> loops<br />
* infinite loops and how to stop them<br />
* <tt>if</tt> statements inside <tt>while</tt> loops<br />
* <tt>break</tt></div>Guyrt