Python Programming Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

PYTHON

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

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’:C
Enter temperature in Celsius:38
conversion of 38.00 Celsius is: 100.40 Fahrenheit
2. Program to find the area of rectangle, square, circle and triangle by accepting
suitable input parameters from user.

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:

Enter your choice:t


Enter the side 1 of a triangle:7
Enter the side 2 of a triangle:8
Enter the side 3 of a triangle:9
The area of the triangle is 26.83

Enter your choice:s


Enter the side of the square:8
Area of square: 64

3. Program to calculate total marks, percentage and grade of a student. Marks


obtained in each of the five subjects are to be input by user. Assign grades
according to the following criteria:
Grade A: Percentage >=80 Grade B: Percentage >=70 and <80
Grade C: Percentage >=60 and <70 Grade D: Percentage >=40 and <60
Fail: Percentage <40

sub1=int(input(“Enter marks of the first subject:”))


sub2=int(input(“Enter marks of the second subject:”))
sub3=int(input(“Enter marks of the third subject:”))
sub4=int(input(“Enter marks of the fourth subject:”))
sub5=int(input(“Enter marks of the fifth subject:”))
total=sub1+ sub2+ sub3+ sub4+ sub5
avg=total/5
print(“Total :”, total)
print(“Average:”, avg)
if(avg>=80):
print(“Grade A”)
elif(avg>=70 and avg<80):
print(“Grade B”)
elif(avg>=60 and avg<70):
print(“Grade C”)
elif(avg>=40 and avg<60):
print(“Grade D”)
else:
print(“Fail”)
Output:
Enter marks of the first subject:89
Enter marks of the second subject:90
Enter marks of the third subject:93
Enter marks of the fourth subject:98
Enter marks of the fifth subject:97
Total : 467
Average: 93.4
Grade A

Enter marks of the first subject:40


Enter marks of the second subject:34
Enter marks of the third subject:24
Enter marks of the fourth subject:30
Enter marks of the fifth subject:20
Total : 148
Average: 29.6
Fail

4. Program to display the first ‘n’ terms of Fibonacci series.

n=int(input(“Enter the number:”))


a=0
b=1
print(“Fibonacci Series”)
if(n<0):
print(“Invalid Input”)
elif(n==0):
print(a)
elif(n==1):
print(b)
else:
print(a)
print(b)
for i in range(2,n):
c=a+b
print(c)
a=b
b=c
Output:
Enter the number:10
Fibonacci Series
0
1
1
2
3
5
8
13
21
34

Enter the number:-1


Fibonacci Series
Invalid Input

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.

string = input('Enter the string: ')


i = string
reverse = ' '
while(len(i) > 0):
if(len(i) > 0):
a = i[-1]
i = i[:-1]
reverse += a
print(‘Reverse String:’, reverse)
if(reverse == string):
print(string,'is a Palindrome')
else:
print(string,'is not a Palindrome')

Output:
Enter the string: madam
Reverse String: madam
madam is a Palindrome

Enter the string: computer


Reverse String: retupmoc
computer is not a Palindrome

9. Write a program to find sum of all items in a dictionary.

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.

def copy_odd_lines(input_file, output_file):


with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line_number, line in enumerate(infile, 1):
if line_number % 2 != 0:
outfile.write(line)
def copy_even_lines(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line_number, line in enumerate(infile, 1):
if line_number % 2 == 0:
outfile.write(line)
input_file_name = 'input.txt'
output_file_name = 'output.txt'
output1_file_name = 'output1.txt'
copy_odd_lines(input_file_name, output_file_name)
copy_even_lines(input_file_name, output1_file_name)

Output:
input.txt
C Programming
C++ Programming
Visual C++
Java
Python
DotNet

output.txt (odd lines)


C Programming
Visual C++
Python
output1.txt (even lines)
C++ Programming
Java
DotNet

11.Program to find factorial of the given number using recursive function.

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

12. Write a Python program for Towers of Hanoi using recursion.

def TowerOfHanoi(n , source, destination, auxiliary):


if n==1:
print ("Move disk 1 from source",source,"to destination",destination)
return
TowerOfHanoi(n-1, source, auxiliary, destination)
print ("Move disk",n,"from source",source,"to destination",destination)
TowerOfHanoi(n-1, auxiliary, destination, source)
n = int(input(“Enter the number of disks:”))
TowerOfHanoi(n,'A','B','C')
Output:
Enter the number of disks:3
Move disk 1 from source A to destination B
Move disk 2 from source A to destination C
Move disk 1 from source B to destination C
Move disk 3 from source A to destination B
Move disk 1 from source C to destination A
Move disk 2 from source C to destination B
Move disk 1 from source A to destination B

************************

You might also like