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
interest
rate, measured as a percentage per annum. - π
A
time
period, 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_interest
that accepts an input parameterinterest
, which is a list ofn
values, wheren
istime
. For example, iftime
is5
, theninterest
will be a list with5
elements, where each element indicates the interest for each respective year.