Python Program to Calculate Simple and Compound Interest
Beginner Numbers
Works out the simple and the compound interest on the same principal, rate and number of years, shown to two decimal places.
Simple interest is paid on the original amount (the principal) only, so it's the same every year. Compound interest is added to the money at the end of each year, so the next year's interest is worked out on a bigger amount: interest earns interest. On 10,000 at 5% a year for 3 years, simple interest pays 500 a year, 1500 in all. Compounded once a year, the same money earns 1576.25.
Program
def simple_interest(principal, rate, years):
"""The interest on principal at rate percent a year."""
return principal * rate * years / 100
def compound_interest(principal, rate, years):
"""The interest when it's compounded once a year."""
amount = principal * (1 + rate / 100) ** years
return amount - principal
print(f"Simple interest: {simple_interest(10000, 5, 3):.2f}")
print(f"Compound interest: {compound_interest(10000, 5, 3):.2f}")
Open in the editor Download simple_compound_interest.py
Output
Simple interest: 1500.00
Compound interest: 1576.25
How it works
Step through the program line by line: each step below shows up as its line runs.
- Simple interest is the principal × the rate × the years, divided by 100 because the rate is a percentage: 10000 × 5 × 3 / 100 = 1500.
- With compounding, each year multiplies the money by 1 + rate / 100, which is 1.05 for 5%. After
yearsyears it has been multiplied by thatyearstimes, which**(to the power of) does in one step: 10000 × 1.05³ = 11576.25. - The interest is what the money grew by: the final amount minus the principal.
- The program works out both for 10,000 at 5% for 3 years.
:.2fin the f-string shows each result with 2 decimal places, as money is written. It rounds only what's printed: without it, Python would print1500.0and1576.2500000000018, as a float can't hold most decimals exactly.
Year by year, the compound interest grows because each year starts with more money:
| Year | At the start | Interest at 5% | At the end |
|---|---|---|---|
| 1 | 10,000.00 | 500.00 | 10,500.00 |
| 2 | 10,500.00 | 525.00 | 11,025.00 |
| 3 | 11,025.00 | 551.25 | 11,576.25 |
Other ways to write it
With a loop, year by year
Adding each year's interest in a loop does what the table does, one year at a time, and gives the same result as the formula.
def compound_interest(principal, rate, years):
amount = principal
for year in range(years):
amount += amount * rate / 100
return amount - principal
print(f"{compound_interest(10000, 5, 3):.2f}")Rounding with round()
round(value, 2) rounds the number itself, not only how it's printed, so the function returns 1576.25. A float keeps
no zeros at the end, though: 10,000 at 8% for 2 years prints as 1664.0.
def compound_interest(principal, rate, years):
amount = principal * (1 + rate / 100) ** years
return round(amount - principal, 2)
print(compound_interest(10000, 5, 3))
print(compound_interest(10000, 8, 2))Try it yourself
- Make it 10 years instead of 3 and watch the gap grow: compound interest comes to 6288.95, against 5000.00.
- Print the final amount, the principal plus the interest, as well as the interest.
- Compound the interest monthly: divide the rate by 12 and multiply the years by 12. 10,000 at 5% for 3 years then earns 1614.72.
- Print the balance at the end of each year, as in the table above, with a for loop.