Final Python Lab Manual
Final Python Lab Manual
JAIN INSTITUTE OF
TECHNOLOGY, DAVANAGERE
(A Unit of Jain group of Institutions, Bangalore)
Affiliated to VTU, Belagavi |Approved by AICTE, New Delhi
Introduction to
Python Programming
(BPLCK105B/205B)
Laboratory Manual
I/II Semester
2022-23
Arka Educational & Cultural Trust (Regd.)
JAIN INSTITUTE OF TECHNOLOGY, DAVANAGERE
(A Unit of Jain Group of Institutions, Bangalore)
To develop socially responsible computer engineers and entrepreneurs with strong academic excellence,
technical backgrounds, research and innovation, intellectual skills and creativity to cater the needs of IT
Industry and society by adopting professional ethics.
1. a. Develop a program to read the student details like Name, USN, and
Marks in three subjects. Display the student details, total marks and
percentage with suitable messages.
totalmarks= stdntmarks1+stdntmarks2+stdntmarks3
percentage = (totalmarks/300)*100
spcstr = "=" * 70
print(spcstr)
print("The name of the student is :", stdntname)
print("The Student USN is :", stdntusn)
print("The Marks secured in Subject1 :", stdntmarks1)
print("The Marks secured in Subject2 :", stdntmarks2)
print("The Marks secured in Subject3 :", stdntmarks3)
print(spcstr)
output:
curryear = date.today().year
age = curryear - birthyear
print(age)
if age>=60:
print("person is senior citizen")
else:
print("Person is not senior citizen")
output:
output:
Enter the value of n 5
The Fibonacci Series of n numbers are
0
1
1
2
3
def factorial(n):
if n==0:
return 1
elif n==1:
return 1
else:
return n*factorial(n-1)
output:
Enter the value of n5
enter the value of r2
The result of nCr is
10.0
3. Read N numbers from the console and create a list. Develop a program
to print mean, variance and standard deviation with suitable
messages.
variance = 0
for elem in mylist:
variance+= (elem-mean)*(elem-mean)
variance=variance/n
print("variance=",variance)
stddev= sqrt(variance)
print("Stddev=",stddev)
output:
Enter the Number of Elements: 3
Enter the element :15
Enter the element :10
Enter the element :10
Sum= 35
Mean= 11.666666666666666
variance= 5.5555555555555545
Stddev= 2.357022603955158
output:
Enter a number : 11232
The number entered is : 11232
3 occurs 1 times
2 occurs 2 times
1 occurs 2 times
import sys
import string
import os.path
if not os.path.isfile(fname):
print("File", fname, "doesn’t exists")
sys.exit(0)
filecontents = ""
wordFreq = {}
wordList = filecontents.split()
output:
Enter the filename : 1.txt
===================================================
10 most frequently appearing words with their count
===================================================
i occurs 3 times
am occurs 3 times
and occurs 2 times
abc occurs 1 times
working occurs 1 times
in occurs 1 times
CS occurs 1 times
E occurs 1 times
an occurs 1 times
assistant occurs 1 times
6. Develop a program to sort the contents of a text file and write the
sorted contents into a separate text file. [Hint: Use string methods
strip(), len(), list methods sort(), append(), and file methods
open(), readlines(), and write()].
import os.path
import sys
if not os.path.isfile(fname):
print("File", fname, "doesn’t exists")
sys.exit(0)
myList = infile.readlines()
# print(myList)
#Remove trailing \n characters
lineList = []
for line in myList:
lineList.append(line.strip())
lineList.sort()
outfile = open("sorted.txt","w")
for line in lineList:
outfile.write(line + '\n')
if os.path.isfile("sorted.txt"):
print("\nFile containing sorted content sorted.txt created
successfully")
print("sorted.txt contains", len(lineList), "lines")
print("Contents of sorted.txt")
print("===========================================================")
rdFile = open("sorted.txt","r")
for line in rdFile:
print(line, end="")
output:
curDirectory = pathlib.Path(dirName)
with zipfile.ZipFile("myZip.zip", mode="w") as archive:
for file_path in curDirectory.rglob("*"):
archive.write(file_path,
arcname=file_path.relative_to(curDirectory))
if os.path.isfile("myZip.zip"):
print("Archive", "myZip.zip", "created successfully")
else:
print("Error in creating zip archive")
output:
Enter Directory name that you want to backup : jit
Archive myZip.zip created successfully
import sys
def DivExp(a,b):
assert a>0, "a should be greater than 0"
try:
c = a/b
except ZeroDivisionError:
print("Value of b cannot be zero")
sys.exit(0)
else:
return c
val1 = int(input("Enter a value for a : "))
val2 = int(input("Enter a value for b : "))
val3 = DivExp(val1, val2)
print(val1, "/", val2, "=", val3)
output:
Enter a value for a : 7
Enter a value for b : 6
7 / 6 = 1.1666666666666667
Enter a value for a : 0
Enter a value for b : 10
AssertionError: a should be greater than 0
class Complex:
def init (self, realp = 0, imagp=0):
self.realp = realp
self.imagp = imagp
def readComplex(self):
self.realp = int(input("Enter the real part : "))
self.imagp = int(input("Enter the imaginary part : "))
def showComplex(self):
print('(',self.realp,')','+i','(',self.imagp,')',sep="")
def add2Complex(a,b):
c = a.addComplex(b)
return c
def main():
c1 = Complex(3,5)
c2 = Complex(6,4)
c3 = add2Complex(c1, c2)
for i in range(num):
print("Object", i+1)
obj = Complex()
obj.readComplex()
compList.append(obj)
sumObj = Complex()
for obj in compList:
sumObj = add2Complex(sumObj, obj)
output:
Complex Number 1
(3)+i(5)
Complex Number 2
(6)+i(4)
Sum of two Complex Numbers
(9)+i(9)
10. Develop a program that uses class Student which prompts the
user to enter marks in three subjects and calculates total marks,
percentage and displays the score card details. [Hint: Use list to
store the marks in three subjects and total marks. Use init method to
initialize name, USN and the lists to store marks and total, Use
getMarks() method to read marks into the list, and display() method
to display the score card details.]
class Student:
def init (self, name = "", usn = "", score = [0,0,0,0]):
self.name = name
self.usn = usn
self.score = score
def getMarks(self):
self.name = input("Enter student Name : ")
self.usn = input("Enter student USN : ")
self.score[0] = int(input("Enter marks in Subject 1 : "))
self.score[1] = int(input("Enter marks in Subject 2 : "))
self.score[2] = int(input("Enter marks in Subject 3 : "))
self.score[3] = self.score[0] + self.score[1] + self.score[2]
def display(self):
percentage = self.score[3]/3
spcstr = "=" * 81
print(spcstr)
print("SCORE CARD DETAILS".center(81))
print(spcstr)
print("%15s"%("NAME"), "%12s"%("USN"),"%8s"%"MARKS1",
"%8s"%"MARKS2","%8s"%"MARKS3","%8s"%"TOTAL", "%12s"%
("PERCENTAGE"))
print(spcstr)
print("%15s"%self.name, "%12s"%self.usn, "%8d"%self.score[0],
"%8d"%self.score[1],"%8d"%self.score[2], "%8d"%self.score[3],
"%12.2f"%percentage)
print(spcstr)
def main():
s1 = Student()
s1.getMarks()
s1.display()
main()
output:
Enter student Name : Pooja
Enter student USN : 4jd21cs045
Enter marks in Subject 1 : 85
Enter marks in Subject 2 : 92
Enter marks in Subject 3 : 75
=====================================================================
SCORE CARD DETAILS
=====================================================================
NAME USN MARKS1 MARKS2 MARKS3 TOTAL PERCENTAGE
=====================================================================
Pooja 4jd21cs045 85 92 75 252 84.00
=====================================================================