π Day 22/150 – Simple Interest in Python
Calculating Simple Interest (SI) is a fundamental concept in both mathematics and programming. It helps you understand how formulas translate into code and how Python can be used for real-world financial calculations.
The formula for Simple Interest is:
Where:
- P = Principal amount
- R = Rate of interest
- T = Time (in years)
πΉ Method 1 – Direct Calculation
P = 1000 R = 5 T = 2 SI = (P * R * T) / 100 print("Simple Interest:", SI)
π§ Explanation:
- Values are directly assigned.
- Formula is applied in one line.
- Easy to understand and quick to execute.
π Best for: Learning basics and testing formulas.
πΉ Method 2 – Taking User Input
P = float(input("Enter principal: ")) R = float(input("Enter rate: ")) T = float(input("Enter time (years): ")) SI = (P * R * T) / 100 print("Simple Interest:", SI)
π§ Explanation:
- input() allows dynamic values.
- float() ensures decimal calculations.
- Makes the program interactive.
π Best for: Real-world scenarios.
πΉ Method 3 – Using a Function
def simple_interest(p, r, t): return (p * r * t) / 100 print(simple_interest(1000, 5, 2))
π§ Explanation:
- Function improves code reusability.
- Parameters (p, r, t) make it flexible.
- return gives the calculated value.
π Best for: Clean and reusable code.
πΉ Method 4 – Using Lambda Function
si = lambda p, r, t: (p * r * t) / 100
print(si(1000, 5, 2))
π§ Explanation:
- lambda creates a one-line function.
- Useful for short calculations.
π Best for: Quick operations.
πΉ Method 5 – Using Tuple Input (Extended)
P = 1000 R = 5 T = 2 SI = (P * R * T) / 100 Amount = P + SI print("Simple Interest:", SI) print("Total Amount:", Amount)
π§ Explanation:
- Calculates both Simple Interest and Total Amount. Amount = Principal + Interest
- Useful in financial applications.
π Best for: Practical use cases.
⚡ Key Takeaways
- Formula: (P × R × T) / 100
-
Use:
- Direct values → for simplicity
- Input → for user interaction
- Functions → for modular code
- Lambda → for short expressions
- Extended logic → for real applications
.png)

0 Comments:
Post a Comment