Python Programming Lab Final Exam - I B
(CS)
Computer science (University of Madras)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
92 / 1 & 9, Muthamizh Nagar,
Kodungaiyur, Chennai – 118.
I B.SC(COMPUTER SCIENCE)
PG DEPARTMENT OF COMPUTER SCIENCE
PYTHON PROGRAMMING LAB
Name of Student :
Batch Number :
University Register No. :
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
92 / 1 & 9, Muthamizh Nagar,
Kodungaiyur, Chennai – 118.
I B.SC (COMPUTER SCIENCE)
PG DEPARTMENT OF COMPUTER SCIENCE
PYTHON PROGRAMMING LAB
Name of Student :
Batch Number :
University Register No. :
Certified to be bonafide record of works done in the computer lab.
Head of the Department Staff-in-Charge
Submitted for the Practical Examination held in the year 2022 on at
Sree Muthukumaraswamy College, Chennai.
Internal Examiner External Examiner
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
INDEX
S.NO DATE TITLE PAG SIGN
E
NO
1 CELSIUS TO FAHRENHEIT
CONVERSION
2 PERCENTAGE CALCULATION
3 FIND THE AREA OF RECTANGLE,
SQUARE, CIRCLE AND
TRIANGLE BY ACCEPTING
SUITABLE INPUT PARAMETERS
FROM USER
4 FIBONACCI SERIES
5 FACTORIAL NUMBERS
6 COUNTING EVEN AND ODD NUMBERS
7 LOWERCASE AND UPPERCASE
8 STRING PALINDROME
9 SUM OF ALL ITEMS IN DICTIONARY
10 CONSTRUCT PATTERN USING NESTED
LOOP
INDEX
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
INDEX
S.NO DATE TITLE PAG SIGN
E
NO
READ A FILE CONTENT AND COPY
11 ONLY THE CONTENTS AT ODD
LINES IN TO A NEW FILE
12 CREATE A TURTLE
GRAPHICS WINDOW WITH
SPECIFIC SIZE
13 TOWER OF HANOI
CREATE A MENU DRIVEN PYTHON
14 PROGRAM WITH A DICTIONARY
FOR WORDS AND THEIR
MEANINGS
15 HANGMAN GAME
INDEX
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
1.CELSIUS TO FAHRENHEIT
print(“CONVERT CELSIUS TO FAHRENHEIT AND VICE VERSA”)
cel=float(input("enter the celsius value"))
fa=(1.8*cel)+32
print("the fahrenheit value is\t",fa)
far=float(input("enter the fahrenheit
value")) celsius=(far-32)/1.8
print("the celsius value is\t",celsius)
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
1.CELSIUS TO FAHRENHEIT
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
2. PERCENTAGE
m1=int(input("Enter the maths mark of the student:"))
p1=int(input("Enter the physics mark of the student:"))
c1=int(input("Enter the chemistry mark of the student:"))
lang=int(input("Enetr the language mark of the student:"))
eng=int(input("Enter the english mark of the student:"))
tot=m1+p1+c1+lang+eng
perc=(100*tot)/500
print("The total mark of the student is:",tot)
print("The percentage of the student is :",perc)
if perc>=80:
print("The student has obtained A grade")
elif perc>=70 and perc<80:
print("The student has obtained B grade")
elif perc>=60 and perc<70:
print("The student has obtained C grade")
elif perc>=40 and perc<60:
print("The student has obtained D grade")
elif perc<40:
print("The student has obtained E grade")
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
2. PERCENTAGE
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
3. FIND THE AREA OF RECTANGLE, SQUARE, CIRCLE
AND TRIANGLE BY ACCEPTING SUITABLE INPUT
PARAMETERS FROM USER
print("this program will calculate area of some geometric shapes for you\n")
print("shapes available are square,rectangle,triangle and circle\n")
print("what are would like to calculate?")
area_calc=input("enter square,reactangle,triangle or circle")
if area_calc=="square":
a_side=float(input("give length of side:"))
square_area=a_side**2
print("area of square is",square_area)
elif area_calc=="rectangle":
l_side=float(input("give the length:"))
w_side=float(input("give the width:"))
rect_area=l_side*w_side
print("area of rectangle is",rect_area)
elif area_calc=="triangle":
base=float(input("give the base length:"))
height=float(input("give the height:"))
triangle_area=(base*height)/2
print("area of triangle is",triangle_area)
elif area_calc=="circle":
radius=float(input("give the radius:"))
circle_area=3.14*radius**2
print("area of circle is ",circle_area)
else:
print("incorrect input")
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
3. FIND THE AREA OF RECTANGLE, SQUARE, CIRCLE
AND TRIANGLE BY ACCEPTING SUITABLE INPUT
PARAMETERS FROM USER
OUTPU
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
3. FIND THE AREA OF RECTANGLE, SQUARE, CIRCLE
AND TRIANGLE BY ACCEPTING SUITABLE INPUT
PARAMETERS FROM USER
OUTPU
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
4. FIBONACCI
print("fibonacci series")
f1=0
f2=1
n=int(input("enter the limit"))
print(f1)
print(f2)
for i in range(0,n-2):
f3=f1+f2
print (f3)
f1=f2
f2=f3
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
4. FIBONACCI
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
5. FACTORIAL
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 exist for negative number")
elif num==0:
print("the factorial of 0 is 1")
else:
print("the factorial of",num,"is",recur_factorial(num))
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
5. FACTORIAL
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
6. COUNTING EVEN AND ODD
print("FIND NUMBER OF ODD AND EVEN NUMBERS FROM GIVEN
LIST") a=[1,2,3,4,5]
ne=0
no=0
for i in range (0,len(a)):
if(a[i]%2==0):
ne=ne+1
else:
no=no+1
print("the number of odd number is",no)
print("the number of even number is",ne)
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
6. COUNTING EVEN AND ODD
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
7. LOWERCASE AND
s=input("enter the string")
u=0
l=0
for i in s:
if(i.isupper())==True:
u=u+1
else:
l=l+1
print("the total number of upper case letters are",u)
print("the total number of lower case letters are",l)
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
7. LOWERCASE AND
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
8. STRING
string=input("enter string:\t")
print(" The reversed string is",string[::-1])
if(string==string[::-1]):
print("the string is a palindrome")
else:
print("the string is not a palindrome")
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
8. STRING
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
9. SUM OF ALL ITEMS IN
print("sum of all the items in dictionary")
dict={'a':100,'b':200,'c':300}
sum=0
for i in dict:
sum=sum+dict[i]
print("sum:",sum)
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
9. SUM OF ALL ITEMS IN
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
10. CONSTRUCT PATTERN USING NESTED
for i in range(1,10):
for j in range(0,i):
print(i,end="")
print("")
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
10. CONSTRUCT PATTERN USING NESTED
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
11. READ A FILE CONTENT AND COPY ONLY THE CONTENTS
AT ODD LINES IN TO A NEW FILE
#open file in read mode
fn=open('bcd.txt','r')
#open file in write mode
fn1=open('infile.txt','w')
#read the content of the file line byb line
cont=fn.readlines( )
for i in range(0,len(cont)):
if((i%2)==0):
fn1.write(cont[i])
else:
pass
#close the file
fn1.close()
#open file in read mode
fn1=open('nfile.txt','r')
#read the content of the
file cont1=fn1.read()
#print the content of the file
print(cont1)
#close all files
fn.close()
fn1.close()
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
11. READ A FILE CONTENT AND COPY ONLY THE CONTENTS
AT ODD LINES IN TO A NEW FILE
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
12. CREATE A TURTLE GRAPHICS WINDOW WITH SPECIFIC
from turtle import*
forward(200)
left(90)
forward(200)
left(90)
forward(200)
left(90)
forward(200)
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
12. CREATE A TURTLE GRAPHICS WINDOW WITH SPECIFIC
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
13. TOWER OF HANOI USING
def hanoi(disks,source,auxiliary,target):
if disks==1:
print('Move disk 1 from peg{} to peg{}.'.format(source,target))
return
hanoi(disks-1,source,target,auxiliary)
print('Move disk{} from peg{}to
peg{}.'.format(disks,source,target)) hanoi(disks-
1,auxiliary,source,target)
disks=3
hanoi(disks,'A','B','C')
13. TOWER OF HANOI USING
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
13. TOWER OF HANOI USING
OUTPUT
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
>>>
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
14. CREATE A MENU DRIVEN PYTHON PROGRAM WITH
DICTIONARY FOR WORDS AND THEIR MEANINGS
dict={'ample':'enough','huge':'big','entire':'whole'}
print("choose the words for getting their meaning ")
print("ample")
print("huge")
print("entire")
s=input("enter the input ")
if s in dict:
print (dict[s])
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
14. CREATE A MENU DRIVEN PYTHON PROGRAM WITH A
DICTIONARY FOR WORDS AND THEIR MEANINGS
OUTPUT
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
15. HANGMAN
import random
words=['rainbow','compute','science','programming','python','mathematics','player','con
dition','reverse','water','board','geeks']
word=random.choice(words)
print("guess the characters")
guesses= ''
turns=12
while turns>0:
failed =0
for char in word:
if char in guesses:
print(char)
else:
print("-")
failed +=1
if failed ==0:
print("you win")
print("the word is:",word)
break
guess=input("guess a character")
guesses+=guess
if guess not in word:
turns-=1
print("wrong")
print(" you have", +turns,'more guesses')
if turns==0:
print("you loose")
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
15. HANGMAN
OUTPUT
Guess the characters
Guess a
characters:c Wrong
You have 11 more guesses
Guess a
characters:m Wrong
You have 10 more guesses
Guess a
characters:g Wrong
You have 9 more guesses
15. HANGMAN
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
Guess a
characters:s Wrong
You have 8 more guesses
guess a characters:
rr
guess a characters:
br
_b
guess a characters:
or
_b
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)
o
guess a characters:
wr
_b
guess a characters:
ar
a_
_b
guess a characters: i
_b
guess a characters: i
You Win
The word is: rainbow
>>>
Downloaded by nagu lekshmi (lekshmimarayoor@gmail.com)