{ "cells": [ { "cell_type": "markdown", "id": "442407cc-e877-47e6-b523-4455b9536b0d", "metadata": {}, "source": [ "# Solutions to questions" ] }, { "cell_type": "markdown", "id": "bcba608f-7a52-4a60-aa9f-0349fa2e589a", "metadata": {}, "source": [ "## Chapter 2\n", "\n", "2.1\n", "```python\n", "math.sin(math.pi)\n", "```\n", "\n", "2.2 \n", "```python\n", "my_variable = \"Python is cool!\"\n", "my_variable\n", "```\n", "\n", "2.3 \n", "```python\n", "# Solutions may vary\n", "first_variable = \"Python\"\n", "second_variable = \" is cool!\"\n", "\n", "print(first_variable + second_variable) # Works\n", "print(5 * first_variable) # Works\n", "print(first_variable - second_variable) # Fails\n", "```\n", "\n", "2.4 \n", "```python\n", "for i in range(2, 9, 3):\n", " print(i)\n", "```\n", "\n", "2.5\n", "```bash\n", "11\n", "7\n", "11\n", "15\n", "11\n", "```\n", "\n", "2.6\n", "```python\n", "weather = \"rain\"\n", "\n", "if weather == \"rain\":\n", " print(\"Wear a raincoat\")\n", " print(\"Wear rain boots\")\n", "else:\n", " print(\"No rainwear needed\")\n", "```\n", "\n", "2.7\n", "```python\n", "'B'\n", "```\n", "\n", "2.8\n", "```python\n", "weather = \"rain\"\n", "wind_speed = 20\n", "comfort_limit = 18\n", "\n", "# If it is windy or raining, print \"stay at home\", else print \"go out and enjoy the weather!\"\n", "if (weather == \"rain\") or (wind_speed >= comfort_limit):\n", " print(\"Just stay at home\")\n", "else:\n", " print(\"Go out and enjoy the weather! :)\")\n", "```\n", "\n", "2.9\n", "```python\n", "def hello(name, age):\n", " return \"Hello, my name is \" + name + \". I am \" + str(age) + \" years old.\"\n", "\n", "output = hello(name=\"Dave\", age=41)\n", "print(output)\n", "```" ] }, { "cell_type": "markdown", "id": "71154383-cd9e-405a-ae1d-fdc2f8e34476", "metadata": {}, "source": [ "## Chapter 3\n", "\n", "3.1\n", "```python\n", "len(data.columns)\n", "```\n", "\n", "3.2\n", "```python\n", "data[\"TEMP_KELVIN\"] = data[\"TEMP_CELSIUS\"] + 273.15\n", "```\n", "\n", "3.3\n", "```python\n", "data.loc[23:29, \"TEMP_CELSIUS\"].mean()\n", "```\n", "\n", "3.4\n", "```python\n", "data[\"TEMP_CELSIUS\"].loc[data[\"YEARMODA\"] >= 20160624].mean()\n", "```\n", "\n", "3.5\n", "```python\n", "data[\"MONTH\"] = data[\"TIME_STR\"].str.slice(start=4, stop=6)\n", "```" ] }, { "cell_type": "markdown", "id": "8c7a08f7-1c4f-46fb-8fe6-35c9f1c07164", "metadata": {}, "source": [ "## Chapter 4\n", "\n", "4.1\n", "```python\n", "# Define dates\n", "start_time = pd.to_datetime(\"201910011800\")\n", "end_time = pd.to_datetime(\"201910020000\")\n", "warm_time = pd.to_datetime(\"201910012055\")\n", "\n", "# Adjust axis limits\n", "ax = oct1_temps.plot(\n", " style=\"k--\",\n", " title=\"Helsinki-Vantaa temperatures\",\n", " xlabel=\"Date\",\n", " ylabel=\"Temperature [°F]\",\n", " xlim=[start_time, end_time],\n", " ylim=[35.0, 44.0],\n", " label=\"Observed temperature\",\n", " figsize=(12, 6),\n", ")\n", "\n", "# Add plot text\n", "ax.text(warm_time, 43.0, \"Warmest temperature in the evening ->\")\n", "ax.legend(loc=4)\n", "```\n", "\n", "4.2\n", "```python\n", "len(data.dropna())\n", "```\n", "\n", "4.3\n", "```python\n", "# Create the new figure and subplots\n", "fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12, 5))\n", "\n", "# Rename the axes for ease of use\n", "ax1 = axs[0]\n", "ax2 = axs[1]\n", "\n", "# Set plot line width\n", "line_width = 1.5\n", "\n", "# Plot data\n", "winter_temps.plot(\n", " ax=ax1,\n", " c=\"blue\",\n", " lw=line_width,\n", " ylim=[min_temp, max_temp],\n", " xlabel=\"Date\",\n", " ylabel=\"Temperature [°C]\",\n", " grid=True,\n", ")\n", "summer_temps.plot(\n", " ax=ax2,\n", " c=\"green\",\n", " lw=line_width,\n", " ylim=[min_temp, max_temp],\n", " xlabel=\"Date\",\n", " grid=True,\n", ")\n", "\n", "# Set figure title\n", "fig.suptitle(\n", " \"2012-2013 Winter and summer temperature observations - Helsinki-Vantaa airport\"\n", ")\n", "\n", "# Rotate the x-axis labels so they don't overlap\n", "plt.setp(ax1.xaxis.get_majorticklabels(), rotation=20)\n", "plt.setp(ax2.xaxis.get_majorticklabels(), rotation=20)\n", "\n", "# Season label text\n", "ax1.text(pd.to_datetime(\"20130215\"), -25, \"Winter\")\n", "ax2.text(pd.to_datetime(\"20130815\"), -25, \"Summer\")\n", "```" ] }, { "cell_type": "markdown", "id": "24d4eb8f-ba3a-4ae1-bf64-0718ddd733e6", "metadata": {}, "source": [ "## Chapter 6\n", "\n", "6.1\n", "```python\n", "\n", "# Triangle\n", "Polygon([(0, 0), (2, 4), (4, 0)])\n", "\n", "# Square\n", "Polygon([(0, 0), (0, 4), (4, 4), (4, 0)])\n", "\n", "# Circle (using a buffer around a point)\n", "point = Point((0, 0))\n", "point.buffer(1)\n", "\n", "```\n", "\n", "6.2\n", "```python\n", "\n", "# Save to file\n", "temp = gpd.read_file(output_fp)\n", "\n", "# Check first rows\n", "temp.head()\n", "\n", "# You can also plot the data for a visual check\n", "temp.plot()\n", "\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 }