In this tutorial, we will learn how to calculate Simple Interest using Python. Simple interest is a method of calculating the interest charge on a loan or deposit, based on the original principal amount, interest rate, and the time period for which the money is borrowed or invested. The formula for calculating simple interest is:
Simple Interest (SI) = (Principal Amount * Rate of Interest * Time) / 100
Where:
- Principal Amount (P) is the initial amount of money.
- Rate of Interest (R) is the annual interest rate (as a percentage).
- Time (T) is the time period for which the money is borrowed or invested, typically in years.
Now, let’s look at the Python code to calculate Simple Interest:
Python Code
# Simple Interest Calculator in Python # Function to calculate simple interest def calculate_simple_interest(principal, rate, time): # Calculate simple interest simple_interest = (principal * rate * time) / 100 return simple_interest # Main program to take user input and display the result def main(): # Taking user inputs principal = float(input("Enter the principal amount: ")) rate = float(input("Enter the rate of interest (in percentage): ")) time = float(input("Enter the time period in years: ")) # Calculate simple interest interest = calculate_simple_interest(principal, rate, time) # Display the result print(f"The Simple Interest is: {interest}") print(f"The total amount (Principal + Interest) is: {principal + interest}") # Run the program if __name__ == "__main__": main()
Explanation of the Program
The Python program has the following components:
- Function: calculate_simple_interest(principal, rate, time): This function takes three inputs: the principal amount, the rate of interest, and the time period. It calculates and returns the simple interest using the formula provided above.
- Main Program: The main program prompts the user to input the principal amount, the rate of interest, and the time period. It then calls the
calculate_simple_interest
function to compute the interest, and displays both the interest and the total amount (Principal + Interest). - Input and Output: The program uses the
input()
function to take user input for the principal, rate, and time. Theprint()
function is used to display the results to the user. - Program Execution: The program is executed using the
main()
function, which is called only if the script is run directly (not imported as a module). Theif __name__ == "__main__"
block ensures this.
How to Run the Program
Follow these steps to run the program on your local machine:
- Ensure that Python is installed on your system. You can download and install Python from the official website: https://www.python.org/downloads/.
- Copy the Python code provided above into a text editor (like Notepad on Windows, or TextEdit on macOS) and save the file with a
.py
extension, for example,simple_interest.py
. - Open the terminal (command prompt on Windows, Terminal on macOS or Linux).
- Navigate to the directory where you saved the
simple_interest.py
file using thecd
command. - Run the program by typing the following command and pressing Enter:
python simple_interest.py
- Enter the required inputs when prompted (principal, rate, and time). The program will output the calculated simple interest and the total amount.
Example
Suppose you have the following inputs:
- Principal: 1000
- Rate of Interest: 5%
- Time: 2 years
The program will output the following result:
The Simple Interest is: 100.0 The total amount (Principal + Interest) is: 1100.0
That’s it! You’ve successfully created a simple interest calculator using Python.