PYTHON LAB PROGRAMS
1. Program to convert the given temperature from Fahrenheit to
Celsius and vice versa depending upon user’s choice
Aim: To write a python program for temperature conversion from
Fahrenheit to Celsius and vice versa.
Program:
print('Welcome to celsius to fahrenheit conversion and vice versa
program')
print('[Link] Celsius to Fahrenheit')
print('[Link] Fahrenheit to Celsius')
choice=int(input('Enter your choice:'))
if choice==1:
→ celsius=float(input("Enter temperature in celsius:"))
→fahrenheit=(celsius*9/5 +32)
→ print('celsius :%.2f \nFahrenheit:%0.2f'%(celsius,fahrenheit))
elif choice==2:
→ fahrenheit=float(input("Enter temperature in fahrenheit:"))
→ celsius=(fahrenheit-32)* 5/9
→ print('Fahrenheit:%.2f \nCelsius:%0.2f'%(fahrenheit,celsius))
else:
→ print("Wrong Choice")
1
Result:
Thus the program for temperature conversion has been verified
successfully.
********************************************************
2. Program to find the area of rectangle, Square, Circle and
Triangle by accepting suitable input parameters from user.
Aim: To write a python program to find the area of different shapes
Program:
print("[Link] of rectangle")
print("[Link] of a triangle")
print("[Link] of a square")
print("[Link] of a circle")
ch=int(input("\n Enter your choice"))
if(ch==1):
→ width=float(input('Enter the width of a rectangle:'))
→ height=float(input('Enter the height of a rectangle:'))
→ area=width*height
→ print("\[Link] of a rectangle is:%0.2f"%area)
elif(ch==2):
→ b=float(input('Enter the area of a triangle:'))
→ h=float(input('Enter the height of a triangle:'))
→ area=0.5*(b*h)
2
→ print("\[Link] of a triangle is:%0.2f"%area)
elif(ch==3):
→ side=float(input('Enter the side of a square:'))
→ area=side*side
→ print("\[Link] of a square is:%0.2f"%area)
elif(ch==4):
→ pi=3.14
→ radius=float(input('Enter the radius of a circle:'))
→ area=pi*radius*radius
→ print("\[Link] of a circle is:%0.2f"%area)
else:
→ print("Wrong choice")
3
Result:
Thus the program for area of different shapes has been verified
successfully.
********************************************************
3. Use for Iterative statements in Python program and find the
factorial of a number
Aim: To write a python program to print factorial using recursion
Program:
def recur_factorial(n):
→if n==1:
→→return n
→else:
→→return n*recur_factorial(n-1)
num=int(input("Enter a number:"))
if num<0:
→print("sorry,factorial does not exists for negative number")
elif num==0:
→print("The factorial of 0 is 1")
else:
→print ("The factorial of",num,"is",recur_factorial(num))
4
Result:
Thus the program for factorial using recursion has been verified
successfully.
********************************************************
4. Write a Python program to count the number of even and
odd numbers from array of N numbers.
Aim: To write a python program to count the number of even and odd
numbers from array of N numbers.
Program:
import array as arr
num=[Link]('i',[2,3,245,6,89,90,20,35])
odd=0
even=0
for i in num:
→if(i%2==0):
→→even+=1
→else:
5
→→odd+=1
print("counting odd and even number in a list using array")
print("Number of even number",even)
print("Number of odd number",odd)
Result:
Thus the program for counting the number of even and odd
numbers from array of N numbers has been verified successfully.
********************************************************
5. Write a Python program to find the sum of the series of
n=10
Aim: To write a python program to find the sum of the series
Program:
n=int(input("Enter a number:"))
sum1=0
while(n>0):
→sum1=sum1+n
→n=n-1
print("The sum of first n natural numbers is",sum1)
6
Result:
Thus the program for finding the sum of series has been verified
successfully.
*****************************************************
6. Write a python program with tuple and list as input to count
the occurrences of all items of the list in the tuple.
Aim: To write a python program to count the occurrences of all items
of the list in the tuple.
Program:
def occurence (tup,lst):
→count=0
→for item in tup:
→→if item in lst:
→→→count+=1
→return count
tup=('a','a','c','b','d')
7
lst=['a','b']
print(occurence(tup,lst))
Result:
Thus the program to count the occurrences of all items of the list
in the tuple.