Link Search Menu Expand Document

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 parameter interest, which is a list of n values, where n is time. For example, if time is 5, then interest will be a list with 5 elements, where each element indicates the interest for each respective year.