C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Rate: This is the interest rate. For an interest rate of 4.3%, we can pass 0.043.
Times: The times_per_year argument indicates yearly (1), quarterly (4) or monthly (12) interest.
Years: This final argument to compound_interest tells us how many years we compound interest. Thinking long-term is important.
Python program that computes compound interest
def compound_interest(principal, rate, times_per_year, years):
# (1 + r/n)
body = 1 + (rate / times_per_year)
# nt
exponent = times_per_year * years
# P(1 + r/n)^nt
return principal * pow(body, exponent)
# Compute 0.43% quarterly compound interest for 6 years.
result = compound_interest(1500, 0.043, 4, 6)
# Write result.
print(result)
print()
# Compute 20% compound interest yearly, quarterly and monthly.
print(compound_interest(1000, 0.2, 1, 10))
print(compound_interest(1000, 0.2, 4, 10))
print(compound_interest(1000, 0.2, 12, 10))
Output
1938.8368221341054
6191.7364223999975
7039.988712124658
7268.254992160187
And: Monthly and quarterly interest accumulates much faster than yearly interest. So this metric is important in an investment.
Tip: This comparison can help us judge certain investments. For example, bonds may pay monthly, but dividend stocks quarterly.