Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

Compound interest calculator in Python

Compute future investment value with the compound interest formula and a readable year-by-year loop.

Easy Python 3.9+ Jun 30, 2026 Functions & basics 2 views 0 copies

Python code

48 lines
Python 3.9+
def future_value(
    principal: float,
    annual_rate: float,
    years: int,
    compounds_per_year: int = 12,
) -> float:
    """Return balance after compound interest (rounded to cents)."""
    rate_per_period = annual_rate / compounds_per_year
    periods = compounds_per_year * years
    amount = principal * (1 + rate_per_period) ** periods
    return round(amount, 2)


def yearly_balances(
    principal: float,
    annual_rate: float,
    years: int,
    compounds_per_year: int = 12,
) -> list[tuple[int, float]]:
    """Return (year, balance) after each full year."""
    schedule: list[tuple[int, float]] = []
    for year in range(1, years + 1):
        balance = future_value(principal, annual_rate, year, compounds_per_year)
        schedule.append((year, balance))
    return schedule


def main() -> None:
    principal = 1_000.0
    annual_rate = 0.05
    years = 10
    compounds_per_year = 12

    final = future_value(principal, annual_rate, years, compounds_per_year)
    print(f"Principal: ${principal:,.2f}")
    print(f"Rate: {annual_rate * 100:.1f}% compounded {compounds_per_year}x/year")
    print(f"Years: {years}")
    print(f"Future value: ${final:,.2f}")
    print()
    print("Year-by-year:")
    for year, balance in yearly_balances(
        principal, annual_rate, years, compounds_per_year
    ):
        print(f"  Year {year:2d}: ${balance:,.2f}")


if __name__ == "__main__":
    main()

Output

stdout
Principal: $1,000.00
Rate: 5.0% compounded 12x/year
Years: 10
Future value: $1,647.01

Year-by-year:
  Year  1: $1,051.16
  Year  2: $1,104.94
  Year  3: $1,161.47
  Year  4: $1,220.90
  Year  5: $1,283.36
  Year  6: $1,349.02
  Year  7: $1,418.04
  Year  8: $1,490.59
  Year  9: $1,566.85
  Year 10: $1,647.01

How it works

The closed-form formula is fast for a single balance. The loop helps when you need a schedule for charts, CSV export, or comparing contribution strategies.

Common mistakes

  • Using a percentage like 5 instead of the decimal rate 0.05.
  • Forgetting to compound — simple interest adds only P×r×t each year.
  • Rounding intermediate steps instead of the final balance.

Variations

  1. Add monthly contributions inside the loop for a savings-plan projection.
  2. Return a list of (year, balance) tuples for plotting with matplotlib.

Real-world use cases

  • Retirement and savings calculators on fintech dashboards.
  • Loan amortization vs investment growth comparisons in spreadsheets.
  • Teaching personal finance workshops with reproducible Python notebooks.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Functions & basics

Related tutorials and quizzes for this topic.