Home Practicum 1 Practicum 2 Practicum 3 Practicum 4 Practicum 5

Welcome to Practicum 5!

Today we continue practicing loops and lists, this time working with real data. We start with weather data: daily and nightly temperatures in Boston during September 2019, and then we'll move to climate data: monthly lows and highs aggregated over the last 10 years.

In the exercise starter file there are a few functions whose definitions are empty and you'll need to implement them. Do not modify the argument lists of the functions, only implement their bodies.

Excercise 1: Warm up

  1. Download the pr05.py file.
  2. Implement the body of the avg(any_list) function. Feel free to re-use your code from last's week exercise!
  3. In the main() function add the call to your avg function to see if it calculates the average daily temperatures correctly (the answer is around 72.9). Your main function will look something like that:
    def main():
        avg_daily = avg(TEMP_DAY)
        print('Average daily temperature in September 2019 was', avg_daily, 'Fahrenheit')
  4. Run your code. Python will call the main() function and that function will call your avg function.

List indexing

Remeber, we can access elements in a list using an index (starting at 0!) inside of square brackets, like list_variable[index], for example in a list of moths:

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
we access the zeroth element by writing months[0] and that is a string with a value of 'Jan', the first element can be accessed by writing months[1] and it's a string with a value of 'Feb' and so on.

The first new thing we'll learn today is indexing counting down from the end of the array. We can access the last element of the array (without caring about it's length), using the -1 index. In our months example list, writing months[-1] will give us 'Dec', whereas months[-2] gives 'Nov', and so on.


Apart from accessing single elements from a list, we can also access ranges of elements like list_variable[start_index:end_index], the end_index is not inclusive. Sticking to our months example, we can get the list of months in the first quarter by writing

That returns a list that contains the first three monhts:
['Jan', 'Feb', 'Mar']

When we omit the start_index, the result will span from the beginning of the list until end_index, for example

['Jan', 'Feb', 'Mar']

Similarly, when we omit the end_index, the result will span from the start_index to the end (including the last element):

['Oct', 'Nov', 'Dec']

We can combine it with negative indexing, for example:

returns the last three monhts
['Oct', 'Nov', 'Dec']
returns months from the third to the third from the end (exclusive):
['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep']

Excercise 2: Average temperatures

In this exercise we will use your avg(any_list) function to learn a few things about the temperatures in September.

Do not modify the avg(any_list) function to answer the questions. Instead call it in the main() function with parameters that will let you answer the following questions:

  1. Was the second or the third week of the month warmer during days?
  2. Were the nights during the last week warmer than the nights during the first week?
  3. Was the first half of the month colder than the second half during the days?
  4. For the nights of each week check if they were on average warmer or colder than the full month nightly average (try using the for loop with an index for the week in range(0, 4))


We use the sorted() function, to order lists, like this:
temperatures = [70, 75, 74, 84, 66]
sorted_temperatures = sorted(temperatures)
will print:
[66, 70, 74, 75, 84]

Excercise 3: Extreme temperatures

Use the sorted() function and your new slicing abilities to answer the following questions:

  1. What were the temperatures on the five coldest September nights?
  2. What were the temperatures on the 7 hottest September days?

Exercise 4: Averages in two dimensions

In this exercise we practice the new skills on a two dimensional dataset. The TEMP_MONTHS is a list of lists that we can think about as a two-dimensional array: each row represents a month, and the columns contain the average low and high temperatures:

TEMP_MONTHS = [[23, 37],
    [24, 38],
    [31, 45],
    [41, 56],
    [50, 66],
    [59, 76],
    [66, 82],
    [65, 80],
    [58, 73],
    [47, 62],
    [38, 52],
    [29, 42]]

For example, January has the average lows of 24 and average highs of 38. Let's assume that means January has the average temperature of 31 degrees.

  1. Create a avg2d(any_2dlist) function that takes a two-dimensional array as an argument and calculates the average of the whole array.
    1. This function will create an empty list, then use a for loop to calculate the average value of each row (using the avg(any_list) function) and append it to that list.
    2. At the end it uses the avg(any_list) function to calculate the average of that new list and returns it.
  2. Call your newly created function in the main() to answer the following questions:
    1. What is the anual average temperature in Boston?
    2. What is the difference between average temperatures in the spring (April, May, June) and in the summer (July, Aug, Sep)? (use slicing!)