{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this assignment, you will need the information in:\n", "\n", "* [Expressions](https://matthew-brett.github.io/dsfe/chapters/02/Expressions)\n", "* [Names](https://matthew-brett.github.io/dsfe/chapters/02/Names)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don't forget to execute the cells in the notebook, that I've written for you." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In what follows, you will see me using *comments*. Comments start with a hash character - `#`. Python ignores everything after the hash character. This is useful to write text for you or others to read, inside your code." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "For example, try executing the following cell." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# This is just a comment. Python ignores it. It's just for show." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nothing happens - Python ignored everything after the hash character." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The problem is my bank.\n", "\n", "I have a credit card debt of £500.\n", "\n", "Let's give that debt amount, a *name*:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# This is the amount I owe at the moment\n", "my_debt = 500" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At the moment, the bank is nice enough to charge me 10% interest per year. Let's give that a name too." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "interest_rate = 0.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The amount of interest, after one year, is therefore:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interest = my_debt * interest_rate\n", "interest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then my total new debt will be:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "550.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_debt + interest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I can also calculate my total new debt all in one go by multiplying by `1 + interest_rate` - in my case - `1.1`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "debt_increaser = 1 + interest_rate\n", "debt_increaser" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "550.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_debt * debt_increaser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now your turn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add your code to the cell below, and execute it, to show my total debt after two years. Your code will probably start with:\n", "\n", "```\n", "my_debt * debt_increaser\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show my debt after two years.\n", "# Your code below this comment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fill in the next cell, in the same way, to show my debt after three years." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show my debt after three years." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now show my debt after 10 years. You might want to use the *power* operator `**` to do this." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show my debt after ten years." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## An offer from the bank" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The bank has just sent me a nice letter explaining that they are going to start charging me interest every week instead of every year. They value me as a customer, so, instead of dividing the annual interest rate by 52, they are going to divide it by 53 instead. But - is that a good offer?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is their proposed weekly interest rate:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0018867924528301887" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weekly_interest_rate = interest_rate / 53\n", "weekly_interest_rate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That corresponds to:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0018867924528303" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weekly_debt_increaser = 1 + weekly_interest_rate\n", "weekly_debt_increaser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, starting from my original debt, I will owe this much after one week:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "500.94339622641513" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# What I owe, after one week, on the new deal\n", "my_debt * weekly_debt_increaser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fill in the cell below, to show how much I will owe after 52 weeks. You will probably want `**` here again." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show my debt after 52 weeks, on the new deal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For extra cool points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you calculate roughly what the weekly interest rate has to be, in order to correspond to the 10% annual interest that I started with? There are several ways to do this, but you can try trial and error, if you like." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code to estimate the right weekly interest rate." ] } ], "metadata": { "jupytext": { "main_language": "python", "text_representation": { "extension": ".Rmd", "format_name": "rmarkdown", "format_version": "1.0", "jupytext_version": "0.8.2" } }, "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }