Community Data Science Course (Spring 2023)/Week 2 lecture notes: Difference between revisions

From CommunityData
(Created page with "== Resources == * Python data types cheat sheet * Python loops cheat sheet == Lecture outline == === Review Week 1 python material === * math: using python as a calculator **addition, subtraction, multiplication, division **division shows something different: <code>8/2</code> versus <code>2*2</code> * <tt>type()</tt> ** there are different types of things in python (called objects) ** variables that "know about the decimal place" (int) and variables that don't...")
 
 
(7 intermediate revisions by the same user not shown)
Line 42: Line 42:
** e.g., adding elif: fix the bug in the previous program if they were the same age
** e.g., adding elif: fix the bug in the previous program if they were the same age
** indent with spaces (we use 4 spaces!)
** indent with spaces (we use 4 spaces!)
===Exercise:===
The code below lets a user enter their age, and it prints their age back to the screen.
1) Copy the code to a file and run it.
2) Modify the file to take the user's age and do the following:
a) If the age entered is younger than your age, print <code>You are younger than me!</code>
b) If the age entered is older than your age, print <code>You are older than me!</code>
c) If the age entered is your age, print <code>You are the same age as me!</code>
    input_age = input("Enter your age in years: ")
    print("User entered " + input_age)


=== Lists ===
=== Lists ===
Line 96: Line 78:
** e.g., <code>min()</code> and <code>max()</code>
** e.g., <code>min()</code> and <code>max()</code>
** e.g., create a list of names and sort it <code>names.sort()</code>
** e.g., create a list of names and sort it <code>names.sort()</code>
** e.g., reverse a list with <code>names.reverse()</code>
** search "python lists" in any search engine to see many more functions that list knows about.
** search "python lists" in any search engine to see many more functions that list knows about.


===Exercise===
=== Optional Exercise ===


Figure out what the list function <code>pop</code> does. Confirm with your neighbor. Write an example to be sure.
Figure out what the list function <code>pop</code> does. Confirm with your neighbor. Write an example to be sure.
Line 114: Line 97:
** show we can test things outside the loop to show how the comparisons are working
** show we can test things outside the loop to show how the comparisons are working
** add an else statement to capture words that start with a consonant
** add an else statement to capture words that start with a consonant
** append to a list within a for loop
** create a counter within a for loop (keep track)
** create a counter within a for loop (keep track)
* nested <tt>for</tt> loops
* <tt>range()</tt>
* <tt>while</tt> loops
* infinite loops and how to stop them
* <tt>if</tt> statements inside <tt>while</tt> loops
* <tt>break</tt>

Latest revision as of 01:24, 5 April 2023

Resources[edit]

Lecture outline[edit]

Review Week 1 python material[edit]

  • math: using python as a calculator
    • addition, subtraction, multiplication, division
    • division shows something different: 8/2 versus 2*2
  • type()
    • there are different types of things in python (called objects)
    • variables that "know about the decimal place" (int) and variables that don't (floats)
    • variables that are strings.
  • variables
    • assignment of variables
    • e.g., math with variables: scale up a recipe, into an assignment
    • you can assign to a variable and it will replace the old value
  • strings
    • things within quotation marks
    • adding strings with "concatenation" (smushing things together)
    • e.g., print("Hello" + name)
    • concatenating strings and integers doesn't work (e.g., print(1 + "tommy"))
    • 1 is different than "1"; name is different than "name"
    • convert "1" to 1 with the int() function
    • single quotes versus double quotes (python doesn't care)
    • you can also multiply strings! (although it's not clear why you want to)
    • The type of a variable tells you what you can do with it
  • Booleans
    • comparisons (e.g., 1 == 1 or 1 == 0)
      • you can compare strings (case sensitive!)
      • also >, <, and !=
    • type() shows that the output of True or False is bool
    • e.g., "i" in "team"
    • e.g., "i" not in "team"
  • if/elif/else (move to external file)
    • if, something that evaluates to a Boolean, and then colon
    • e.g., if "tom" in "tommyguy"
    • e.g., adding else example: if brother_age > sister_age
    • e.g., temperature range
    • e.g., adding elif: fix the bug in the previous program if they were the same age
    • indent with spaces (we use 4 spaces!)

Lists[edit]

  • purpose
    • Stores things in order
  • initialization
    • making a list called my list: my_list = ["a", "b", "c"]
    • comma separated elements. in python they can be a mix of any kind of types
    • type(my_list)
  • len() review
  • accessing elements
    • indexing like my_list[0]
    • indexing starts from the front and we start counting at 0
    • we go from the end with negative numbers
    • what happens if we try to move outside of the range? ('error!)
  • adding elements
    • using the the my_list.append() function
    • the .append() function is a special kind of function that lists know about
    • compare to print which is a global function.
  • changing elements
    • replacing elements like my_list[0] = "foo"
  • finding elements in list
    • e.g., "z" in my_list
  • slicing lists
    • the colon inside the [] is the slicing syntax
    • e.g., my_list[0:2] is 0th up to, but not including, the 2nd
    • e.g., my_list[2:]
    • e.g., my_list[:2]
    • e.g., my_list[:]
  • strings are like lists
    • we can slice lists
    • len()
      • len("") length of the empty string
  • many other interesting functions for lists
    • e.g., min() and max()
    • e.g., create a list of names and sort it names.sort()
    • e.g., reverse a list with names.reverse()
    • search "python lists" in any search engine to see many more functions that list knows about.

Optional Exercise[edit]

Figure out what the list function pop does. Confirm with your neighbor. Write an example to be sure.

loops and more flow control[edit]

  • for loops
    • e.g., for name in names: print name
    • e.g., for name in names: print 'hello ' + name
    • Super powerful because it can do something many many times. Data science is about doing tedious things very quickly. for is the workhorse that makes this possible.
    • Look and see what name is after we're done looping.
    • Move to editor.
  • if statements inside for loops
    • e.g., if name[0] in "AEIOU" then print "starts with a vowel"
    • show we can test things outside the loop to show how the comparisons are working
    • add an else statement to capture words that start with a consonant
    • create a counter within a for loop (keep track)