Python Programming Lab Manual
Python Programming Lab Manual
Python Programming Lab Manual
PROGRAMMING
LAB
22UCSCP44
1. Program to convert the given temperature from Fahrenheit to Celsius and vice
versa depending upon user’s choice.
def displayWelcome():
print(‘This program will convert a given temperature’)
print(‘Enter(F) from Fahrenheit to Celsius or (C) from Celsius to Fahrenheit’)
def getConvertTo():
which=input(“Enter your choice ‘F’ or ‘C’:”)
return which
def displayFahrenToCelsius():
fahrenheit=float(input(“Enter temperature in Fahrenheit:”))
celsius=(fahrenheit-32)*5/9
print(‘conversion of %.2f Fahrenheit is: %0.2fCelsius’%(fahrenheit, celsius))
def displayCelsiusToFahren():
celsius=float(input(“Enter temperature in Celsius:”))
fahrenheit=(celsius*9/5)+32
print(‘conversion of %2f Celsius is: %2f Fahrenheit’%(celsius, fahrenheit))
# Main Program
displayWelcome()
which=getConvertTo()
if which==’F’:
displayFahrenToCelsius()
else:
displayCelsiusToFahren()
Output:
This program will convert a given temperature
Enter(F) from Fahrenheit to Celsius or (C) from Celsius to Fahrenheit
Enter your choice ‘F’ or ‘C’:F
Enter temperature in Fahrenheit:102
conversion of 102.00 Fahrenheit is: 38.89Celsius
def rectangle():
length=int(input(“Enter length of rectangle:”))
breadth=int(input(“Enter breadth of rectangle:”))
area= length*breadth
print(“Area of rectangle:”, area)
def square():
side=int(input(“Enter the side of the square:”))
area= side**2
print(“Area of square:”, area)
def triangle():
a=float(input(“Enter the side 1 of a triangle:”))
b=float(input(“Enter the side 2 of a triangle:”))
c=float(input(“Enter the side 3 of a triangle:”))
s=(a+b+c)/2.0
area= (s*(s-a)*(s-b)*(s-c))**0.5
print(“The area of the triangle is %0.2f”%area)
def circle():
PI=3.14
radius=float(input(“Enter the radius of a circle:”))
area= PI*radius*radius
print(“Area of a circle=%.2f:”% area)
choice=input(“Enter your choice:”)
if choice==’c’:
circle()
elif choice==’r’:
rectangle()
elif choice==’s’:
square()
elif choice==’t’:
triangle()
Output:
Enter your choice:r
Enter length of rectangle:5
Enter breadth of rectangle:4
Area of rectangle: 20
Enter your choice:c
Enter the radius of a circle:5
Area of a circle=78.50:
5. Write a Python program to count the number of even and odd numbers from
list of N numbers.
list1=[5,20,15,60,40,111,12,156,135]
even_count, odd_count=0, 0
num=0
while(num<len(list1)):
if list1[num]%2==0:
even_count+=1
else:
odd_count+=1
num+=1
print(“Even numbers in the list:”, even_count)
print(“Odd numbers in the list:”, odd_count)
Output:
Even numbers in the list: 5
Odd numbers in the list: 4
6. Create a Turtle graphics window with specific size.
import turtle
#set window style
turtle.setup(800,600)
#get reference to turtle window
window=turtle.Screen()
#set window title bar
window.title(“My first Turtle program”)
Output:
7. Write a Python program using function that accepts a string and calculate the
number of upper-case letters and lower-case letters.
def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])
string_test('Python Programming Lab')
Output:
Original String : Python Programming Lab
No. of Upper case characters : 3
No. of Lower case Characters : 17
8. Python program to reverse a given string and check whether the give string is
palindrome or not.
Output:
Enter the string: madam
Reverse String: madam
madam is a Palindrome
def returnSum(myDict):
sum = 0
for i in myDict:
sum = sum + myDict[i]
return sum
dict = {'a': 100, 'b':200, 'c':300}
print("Sum :", returnSum(dict))
Output:
Sum : 1650
10. Read a file content and copy only the contents at odd and even lines into
separate new files.
Output:
input.txt
C Programming
C++ Programming
Visual C++
Java
Python
DotNet
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input(“Enter the number:”))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
Output:
Enter the number:7
The factorial of 7 is 5040
************************