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
Download the pr05.py file.
Implement the body of the
avg(any_list)function. Feel free to re-use your code from last's week exercise! In the
main()function add the call to your
avgfunction to see if it calculates the average daily temperatures correctly (the answer is around 72.9). Your main function will look something like that:
avg_daily = avg(TEMP_DAY)
print('Average daily temperature in September 2019 was', avg_daily, 'Fahrenheit')
main()function and that function will call your
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:
monthsand that is a string with a value of
'Jan', the first element can be accessed by writing
monthsand 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
'Nov', and so on.
Apart from accessing single elements from a list, we can also access ranges of elements like
end_index is not inclusive. Sticking to our months example, we can get the list of months in the first quarter by writing
When we omit the
start_index, the result will span from the beginning of the list until
end_index, for example
Similarly, when we omit the
end_index, the result will span from the
start_index to the end (including the last element):
We can combine it with negative indexing, for example:
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:
Was the second or the third week of the month warmer during days?
Were the nights during the last week warmer than the nights during the first week?
Was the first half of the month colder than the second half during the days?
For the nights of each week check if they were on average warmer or colder than the full month nightly average (try using the
forloop with an index for the week in
SortingWe use the
sorted()function, to order lists, like this:
sorted_temperatures = sorted(temperatures)
Excercise 3: Extreme temperatures
sorted() function and your new slicing abilities to answer the following questions:
What were the temperatures on the five coldest September nights?
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:
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.
avg2d(any_2dlist)function that takes a two-dimensional array as an argument and calculates the average of the whole array.
This function will create an empty list, then use a
forloop to calculate the average value of each row (using the
avg(any_list)function) and append it to that list. At the end it uses the
avg(any_list)function to calculate the average of that new list and returns it.
main()to answer the following questions:
What is the anual average temperature in Boston?
What is the difference between average temperatures in the spring (April, May, June) and in the summer (July, Aug, Sep)? (use slicing!)