Simple Interest is calculated using the principal amount, time period, and rate of interest. In Python, a function can be used to calculate Simple Interest and return the result.
Example: if P = 1000, T = 2 years, and R = 5%, the Simple Interest is 100.0.
Simple interest formula : (P x T x R)/100
Using function
A function can be defined to calculate Simple Interest using the given principal, time, and rate values.
lambda p, t, r creates an anonymous function with three parameters.
si stores the lambda function.
si(p, t, r) calls the lambda function with the given values.
result stores the returned value.
Using list comprehension
List comprehension can also be used to perform the calculation and extract the result from a single-element list.
Python
p,t,r=8,6,8si=[p*t*r/100][0]print(si)
Output
3.84
Explanation :
[p * t * r / 100] creates a list containing the calculated value.
[0] accesses the first element of the list.
si stores the resulting value.
Note: List comprehension is generally used for creating lists from iterable data. It is not necessary for calculating a single value, but it can be used to demonstrate another Python approach.
You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.