Using Arrays

 


Introduction

The purpose of this mini-lab is to practice creating and working with arrays in Python to process temperature data.



Exercises

  1. Create a new file for the statements and functions you will write in this mini-lab. Then save the new file with a name representative of this minilab.
  2. Immediately after your header comments (name, date, program description) for this file, add the following statement to import the NumPy module:
    import numpy as np
    This statement will allow us to use the functions in this module.
  3. In this minilab, you will be exploring high and low temperatures in Kalamazoo during a given month over several years. Decide if you would like to explore temperatures from January or August. Then, using data from the files below, create 5 1-dimensional arrays of high temperatures, corresponding to the 5 different years of high temperatures for the month you chose. Then create another set of 5 1-dimensional arrays for the low temps for each of the 5 years.
    Files with temperature data:
  4. Wrtite a function called avgTemp that takes an array of temperature values as a parameter and calculates the average value of the elements in the array. To calculate the average, your function should add up all of the array values and then divide by the number of elements in the array. (Note: You should not assume your array has 31 values, but rather, use the len of the array to get the size.) Your function should then return the average value.
  5. Test your function by calling it with one of your temperature arrays and printing the result. You may have a statement such as print("The average high temp from Aug 2021 is: ", avgTemp(aug2021HighArray)). Double check your results by calculating the average with a calculator.
  6. Print out the average temperatures for each of your arrays.
  7. Write a function called medianTemp that takes an array of temperature values as a parameter and returns the median value of the array. To find the median value, you should first sort the array (use the sort function from NumPy), and then return the middle element of the sorted array.
  8. Print the median values for each of your temperature arrays.
  9. Write a function called maxTemp that takes an array of temperatures as a parameter and calculates the maximum value of the array. It should return the maximum value. To calculate the maximum value, create a variable, call it maxValue, that gets initialized to the first value in the array. Then use a for loop to go through the elements in the array. Inside the loop, you should use an if statement to compare the current array element with what you have for the maxValue so far. Update maxValue if the current array element is larger. When the loop is finished, return the maximum value.
  10. Print out the maximum temperature for each of the 5 years of monthly data that you have, for your low temperatures as well as your high temperatures. This means you will need to call the maxTemp function with each of your arrays and print the value that get returned. Include text with your printed statement to indicate what year you are reporting on, as in print("Max low temp for the month of August in the year 2005 is: " , maxTemp(aug2005LowArray)).
  11. Write a function called minTemp that returns the minimum value from an array of temperature values. The code for this function will be quite similar to that for the maxTemp function.
  12. Print out the minimum temperatures for each of your arrays, similar to what you did for the maximum temperatures.
  13. Write a function called numGreaterThan that takes an array of temperature values and a specific value as parameters, and returns the number of temperature values that are greater than the specifed value. To do this, create a variable called count that is initially 0. Loop through the array and check if each value is greater than the specified value. If it is, increment count by 1. Return the count at the end of the function.
  14. If you are using August data, test your function by calling it with one of your high temp arrays and the value of 90. (This should give you the number of days with highs over 90 degrees.) You can compare your answer to the data provided on the file with the temperatures. If you are using January data, test the function by calling it with one of your low temp arrays and the value of 32. (This should give you the number of days with low temps over 32 degrees.)
  15. For each of your high temperature arrays, print out the number of temperatures above 90 degrees. For each of your low temperature arrays, print out the number of temperatures above 32 degrees.
  16. Write a similar function, numLessThan that takes an array of temperature values and a specific value as parameters, and returns the number of temperature values that are less than the specified value.
  17. For each of your high temperature arrays, print both the number of temperatures less than 90 degrees and the number of temperatures less than 32 degrees. For each of your low temperature arrays, print both the number of temperatures less than 32 degrees and the number of temperatures less than 0 degrees.
  18. (Exploratory, nothing to submit.) Are there other values you are interested in learning? Can you use the functions you have written to determine these values or would you need to write some new functions?

Connections

  1. Think about how the average temperatures you calculated compare with the averages published for Kalamazoo. January weather, August weather
  2. Read the following article: Why Record-Breaking Overnight Temperatures Are So Concerning.
  3. Write several paragraphs that discuss any trends you saw in your monthly data over the five years that you looked at, and how that does or does not correspond to current trends, and what is discussed in the article. You should write this in a Word document, text file, or pdf to submit.

Submit