# printing the values
print("Principle amount: ", p)
print("Interest rate : ", r)
print("Time in years : ", t)
print("Simple Interest : ", si)
Output
First run:
Enter the principle amount : 10000
Enter the rate of interest : 3.5
Enter the time in the years: 1
Principle amount: 10000.0
Interest rate : 3.5
Time in years : 1.0
Simple Interest : 350.0
Second run:
Enter the principle amount : 250000
Enter the rate of interest : 36
Enter the time in the years: 1
Principle amount: 250000.0
Interest rate : 36.0
Time in years : 1.0
Simple Interest : 90000.0
Calculate compound interest
We use the equation below to calculate compound interest
P(1 + R / 100) T
Where,
P – Principle amount
R – Rate of the interest, and
T – Time in the years
Example:
Input:
p = 250000
r = 36
t=1
# formula
ci = p * (pow((1 + r / 100), t))
print(ci)
Output:
339999.99999999994
Python program
# Python program to find compound interest
p = float(input("Enter the principle amount : "))
r = float(input("Enter the rate of interest : "))
t = float(input("Enter the time in the years: "))
# calculating compound interest
ci = p * (pow((1 + r / 100), t))
# printing the values
print("Principle amount : ", p)
print("Interest rate : ", r)
print("Time in years : ", t)
print("compound Interest : ", ci)
Output
First run:
Enter the principle amount : 10000
Enter the rate of interest : 3.5
Enter the time in the years: 1
Principle amount : 10000.0
Interest rate : 3.5
Time in years : 1.0
compound Interest : 10350.0
Second run:
Enter the principle amount : 250000
Enter the rate of interest : 36
Enter the time in the years: 1
Principle amount : 250000.0
Interest rate : 36.0
Time in years : 1.0
compound Interest : 339999.99999999994
Python program to check the given
year is a leap year or not
A leap year is a year that can be divided by 4, except for the century year (a year
that ends with 00). A year of a century is a leap year if it is divisible by 400.
Here, the user presents a year, and we will test if the year in question is a leap
year or not. We can solve this problem in two ways first by using the calendar
module, and second by simply checking the state of the leap year.
1) By using the calendar module
Initially we learn a little about the calendar module before we go to solve the
problem. Calendar module is built into Python which provides us with various
functions to solve the date, month and year related problem.
PROGRAM
# importing the module
import calendar
# input the year
year=int(input('Enter the value of year: '))
leap_year=[Link](year)
# checking leap year
if leap_year: # to check conditio n
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2021
The given year is a non-leap year.
2) By simply checking method
As we know it is a leap year, or not, to test the year in question. So, we'll
introduce the condition here and try to write the Python programme.
Program
# input the year
y=int(input('Enter the value of year: '))
# To check for non century year
if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2000
The given year is a leap year.
Python | Some of the examples of
simple if else
Example1: Enter a number and check whether it is 10 or not
a=int(input("Enter A : "))
if a==10:
print("Equal to 10")
else:
print("Not Equal to 10")
Output
Enter A : 10
Equal to 10
Example2: Find which is largest of two numbers
a=int(input("Enter A: "))
b=int(input("Enter B: "))
if a>b:
g=a
else:
g=b
print("Greater = ",g)
Output
Enter A: 36
Enter B: 24
Greater = 36
Example3: Find which is largest of two numbers using single
statement
a=int(input("Enter A: "))
b=int(input("Enter B: "))
c= a if a>b else b
print("Greater = ",c)
Output
Enter A: 24
Enter B: 36
Greater = 36
Calculate discount based on the sale
amount in Python
The discount rates are:
Amount Discount
0-5000 5%
5000-15000 12%
15000-25000 20%
above 25000 30%
Program:
# input sale amount
amt = int(input("Enter Sale Amount: "))
# checking conditions and calculating discount
if(amt>0):
if amt<=5000:
disc = amt*0.05
elif amt<=15000:
disc=amt*0.12
elif amt<=25000:
disc=0.2 * amt
else:
disc=0.3 * amt
print("Discount : ",disc)
print("Net Pay : ",amt-disc)
else :
print("Invalid Amount")
Output
Enter Sale Amount: 30000
Discount : 9000.0
Net Pay : 21000.0
Design a simple calculator using if elif
in Python
Given two numbers, and we will build a calculator-type program that will use
Python to add, subtract, multiply and divide operations.
Example:
Message:
Calculator
[Link]
[Link]
[Link]
[Link]
Input:
Enter Choice(1-4): 3
Enter A:10
Enter B:20
Output:
Product = 200
Program
# menus
print("Calculator")
print("[Link]")
print("[Link]")
print("[Link]")
print("[Link]")
# input choice
ch=int(input("Enter Choice(1-4): "))
if ch==1:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a+b
print("Sum = ",c)
elif ch==2:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a-b
print("Difference = ",c)
elif ch==3:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a*b
print("Product = ",c)
elif ch==4:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a/b
print("Quotient = ",c)
else:
print("Invalid Choice")
Output
Calculator
[Link]
[Link]
[Link]
[Link]
Enter Choice(1-4): 3
Enter A:10
Enter B:20
Product = 200