๐ Day 23/150 – Compound Interest in Python
Understanding Compound Interest (CI) is a step ahead of simple interest and is widely used in finance, banking, and investments. It helps you see how money grows over time when interest is applied repeatedly.
๐ Formula
Where:
P = Principal
R = Rate of interest
T = Time (years)
๐น Method 1 – Direct Calculation
P = 1000
R = 5
T = 2
Amount = P * (1 + R/100) ** T
CI = Amount - P
print("Compound Interest:", CI)
print("Total Amount:", Amount)๐ง Explanation:
(1 + R/100) calculates growth rate.
** T means power (compounding over time).
Final amount includes interest, so we subtract P to get CI.
๐ Best for: Understanding the formula clearly.
๐น Method 2 – Taking User Input
P = float(input("Enter principal: "))
R = float(input("Enter rate: "))
T = float(input("Enter time (years): "))
Amount = P * (1 + R/100) ** T
CI = Amount - P
print("Compound Interest:", CI)๐ง Explanation:
Makes the program dynamic.
Converts input into numbers using float().
๐ Best for: Real-life usage.
๐น Method 3 – Using a Function
๐ง Explanation:
Encapsulates logic inside a function.
Easy to reuse anywhere in your program.
๐ Best for: Clean and modular code.
๐น Method 4 – Using Lambda Function
ci = lambda p, r, t: p * (1 + r/100) ** t - p
print(ci(1000, 5, 2))๐ง Explanation:
One-line function using lambda.
Great for short calculations.
๐ Best for: Quick operations.
⚡ Key Takeaways
Compound interest grows exponentially, unlike simple interest.
Use ** for power calculations in Python.
Formula: P * (1 + R/100) ** T
CI = Amount - Principal
๐ก Pro Tip
Enhance this program by:
Adding number of times interest is compounded per year
Rounding output using round(value, 2)
Building a mini finance calculator combining SI + CI
๐ฅ Final Thought
Compound interest is one of the most powerful concepts in finance — and now you know how to implement it in Python!
Keep building ๐
.png)
.png)
