Lab Programs Python
Lab Programs Python
1a. 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.
Output:
1b. Develop a program to read the name and year of birth of a person. Display
whether the person is a senior citizen or not.
output:
output:
output:
Mean = 58.4
Variance = 642.64
Standard Deviation = 25.35
4. Read a multi-digit number (as chars) from the console. Develop a program
to print the frequency of each digit with suitable message.
uniqDig = set(num)
#print(uniqDig)
import sys
import string
import os.path
fname = input("Enter the filename : ")
if not os.path.isfile(fname):
print("File", fname, "doesn't exists")
sys.exit(0)
filecontents = ""
for line in infile:
for ch in line:
if ch not in string.punctuation:
filecontents = filecontents + ch
else:
filecontents = filecontents + ' '
wordFreq = {}
wordList = filecontents.split()
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
fname = input("Enter the filename whose contents are to be sorted : ")
if not os.path.isfile(fname):
print("File", fname, "doesn't exists")
sys.exit(0)
infile = open(fname, "r")
myList = infile.readlines()
lineList = []
for line in myList:
lineList.append(line.strip())
lineList.sort()
outfile = open("sorted.txt","w")
for line in lineList:
outfile.write(line + "\n")
infile.close() # Close the input file
outfile.close() # Close the output file
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:
if os.path.isfile("myZip.zip"):
print("Archive", "myZip.zip", "created successfully")
else:
print("Error in creating zip archive")
output:
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
output:
def readComplex(self):
self.realp = int(input("Enter the real part : "))
self.imagp = int(input("Enter the real part : "))
def showComplex(self):
print('(',self.realp,')','+i','(',self.imagp,')',sep="")
def addComplex(self, c2):
c3 = Complex()
c3.realp = self.realp + c2.realp
c3.imagp = self.imagp + c2.imagp
return c3
def add2Complex(a,b):
c = a.addComplex(b)
return c
def main():
c1 = Complex(3,5)
c2 = Complex(6,4)
c3 = add2Complex(c1, c2)
compList = []
num = int(input("\nEnter the value for N : "))
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)
main()
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"%("PE
RCENTAGE"))
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: