Python Lab Rec
Python Lab Rec
PROGRAM 1
Program to find the largest and smallest integer among three declared integers
SOURCE CODE
OUTPUT
PROGRAM 2
Program to check whether given year is a leap year or not
SOURCE CODE
OUTPUT
PROGRAM 3
Program to find the factorial of a given number
SOURCE CODE
n=int(input("enter a number\n"))
fact=1
for i in range(1,n+1):
fact=fact*i
print("the factorial is :",fact)
OUTPUT
PROGRAM 4
Program to check whether given number is Palindrome or not
SOURCE CODE
n=int(input("enter a number\n"))
rev=0
temp=n
while(n>0):
r=n%10
rev=rev*10+r
n=n//10
if(temp==rev):
print("the number is palindrome")
else:
print("the number is not a palindrome")
OUTPUT
PROGRAM 5
Program to check whether given number is Armstrong or not
SOURCE CODE
n=int(input("enter a number\n"))
arm=0
temp=n
while(n>0):
r=n%10
arm=arm+r**3
n=n//10
if(temp==arm):
print("the number is armstrong")
else:
print("the number is not armstrong")
OUTPUT
PROGRAM 6
Program to calculate simple interest
SOURCE CODE
OUTPUT
PROGRAM 7
Program to print the Fibonacci series up to a given limit
SOURCE CODE
OUTPUT
PROGRAM 8
Program to convert Decimal number to Binary number
SOURCE CODE
def dectobin(dec):
if dec>=1:
dectobin(dec//2)
print(dec%2,end="")
dec=int(input("enter a decimal number\t"))
dectobin(dec)
OUTPUT
PROGRAM 9
Program to find the reverse of a given number
SOURCE CODE
n=int(input("enter a number\t"))
rev=0
while(n>=1):
r=n%10
rev=rev*10+r
n=n//10
print("the reverse of the number is:",rev)
OUTPUT
PROGRAM 10
Program to print all numbers in a range divisible by a number
SOURCE CODE
OUTPUT
PROGRAM 11
Program to find the reverse of a string using recursion
SOURCE CODE
def reverse(strg):
if len(strg) == 0:
return strg
else:
return reverse(strg[1:]) + strg[0]
a = input("Enter the string to be reversed: ")
print(reverse(a))
OUTPUT
PROGRAM 12
Program to form a new string using the first two and last two characters of a given
string
SOURCE CODE
OUTPUT
PROGRAM 13
Program to generate a dictionary containing n numbers as keys and its squares as
values
SOURCE CODE
n=int(input("Enter a number:"))
d={x:x*x for x in range(1,n+1)}
print("the resultant dictionary is ")
print(d)
OUTPUT
PROGRAM 14
Program to find the number of digits in a number
SOURCE CODE
OUTPUT
PROGRAM 15
Program to find the area of a rectangle using classes
SOURCE CODE
class Rectangle:
def __init__(self,l,b):
self.l=l
self.b=b
def area(self):
print("Area of the rectangle is",self.l*self.b)
#main program
len=int(input("enter the length\n"))
brd=int(input("enter the breadth\n"))
rect=Rectangle(len,brd)
rect.area()
OUTPUT
PROGRAM 16
Program to implement list operations (Append, Delete and Display) using Classes.
SOURCE CODE
class Listop:
def append(self,elm,l):
l.append(elm)
def delete(self,pos,l):
del l[pos]
def display(self,l):
print(l)
#main program
s=input("enter the list elements\t")
l=s.split()
print("==============LIST OPERATIONS==========")
print("1.Append")
print("2.Delete")
print("3.Display")
print("4.Exit")
print("=======================================")
while True:
c=int(input("enter the choice\t"))
ob=Listop()
if c==4:
break
if c==1:
n=int(input("enter the element to append\t"))
ob.append(n,l)
elif c==2:
p=int(input("enter the position to delete\t"))
ob.delete(p,l)
elif c==3:
ob.display(l)
print("=======================================")
OUTPUT
PROGRAM 17
Program to create a class and compute the Area and Perimeter of a Circle
SOURCE CODE
import math
class Circle:
def __init__(self,r):
self.r=r
def area(self):
ar=round(math.pi*self.r**2,2)
print("Area of the circle is ",ar)
def perimeter(self):
p=round(2*math.pi*self.r,2)
print("Perimeter of the circle is ",p)
#main program
rad=float(input("enter the radius of the circle\t"))
ob=Circle(rad)
ob.area()
ob.perimeter()
OUTPUT
PROGRAM 18
Program to perform basic calculator operations using class concept
SOURCE CODE
class Calculator:
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
print("The sum is ",self.a+self.b)
def sub(self):
print("The difference is ",self.a-self.b)
def div(self):
print("The quotient is ",self.a/self.b)
def mul(self):
print("The product is ",self.a*self.b)
#main program
print("==============SIMPLE CALCULATOR==========")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("4.Exit")
print("=======================================")
while True:
c=int(input("enter the choice\t"))
if c==5:
break
a=int(input("enter the first number\t"))
b=int(input("enter the second number\t"))
ob=Calculator(a,b)
if c==1:
ob.add()
elif c==2:
ob.sub()
elif c==3:
ob.mul()
elif c==4:
ob.div()
print("=======================================")
OUTPUT
PROGRAM 19
Program to read a text file and count the occurrence of a certain letter in that file
SOURCE CODE
OUTPUT
PROGRAM 20
Program to read a text file and print all digits present in it
SOURCE CODE
OUTPUT
PROGRAM 21
Program for the generation of different pyramid patterns
SOURCE CODE
OUTPUT
PROGRAM 22
Program to convert Celsius to Fahrenheit and vice versa
SOURCE CODE
print("==========TEMPERATURE CONVERSION==========")
print("1. Fahrenheit to celisius")
print("2. Celisius to Farenheit")
print("3. Exit")
print("==========================================")
while True:
c=int(input("enter the choice\t"))
if c==3:
break
l=int(input("enter lower limit of range\t"))
u=int(input("enter upper limit of range\t"))
if c==1:
print("The Celisius temperature")
for i in range(l,u):
cel=(5*(i-32))/9
print(round(cel,2))
if c==2:
print("The fahrenheit temperature")
for i in range(l,u):
f=(9*i+(32*5))/5
print(round(f,2))
print("==========================================")
OUTPUT
PROGRAM 23
Program to create a BMI calculator
SOURCE CODE
def bmi(w,h):
print("The body mass index is ",(w/h**2))
#main program
w=float(input("enter the weight in kilogram\n"))
h=float(input("enter the height in centimeters\n"))
hm=h/100
bmi(w,hm)
OUTPUT
PROGRAM 24
Program to read a text file and count the occurrence of a certain word in that file
SOURCE CODE
OUTPUT
PROGRAM 25
Program to calculate restaurant bill based on orders made
SOURCE CODE
print("==========RESTAURANT BILL==========")
print('Enter ordered items for person 1')
ap1 = float(input('Appetizier: '))
ep1= float(input('Main meal: '))
dp1 = float(input('Drinks: '))
des1 = float(input('Dessert: '))
print('\nEnter ordered items for person 2')
ap2 = float(input('Appetizier: '))
ep2 = float(input('Main meal: '))
dp2 = float(input('Drinks: '))
des2 = float(input('Dessert: '))
tot1 = ap1+ep1+dp1+des1
tot2 = ap2+ep2+dp2+des2
cost =tot1+tot2
print("Total bill amount = ",cost)
print("====================================")
c=input("Do you have the gift coupon?(y/n)\t")
if c=='y' or c=='Y':
gift= float(input('Enter amount of the gift coupon: '))
amt=gift-cost
if amt<0:
bill=-1*amt
print("Amount to be paid is ",bill)
else:
print("Nothing to pay, balance in gift coupon is ",amt)
else:
print("total amount to be paid is ",cost)
print("====================================")
OUTPUT