|
| 1 | +--- |
| 2 | +title: Additional Exercises |
| 3 | +teaching: 1 |
| 4 | +exercises: 0 |
| 5 | +questions: |
| 6 | +- "Practice your python." |
| 7 | +objectives: |
| 8 | +- "Work on some python exercises to continue building your skills." |
| 9 | +keypoints: |
| 10 | +- "Practice makes Python." |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +# Python Practice |
| 15 | + |
| 16 | +The key to building your skills as a programmer is _practice_. Try giving yourself challenges to use the Python tools we have learned in class. Here are some exercises for you to work through to continue working on becoming a Pythonista. |
| 17 | + |
| 18 | +## Python Datatypes |
| 19 | + |
| 20 | +1. Create a list in python `x = list(range(1,20))`. This will make a list of numbers from 1 to 20. Now try the following with this list: |
| 21 | + 1. Create a new list containing every 2nd element starting with element `x[1]`. |
| 22 | + 2. Print the last 8 values in the list to screen. |
| 23 | + 3. Write a `for` loop to print each value, one at a time. |
| 24 | + 4. Write a function that converts the entire list to a single string. |
| 25 | + |
| 26 | +2. Create 3 dictionaries: |
| 27 | +``` |
| 28 | +d1 = {'a': 10, 'b': 20, 'c': 30} |
| 29 | +d1 = {'d': 40, 'e': 50} |
| 30 | +d1 = {'f': 60, 'g': 70, 'h': 80} |
| 31 | +``` |
| 32 | + 1. Concatenate the dictionaries into one single dictionary called `my_dictionary` |
| 33 | + 2. Check to see if `my_dictionary` has the key `i`. |
| 34 | + 3. Change the value associated with key `d` to `40.4` |
| 35 | + |
| 36 | +## Math |
| 37 | + |
| 38 | +1. Use the Numpy package to draw 100 random variables from a normal distribution with mean = 5 and standard deviation = 1. Assign these to a list called `norm_rvs`. |
| 39 | +2. Use Numpy to compute the sum of the list `x` crated in the very first exercise. |
| 40 | +3. Write a function that takes the list `norm_rvs` and returns a list where each element is the `log(norm_rvs[i])`. |
| 41 | +4. Use the seaborn package to view histograms of `norm_rvs` and the log-transformed values. |
| 42 | + |
| 43 | +## Pandas |
| 44 | + |
| 45 | +1. Load the CSV file from our previous exercises: `surveys_df = pd.read_csv("surveys.csv")` |
| 46 | + 1. Create a new dataframe variable called `surveys1997` containing only the observations from the year 1997. |
| 47 | + 2. Use a `for` loop to iterate over `surveys1997` and print the plot number to screen. |
| 48 | + |
| 49 | + |
| 50 | +<br> |
| 51 | + |
| 52 | + |
0 commit comments