{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises for Variables" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 1" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Repeat the exercise from earlier on, about the daily return of a stock, but use variables instead of the raw numbers. You can initialize the variables to use \\\\$550 for the closing price of the stock on Monday, and then use \\\\$560 as the closing price on Tuesday. Try afterwards to change these numbers and see the results. (Hint: notice what would happen if you use the name `return` for the variable.) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "price_mon = 550\n", "price_tue = 560\n", "daily_return = 100 * (price_tue - price_mon) / price_mon\n", "print(\"The daily return is\")\n", "print(daily_return)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# If you uncomment the code below, notice that we cannot call the variable \"return\"\n", "# as \"return\" is a reserved keyword in Python\n", "\n", "# price_mon = 550\n", "# price_tue = 560\n", "# return = 100*(price_tue-price_mon)/price_mon\n", "# print(\"The daily return is\")\n", "# print(return)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 2\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Write a Python program to convert centimeters to feet and inches. Remember that one foot is 30.48 centimeters, and one inch is 2.54 centimeters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# So, let's take a look at our code from last time:\n", "print(\"180 cm is\", int(180 // 30.48), \"feet and\", round(180 % 30.48 / 2.54, 1), \"inches\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# We will now start introducing variables\n", "centimeters = 180\n", "feet = int(centimeters / 30.48)\n", "inches = int(centimeters % 30.48 / 2.54)\n", "print(centimeters, \"cm is\", feet, \"ft and\", inches, \"inches\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Now let's introduce more variables to eliminate the \"magic numbers\" 30.48 and 2.54\n", "cm_per_foot = 30.48\n", "cm_per_in = 2.54\n", "# And now let's modify our code from above\n", "centimeters = 180\n", "feet = int(centimeters / cm_per_foot)\n", "inches = int(centimeters % cm_per_foot / cm_per_in)\n", "print(centimeters, \"cm is\", feet, \"ft and\", inches, \"inches\")\n", "\n", "# And if we want to use round instead of int:\n", "inches = round(centimeters % cm_per_foot / cm_per_in, 1)\n", "print(centimeters, \"cm is\", feet, \"ft and\", inches, \"inches\")" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 3" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "We want to compute the \"[wind chill index](https://en.wikipedia.org/wiki/Wind_chill)\" as described at Wikipedia\n", "\n", "$T_\\mathrm{wc}=35.74+0.6215 T_\\mathrm{a}-35.75 v^{0.16}+0.4275 T_\\mathrm{a} v^{0.16}$\n", "\n", "where $T_\\mathrm{wc}$ is the wind chill index, based on the Fahrenheit scale; $T_\\mathrm{a}$ is the air temperature in degrees Fahrenheit, and $v$ is the wind speed in miles per hour. (Note: Windchill temperature is defined only for temperatures at or below 50 °F and wind speeds above 3.0 miles per hour.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "v = 5 # miles per hour\n", "Ta = 32 # air temperature in F\n", "Twc = 35.74 + 0.6215 * Ta - 35.75 * v ** 0.16 + 0.4275 * Ta * v ** 0.16\n", "print(\"The windchil index (feels-like) is:\", int(Twc))" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 4" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "You have a loan with a given principal amount $p$, to be repaid over a period of $n$ years. At the end of each year, you get charged an interest of $r$ for the total amount that you owe. Each year you pay an installment equal to $1/n$-th of the initial principal, plus interest on the *remaining* principal. \n", "\n", "Assume that the principal is \\\\$10K, interest rate of 3%, and repayment is over 10 years.\n", "\n", "Write code that computes how much you owe at the end of year 1. Then write code for calculating your payment at the end of year 2.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "principal = 10000\n", "rate = 0.03\n", "years = 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "interest = rate * principal\n", "payment = principal / years + interest\n", "print(\"Payment at the end of year 1\")\n", "print(payment)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "remaining_principal = principal - principal / years\n", "interest = rate * remaining_principal\n", "payment = principal / years + interest\n", "print(\"payment at the end of year 2\")\n", "print(payment)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }