Week 2 Exercises
Exercise 1
• Generate a list with Apple, Banana and Mango
• Find if it contains banana in it
Exercise 2
• Create a variable x, assign to it the value of 5
• Check if x is an odd number
• If it is odd, print “x is odd”
• If it is even, print “x is even”
• Change x to be 6, re-run the cell
• What happens if x is a string (e.g., “abc”)? How would you handle this possibility?
Exercise 3
• Create a list with names of days of the week
• Print the result of a check that the list length equals 7 (i.e., True or False)
• Print the result of a check that the list contains “Friday” (i.e., True or False)
• Using only slicing, print the weekend days
• Print the list sorted alphabetically
• Print the type of the variable of the list
Exercise 4
• Create a list with “apple”, “orange”, “broccoli”, “potato”, “melon”
• Iterate over the list, printing only those words that begin with “a” or “o”
Exercise 5
• Create a list with an integer (3), a float (3.5), a string (“xyz”), and a list ([1, 3, 5])
• Iterate over the list, printing only those variables that are numbers
Exercise 6
Building a financial calculator
You must have studied how to value annuities. Annuities are constant streams of cash flows
for a specific time period. E.g. receiving £50 per month for 12 months would be one example.
Common applications of annuities are often seen in coupon bond pricing as well as for
retirement savings. In this lab your task is to build a simple annuity calculator that should
work in a similar way to those found on financial calculators.
The present value of an [ordinary] annuity is calculated as:
1
1− N
(1+ R)
PV =PMT ×
R
where: PV is the present value
PMT is the cash flow per period
R is the discount rate per period
N is the number of periods for which one will receive the cash flows
It is assumed that the first cash flow happens one period from today in this formula.
Exercise 6.1
Write a function annuityPv(pmt,r,n)that takes the inputs as stated (relating to the formula
above) and computes the present value of the annuity. Try it with the following numbers:
PMT = 5000
R = 5%
N = 10
Exercise 6.2
A financial calculator has some additional features. One is that it allows for an extra lump
sum payment at the very end of the annuity. This is an important feature as it allows us to
price a coupon bond. A coupon bond pays, for instance, £100 each year for 10 years and at
the end it repays its principal as well (at the same time as the last coupon) of e.g. £1000
(called fvin the function below but more often known as the principal).
Write a function that is called bondPv(coupon,r,n,fv)with the inputs as stated that uses the
annuityPv()function in its calculations to solve for the present value of the bond. Try it with
the following numbers:
R = 5%
N = 10
Coupon = 100
FV = 1000
C FV
PV of a bond=∑ n
+ n
(1+r ) (1+r )
Exercise 6.3:
Compute the sensitivity of the bond price to changes in the discount rate. Store the values in a
list. Use the following parameters:
coupon = 100
r = Varying from 3% to 15%
n = 10
fv = 1000
Store the output in a list called output
Hint: The easiest is to create an empty list called output by:
output = []
And then add values to the list as you loop over the inputs with:
output.append(newvalue)where newvalue is the newly created PV.
Exercise 6.4
We can use the annuity calculator to help us figure out how much to save for retirement.
Assume that we are expecting to work for another N years until retirement and we want to
have X pounds per year (into infinity) during our retirement to live on. Recall that an infinite
stream of cash flows is known as a perpetuity. The present value can be calculated as follows:
perpetuity =
CF
R
Perpetuity
PV of this perpetuity = N
(1+ R)
1
PV /1− N
(1+ R)
PMT =
R
Write a function annualRequiredSavings(X,r,n) that takes the appropriate inputs and
calculates how much we need to save per year for N years to have X pounds per year into
infinity from the moment we retire.
Test it with the following parameters:
X = 10,000
N = 40
R = 7%
What happens if R = 2%?
What happens if N = 30?