python manual_CSE
python manual_CSE
python manual_CSE
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.
stName = input("Enter the name of the student : ")
stUSN = input("Enter the USN of the student : ")
stMarks1 = int(input("Enter marks in Subject 1 : "))
stMarks2 = int(input("Enter marks in Subject 2 : "))
stMarks3 = int(input("Enter marks in Subject 3 : "))
print("Student Details\n=========================")
print("%12s"%("Name :"), stName)
print("%12s"%("USN :"), stUSN)
print("%12s"%("Marks 1 :"), stMarks1)
print("%12s"%("Marks 2 :"), stMarks2)
print("%12s"%("Marks 3 :"), stMarks3)
print("%12s"%("Total :"), stMarks1+stMarks2+stMarks3)
print("%12s"%("Percent :"), "%.2f"%((stMarks1+stMarks2+stMarks3)/3))
print("=========================")
OUTPUT
Enter the name of the student : RAMESH
Enter the USN of the student : 1SI22CS036
Enter marks in Subject 1 : 87
Enter marks in Subject 2 : 78
Enter marks in Subject 3 : 65
Student Details
=========================
Name : RAMESH
USN : 1SI22CS036
Marks 1 : 87
Marks 2 : 78
Marks 3 : 65
Total : 230
Percent : 76.67
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
Enter the name of the person : Akbar Khan
Enter his year of birth : 1978
Akbar Khan aged 44 years is not a Senior Citizen.
OUTPUT
Enter the Fibonacci sequence length to be generated : 8
The Fibonacci series with 8 terms is :
0 1 1 2 3 5 8 13
def fact(num):
if num == 0:
return 1
else:
return num * fact(num-1)
print(n,'C',r," = ","%d"%nCr,sep="")
OUTPUT
Enter the value of N : 7
Enter the value of R (R cannot be negative or greater than N): 5
7C5 = 21
3 Read N numbers from the console and create a list. Develop a program to
print mean, variance and standard deviation with suitable messages.
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)
OUTPUT
OUTPUT
Enter the filename : text.txt
===================================================
10 most frequently appearing words with their count
===================================================
the occurs 45 times
of occurs 24 times
party occurs 12 times
part occurs 12 times
a occurs 9 times
and occurs 8 times
second occurs 7 times
to occurs 6 times
shall occurs 6 times
first occurs 5 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
fname = input("Enter the filename whose contents are to be sorted : ") #sample file unsorted.txt
also provided
if not os.path.isfile(fname):
print("File", fname, "doesn't exists")
sys.exit(0)
infile = open(fname, "r")
myList = infile.readlines()
# print(myList)
#Remove trailing \n characters
lineList = []
for line in myList:
lineList.append(line.strip())
lineList.sort()
#Write sorted contents to new file sorted.txt
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
Enter the filename whose contents are to be sorted : unsorted.txt
File containing sorted content sorted.txt created successfully
sorted.txt contains 15 lines
Contents of sorted.txt
=================================================================
A deep C diva.
All the troubles you have will pass away very quickly.
import os
import sys
import pathlib
import zipfile
dirName = input("Enter Directory name that you want to backup : ")
if not os.path.isdir(dirName):
print("Directory", dirName, "doesn't exists")
sys.exit(0)
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
class PaliStr:
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
class Complex:
def __init__(self, realp = 0, imagp=0):
self.realp = realp
self.imagp = imagp
def setComplex(self, realp, imagp):
self.realp = realp
self.imagp = imagp
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)
print("Complex Number 1")
c1.showComplex()
print("Complex Number 2")
c2.showComplex()
c3 = add2Complex(c1, c2)
print("Sum of two Complex Numbers")
c3.showComplex()
#Addition of N (N >=2) complex numbers
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)
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"%("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 : Shivappa
Enter student USN : 1SI22CS065
Enter marks in Subject 1 : 87
Enter marks in Subject 2 : 79
Enter marks in Subject 3 : 92
======================================================================
SCORE CARD DETAILS
======================================================================
NAME USN MARKS1 MARKS2 MARKS3 TOTAL PERCENTAGE
======================================================================
Shivappa 1SI22CS065 87 79 92 258 86.00
======================================================================