Calculate compound interest
Compound interest allows us to determine the growth of an investment over time, assuming a given annual increase. In other words, we have:
- πΆ A
principal, which is the initial amount of money. - π An
interestrate, measured as a percentage per annum. - π
A
timeperiod, measured in years.
def compound_interest(principal, interest, time):
return principal * (1 + interest) ** time
With the following parameters, you can see that 1000 Euros at 5% per annum over 5 years becomes 1276.
principal = 1000
interest = 0.05
time = 5
end_interest = compound_interest(principal, interest, time)
print(f"{principal} -> {end_interest:.0f} in {time} years at {interest:.0%} interest")
# 1000 -> 1276 in 5 years
βοΈ Exercises:
- Create another function
compound_interestthat accepts an input parameterinterest, which is a list ofnvalues, wherenistime. For example, iftimeis5, theninterestwill be a list with5elements, where each element indicates the interest for each respective year.