Write A Python Function To Calculate Simple Interest

Introduction

Simple interest is a method of calculating the amount of interest charged on a loan or investment. It is based on the principal amount, the interest rate, and the time period. The formula for simple interest is:

Python
Simple Interest = Principal * Rate * Time

Where:

  • Principal is the amount of money borrowed or invested
  • Rate is the interest rate expressed as a decimal
  • Time is the length of time in years

 

Writing a Python Function

To calculate simple interest in Python, we can write a function that takes the principal, rate, and time as parameters and returns the simple interest. Here is an example of how to do this:

Python
def simple_interest(principal, rate, time):
    simple_interest = principal * rate * time
    return simple_interest

To use this function, we would simply call it with the appropriate values for the principal, rate, and time. For example, to calculate the simple interest on a loan of $1000 at 5% for 2 years, we would call the function like this:

Python
simple_interest = simple_interest(1000, 0.05, 2)
print(simple_interest)

This would print the following output:

100.0

 

Examples

Here are some other examples of how to calculate simple interest in Python:

Calculate the simple interest on a loan of $5000 at 3% for 5 years:

Python
simple_interest = simple_interest(5000, 0.03, 5)
print(simple_interest)

This would print the following output:

750.0

Calculate the simple interest on an investment of $10,000 at 7% for 10 years:

Python
simple_interest = simple_interest(10000, 0.07, 10)
print(simple_interest)

This would print the following output:

7000.0

 

Conclusion

Calculating simple interest in Python is a simple task. By writing a function, we can easily calculate the simple interest for any principal, rate, and time.

Post a Comment

0 Comments