{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.1 - Getting started with Python\n", "\n", "The goal of this exercise is to print text like that below to the screen based on variable values you define.\n", "\n", "```\n", "My name is Dave and I give eating ice cream a score of 9 out of 10!\n", "My sleeping enjoyment rating is 8 / 10!\n", "Based on the factors above, my happiness rating is 8.5 out of 10 or 85.0 %!\n", "```\n", "For this exercise you should:\n", "\n", "- Create three variables: one integer value (whole number) for your ice cream rating, another integer for your sleeping rating, and a character string for your first name. \n", "- Calculate the average of the two ratings. Store the result in a variable for your happiness.\n", "- Find the data type of each of the variables you have defined. Are there any surprises?\n", "- Reproduce your version of the example text using the `print()` function and the variables you have defined. Note that you may need to be careful combining text and numbers!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.2 - Creating and changing lists\n", "\n", ": _**Table 2.5**. [FMI observation stations](http://en.ilmatieteenlaitos.fi/observation-stations) and the years in which they began operating_.\n", "\n", "|FMI station name | First year of operation | Group |\n", "|:----------------------|:-----------------------:|:-----:|\n", "|lighthouse | 2003 | 1 |\n", "|Harmaja | 1989 | 1 |\n", "|Suomenlinna aaltopoiju | 2016 | 1 |\n", "|Kumpula | 2005 | 1 |\n", "|Kaisaniemi | 1844 | 1 |\n", "|Malmi airfield | 1937 | 2 |\n", "|Vuosaari harbour | 2012 | 2 |\n", "|Kaivopuisto | 1904 | 2 |\n", "\n", "For this exercise you should use the data in Table 2.5 to:\n", "\n", "- Create two lists for the station names and first years of operation for the stations in Group 1 in Table 2.5. Be sure to list the values in the order they appear in the table.\n", "- Modify the lists you just created to add the values Group 2, again keeping them in the same order.\n", "- Sort the two lists, sorting the first in alphabetical order and the second so that the most recent starting year is first\n", " - Do you see any problems with how the lists have been sorted?\n", " - Python has a function called `zip()` that might be helpful in solving the sorting issue. Search online to see whether you can find a way to use `zip()` to solve the problem with the two lists." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.3 - Lists and index values\n", "\n", ": _**Table 2.6**. [Monthly average temperatures recorded at the Helsinki Malmi airport](https://www.timeanddate.com/weather/finland/helsinki/climate)._\n", "\n", "|Month | Temperature (°C) |\n", "|:---------|:----------------:|\n", "|January | -3.5 |\n", "|February | -4.5 |\n", "|March | -1.0 |\n", "|April | 4.0 |\n", "|May | 10.0 |\n", "|June | 15.0 |\n", "|July | 18.0 |\n", "|August | 16.0 |\n", "|September | 11.5 |\n", "|October | 6.0 |\n", "|November | 2.0 |\n", "|December | -1.5 |\n", "\n", "For this exercise you should use the data in Table 2.6 to:\n", "\n", "- Create two lists with the months and their average temperatures.\n", "- Use a print statement to produce output like that below, where the months and temperatures are selected using index values in the corresponding lists.\n", "\n", "```\n", "The average temperature in Helsinki in March is -1.0\n", "```" ] }, { "cell_type": "markdown", "metadata": { "lines_to_next_cell": 0 }, "source": [ "## Exercise 2.4 - Batch processing files with a `for` loop\n", "\n", "Batch processing is a common task in Python, where a set of data and/or files are analyzed one after another using the same script or program. In this exercise your goal is to produce a Python list of filenames that could be used to batch process the data they contain.\n", "\n", "For this exercise you should:\n", "\n", "- Use a `for` loop to create a Python list that produces the output below when you print the list:\n", "\n", " ```\n", " ['Station_0.txt', 'Station_1.txt', 'Station_2.txt', 'Station_3.txt',\n", " 'Station_4.txt', 'Station_5.txt', 'Station_6.txt', 'Station_7.txt',\n", " 'Station_8.txt', 'Station_9.txt', 'Station_10.txt', 'Station_11.txt',\n", " 'Station_12.txt', 'Station_13.txt', 'Station_14.txt', 'Station_15.txt',\n", " 'Station_16.txt', 'Station_17.txt', 'Station_18.txt', 'Station_19.txt',\n", " 'Station_20.txt']\n", " ```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.5 - Classifying temperatures\n", "\n", "Data classification is another useful data analysis concept, where data values are sorted into different groups that help you to interpret the data. Your goal in this exercise is to sort a list of temperatures into four categories using a Python list for each category (Table 2.7):\n", "\n", ": _**Table 2.7**. Temperature categories and ranges for Exercise 2.5 and 2.7._\n", "\n", "|Category |Temperature range | List name |\n", "|:-----------|:----------------------------------|:-------------:|\n", "|Cold |Less than -2 deg. C | `cold` |\n", "|Slippery |Greater than or equal to -2 deg. C | `slippery` |\n", "| |and less than +2 deg. C | |\n", "|Comfortable |Greater than or equal to +2 deg. C | `comfortable` |\n", "| |and less than +15 deg. C | |\n", "|Warm |Greater than or equal to +2 deg. C | `warm` |\n", "\n", "The list of temperatures below were measured at the Helsinki Malmi Airport in April 2013 with night, day, and evening temperatures recorded for each day.\n", "\n", "```python\n", "temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4, 4.0, -2.2, -3.9, 4.4,\n", " -2.5, -4.6, 5.1, 2.1, -2.4, 1.9, -3.3, -4.8, 1.0, -0.8, -2.8,\n", " -0.1, -4.7, -5.6, 2.6, -2.7, -4.6, 3.4, -0.4, -0.9, 3.1, 2.4,\n", " 1.6, 4.2, 3.5, 2.6, 3.1, 2.2, 1.8, 3.3, 1.6, 1.5, 4.7, 4.0,\n", " 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8,\n", " 4.0, 8.6, 4.1, 1.4, 8.9, 3.0, 1.6, 8.5, 4.7, 6.6, 8.1, 4.5,\n", " 4.8, 11.3, 4.7, 5.2, 11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6,\n", " -0.0, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6, 5.2]\n", "```\n", "\n", "For this exercise you should:\n", "\n", "- Use a `for` loop and conditional statments (e.g., `if`, `elif`, and `else`) to sort the temperatures in the list into the lists associated with each category (see Table 2.7).\n", " - *Hint*: Create the empty lists before the start of the `for` loop.\n", "- Answer the following questions:\n", " - How many times was it cold in Helsinki in April 2013?\n", " - How many times was it comfortable?\n", " - Was it ever warm?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.6 - A temperature conversion function\n", "\n", "Functions are commonly used for small calculations that occur frequently within a program, such as converting between units.\n", "\n", "For this problem you should:\n", "\n", "- Create a function to converts temperature in degrees Fahrenheit to degrees Celsius.\n", " - Be sure to include a docstring in the function definition.\n", "- Use the new function to convert the temperatures below to Celsius:\n", " - 32 °F\n", " - 68 °F\n", " - 91 °F\n", " - -17 °F" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.7 - A temperature classifier function\n", "\n", "This exercise uses the same logic presented in Exercise 2.5 to classify temperatures, but now using a function.\n", "\n", "In this exercise you should:\n", "\n", "- Create a function that classifies temperatures based on Table 2.8.\n", " - The function should return the following values for the different categories\n", " - `0`: Cold\n", " - `1`: Slippery\n", " - `2`: Comfortable\n", " - `3`: Warm\n", "- Use the function to calculate the returned value the following temperatures:\n", " - +17 °C\n", " - +2 °C\n", " - +1.9 °C\n", " - -2 °C" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 4 }