Calculate Compound Interest in Python
Watch quarterly interest grow a balance with Python **. A beginner formula walkthrough explains period rates, compounding counts, and total versus interest.
1# Program to calculate compound interest23principal = float(input("Enter principal amount: "))4rate = float(input("Enter annual interest rate (in %): "))5time = float(input("Enter time in years: "))6n = int(input("Enter number of times interest applied per year: "))78amount = principal * (1 + rate / (100 * n)) ** (n * time)9compound_interest = amount - principal1011print("Compound Interest is:", compound_interest)12print("Total Amount is:", amount)
Sample Output
Total Amount is: 1104.486...
The total includes the original 1000; the earlier printed line contains only the earned interest.
In this one
- Convert annual rate to a period rate
- Use ** for repeated growth
- Separate total amount from earned interest
How it works
- 1
Read the principal, annual percentage, years, and integer frequency n.
- 2
Compute the period factor and raise it to n * time.
- 3
Subtract principal from amount, then print interest and total separately.
Edge cases
Zero frequency
Enter n = 0.
100 * n becomes zero, causing ZeroDivisionError; use a positive frequency.
Zero time
Enter time = 0 with positive n.
The exponent is zero, so amount equals principal and interest is zero.
What's going on
Simple interest is a multiply. Compound interest is a power. Every period starts with a slightly larger balance because earlier interest stays in the account.
The formula is A = P * (1 + R/(100*n))
(n*T). n means how many times interest is applied per year: quarterly means 4. R/(100*n) is the rate for one period, and n*T is the number of periods.
raises the growth factor to that count. ^ is XOR, not power; with these float operands it would raise TypeError.
The final amount includes the principal. compound_interest = amount - principal isolates what was earned. For the sample, eight quarterly periods grow 1000 to about 1104.49. n must be positive; zero causes division by zero. Floats are fine for exploring the formula, but a banking application also needs exact money and rounding rules.
After this program
If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does quarterly compounding use for n?
Pick an answer to check it.
Question 2
Which operator raises the growth factor to a power?
Pick an answer to check it.
Questions people ask
What does quarterly compounding use for n?
Use 4, because a year contains four quarterly interest periods.
Which operator raises the growth factor to a power?
** performs exponentiation; ^ is a different bitwise operator.
Why subtract principal from amount?
amount includes the starting money, while interest is only the extra money earned.