Python Basics

Python Strings

Python Dictionary

Python List

Exception Handling

Python Conversions

Python Collections

Python Examples

- Float to int Python
- Get tofixed of a value using round() Python
- Check if a key exist in a dictionary
- Generate a random number in python
- Find the square root in python
- Reverse a string or number
- Get last item from a List
- Return multiple values from a function
- Remove falsy values from python List
- Use separator in print() method of python
- One line code to get the sum of a list in python
- Python check NaN values with and without using packages
- for loop N times in python
- Get input as a number in python
- Get yesterday date in python
- Read JSON data in python
- Check for falsy values
- How to read a JSON file in Python
- Get Key from a Dictionary using Value in Python

Python Validations

If you are working on a finance application or some mathematical solutions then you may have a requirement to calculate the simple or compound interest. In this post, we will explain to calculate them one by one.

Let's check the mathematical formulas of simple and compound interest.

**Simple Interest = (principal * time * rate) / 100**

**Compound Interest = principal((1 + rate / 100)time - 1)**

We will calculate the simple interest using Python code here. We will use "*" and "/" arithmetic operators of Python to calculate that. We will be using the above Simple Interest formula for the calculation. Let's check the Python program to understand it.

```
# input values required
principal_amount = 10000
time = 4
rate_of_interest = 14
# calculate the simple interest
simple_interest = (principal_amount * time * rate_of_interest)/100
print("Simple interest is: ", simple_interest)
```

**Output**

`Simple interest is: 5600.0`

**In the above Python code example**

- We have defined three variables that will be used to calculate the simple interest in Python -
**principal_amount**,**time,**and**rate_of_interest**. - Using the code -
**(principal_amount * time * rate_of_interest)/100**, we are calculating the simple interest and assigning the result to the**simple_interest**variable. - Print the result -
**simple_interest**.

The **compound interest** can be calculated in Python using two methods. The first is that we use "*", "/" and "**" operators and the second is using "*", "/" and pow() function. We will be using the mathematical formula of Compound Interest to calculate it in Python.

**# Example 1**

```
# input values required
principal_amount = 10000
time = 4
rate_of_interest = 14
# calculate the simple interest
compound_interest = principal_amount * ((1 + rate_of_interest / 100)**time - 1)
print("Compound interest is: ", compound_interest)
```

**Output**

`Compound interest is: 6889.601600000008`

**# Example 2: Using pow() function**

```
# input values required
principal_amount = 8000
time = 4
rate_of_interest = 13.25
# calculate the compound interest
amount = principal_amount * (pow((1 + rate_of_interest / 100), time))
compound_interest = amount - principal_amount
print("Compound interest is: ", compound_interest)
```

**Output**

`Compound interest is: 5159.604275312504`

```
# input values required
principal_amount = 1000
time = 5
rate_of_interest = 12
# calculate the simple interest
simple_interest = (principal_amount * time * rate_of_interest)/100
# calculat the compound interest
compound_interest = principal_amount * ((1 + rate_of_interest/100)**time - 1)
print("Calculated simple interest: ", simple_interest)
# -> Calculated simple interest: 600.0
print("Calculated compound interest: ", compound_interest)
# -> Calculated compound interest: 762.3416832000007
```

Was this helpful?

## 0 Comments