utm-coders / studyGroup

Welcome to the University of Toronto Coders!
https://utm-coders.github.io/studyGroup
Other
6 stars 1 forks source link

Update python lesson #7

Closed James-S-Santangelo closed 5 years ago

James-S-Santangelo commented 5 years ago
aays commented 5 years ago

Looks solid! I'd avoid spending too much time on tuples since I've found they confuse beginners without explicit instruction of how packing and unpacking them works (i.e. 'why would I ever want an immutable list?') but otherwise I think this will be great. It's worth mentioning that when I originally ran the lesson this was partially based on, it ran for about ~1 hr 15 min, so I think the added content should put it at right about 1.5 hrs.

Other practice questions:

  1. One of the best things about Python is the wealth of documentation and tutorials available online for anyone to refer to. With a bit of Googling and reading around, one can find a solution for nearly any Python problem or 'how do I do ___ in Python?' scenario. With this in mind, using our iris data frame object, find a way to create a new column called Sepal Area that is the product of Sepal Length and Sepal Width. (Hint: Search results from a website called Stack Overflow are usually a good place to look!)
  2. There exists a method for string objects that allows users to replace all instances of a given character with something else. Try to find this method on the Internet and see if you can complete question 2 above with it.
  3. A key part of making functions extra powerful and usable is ensuring they handle 'wrong inputs' instead of just blindly ignoring them or breaking. Write a function called filter_list() that takes in two arguments -- a list of integers and a cutoff value -- and returns a list that only contains values greater than the cutoff. However, if your function is provided a list that contains even one instance of something that's not an integer, have it print out some sort of error message instead. Remember to remind users what the correct inputs are in your function's docstring! (Note: mastering error handling is a key part of becoming a more versatile Python programmer. If you'd like to have a more advanced look at error handling, this tutorial is a useful reference)
  4. a) The numpy function np.random.random_sample() returns a decimal value between 0 and 1. Use this to write a function called coin_toss() that performs a fair coin toss and returns 'heads' or 'tails'. b) Use your coin_toss function to perform 1000 coin tosses and save their results to a list called coin_tosses. Hint: Lists have a helpful method called .append() that will modify a list by adding specified input to it (i.e. if cars is a list, cars.append('toyota') will add 'toyota' to the list. .append is a special method in that you do not need to perform a variable assignment, such as cars = cars.append('toyota'), to make it work) c) Lists also have a method called .count() that will count instances of a given input. Use .count() to count how many heads and tails were flipped in your coin_tosses list. Do the values approach what you would expect? What if you go back and modify your coin_toss function to bias the coin and then perform your 1000 tosses?
  5. a) Similarly, the numpy function np.random.randint() returns an array of randomly drawn values from a specified range. For instance, np.random.randint(0, 10, size=5) will return 5 values between 0 and 9 (the higher value is exclusive). Use this to create a function that performs n die rolls called roll_dice(). b) The seaborn function sns.distplot is used to plot histograms. Use your roll_dice() function to create an array of 1000 dice rolls, and find a way to plot them using sns.distplot.