Programs
Programs
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.
total_marks = m1 + m2 + m3
percentage = (total_marks/300) *100
def fact(num):
if num == 0:
return 1
else:
return num * fact(num-1)
n = int(input("Enter the value of N : "))
r = int(input("Enter the value of R (R cannot be negative or greater than N): "))
nCr = fact(n)/(fact(r)*fact(n-r))
print(n,'C',r," = ","%d"%nCr,sep=" ")
3. Read N numbers from the console and create a list. Develop a program to
print mean, variance andstandard deviation with suitable messages.
import os.path
import sys
fname = input("Enter the filename whose contents are to be sorted : ")#sample file
unsorted.txt also provided
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")
if os.path.isfile("sorted.txt"):
print("sorted.txt contains", len(lineList), "lines")
print("Contents of sorted.txt")
rdFile = open("sorted.txt","r")
for line in rdFile:
print(line, end="")
7. Develop a program to backing Up a given Folder (Folder in a current
working directory) into a ZIP File by using relevant modules and suitable
methods.
import os
import sys
import pathlib
import zipfile
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")
8. Write a function named DivExp which takes TWO parameters a, b and
returns a value c (c=a/b). Write suitable assertion for a>0 in function DivExp
and raise an exception for when b=0. Develop a suitable program which reads
two values from the console and calls a function DivExp
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)