{ "cells": [ { "cell_type": "markdown", "id": "f51de3ce-80e3-48eb-9326-94ea63751058", "metadata": {}, "source": [ "# Exercise solutions" ] }, { "cell_type": "markdown", "id": "36d42ce0-a651-4615-9030-d4fe9a607656", "metadata": {}, "source": [ "## Chapter 2\n", "\n", "### Exercise 2.1\n", "\n", "```python\n", "# Define main variables\n", "ice_cream_rating = 9\n", "sleep_rating = 8\n", "name = \"Dave\"\n", "\n", "# Calculate happiness\n", "happiness_rating = (ice_cream_rating + sleep_rating) / 2\n", "\n", "# Print variable types\n", "print(type(ice_cream_rating))\n", "print(type(sleep_rating))\n", "print(type(name))\n", "print(type(happiness_rating))\n", "\n", "# Print formatted text\n", "print(\"My name is\", name, \"and I give eating ice cream a score of\", ice_cream_rating, \"out of 10!\")\n", "print(\"My sleeping enjoyment rating is\", sleep_rating, \"/ 10!\")\n", "print(\"Based on the factors above, my happiness rating is\", happiness_rating, \"or\", happiness_rating * 10, \"%!\")\n", "```\n", "\n", "### Exercise 2.2\n", "\n", "```python\n", "# Define left table lists\n", "station_names = [\"lighthouse\", \"Harmaja\", \"Suomenlinna aaltopoiju\", \"Kumpula\", \"Kaisaniemi\"]\n", "station_start_years = [2003, 1989, 2016, 2005, 1844]\n", "\n", "# Add values from table on the right\n", "station_names.append(\"Malmi airfield\")\n", "station_names.append(\"Vuosaari harbour\")\n", "station_names.append(\"Kaivopuisto\")\n", "station_start_years.append(1937)\n", "station_start_years.append(2012)\n", "station_start_years.append(1904)\n", "\n", "# Sort lists as directed\n", "station_names.sort()\n", "station_start_years.sort()\n", "station_start_years.reverse()\n", "\n", "# Problem: List values are no longer correctly linked for same index value\n", "```\n", "\n", "### Exercise 2.3\n", "\n", "```python\n", "# Create lists of months and temperatures\n", "months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', \\\n", " 'September', 'October', 'November', 'December']\n", "average_temp = [-3.5, -4.5, -1.0, 4.0, 10.0, 15.0, 18.0, 16.0, 11.5, 6.0, 2.0, -1.5]\n", "\n", "# Select month and create print statement output\n", "selected_month_index = 6\n", "print_statement = f\"The average temperature in Helsinki in {months[selected_month_index]} is {average_temp[selected_month_index]}\"\n", "\n", "# Alternative print statement format\n", "print_statement = 'The average temperature in Helsinki in ' + str(months[selected_month_index]) + ' is ' + str(average_temp[selected_month_index])\n", "```\n", "\n", "### Exercise 2.4\n", "\n", "```python\n", "# Create variable with file base name\n", "basename = \"Station\"\n", "\n", "# Create empty list for filenames\n", "filenames = []\n", "\n", "# Use for loop to populate list of filenames\n", "for i in range(21):\n", " station = basename + \"_\" + str(i) + \".txt\"\n", " filenames.append(station)\n", "```\n", "\n", "### Exercise 2.5\n", "\n", "```python\n", "# Store list of temperatures in memory\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", "# Create empty lists for classification\n", "cold = []\n", "slippery = []\n", "comfortable = []\n", "warm = []\n", "\n", "# Use for loop and conditional statements to populate category lists\n", "for temp in temperatures:\n", " if temp < -2:\n", " cold.append(temp)\n", " elif temp >= -2 and temp < 2:\n", " slippery.append(temp)\n", " elif temp >= 2 and temp < 15:\n", " comfortable.append(temp)\n", " elif temp >= 15:\n", " warm.append(temp)\n", "\n", "# Alternative solution\n", "# Note: Because of the test order \"greater than or equal to\" tests are not required\n", "for temp in temperatures:\n", " if temp < -2:\n", " cold.append(temp)\n", " elif temp < 2:\n", " slippery.append(temp)\n", " elif temp < 15:\n", " comfortable.append(temp)\n", " elif temp >= 15:\n", " warm.append(temp)\n", "\n", "# Print out number of times it was cold, comfortable, or warm\n", "print(f\"It was cold {len(cold)} times in Helsinki in April 2013.\")\n", "print(f\"It was cold {len(comfortable)} times in Helsinki in April 2013.\")\n", "print(f\"It was cold {len(warm)} times in Helsinki in April 2013.\")\n", "```\n", "\n", "### Exercise 2.6\n", "\n", "```python\n", "# Create temperature conversion function\n", "def fahr_to_celsius(temp_fahrenheit):\n", " \"\"\"Converts Fahrenheit temperature into Celsius.\"\"\"\n", " converted_temp = (temp_fahrenheit - 32) / 1.8\n", " \n", " return converted_temp\n", "\n", "# Make a list of temperatures to convert and loop over them to convert\n", "fahr_temps = [32, 68, 91, -17]\n", "for fahr_temp in fahr_temps:\n", " print(f\"{fahr_temp} °F is equal to {fahr_to_celsius(fahr_temp):.1f} °C.\")\n", "```\n", "\n", "### Exercise 2.7\n", "\n", "```python\n", "def temp_classifier(temp_celsius):\n", " \"\"\"\n", " Classifies temperatures into 4 different classes according following criteria:\n", " \n", " - Less than -2 degrees: returns 0\n", " - Less than +2 degrees and greater than or equal to -2 degrees: returns 1\n", " - Less than +15 degrees and greater than or equal to +2 degrees: returns 2\n", " - Greater than or equal to 15 degrees: returns 3 \n", " \"\"\"\n", " if temp_celsius < -2:\n", " return 0\n", " elif temp_celsius >= -2 and temp_celsius < 2:\n", " return 1\n", " elif temp_celsius >= 2 and temp_celsius < 15:\n", " return 2\n", " else:\n", " return 3\n", "\n", "# Create list of temperatures to classify and classify them\n", "celsius_temps = [17, 2, 1.9, -2]\n", "for celsius_temp in celsius_temps:\n", " print(f\"The temperature {celsius_temp} °C is in category {temp_classifier(celsius_temp)}.\")\n", "```" ] } ], "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }