1. a. Develop a program to read the student details like Name, USN, and 2. a.
a. Develop a program to generate Fibonacci sequence of length (N). Read N
Marks in three subjects. Display the student details, total marks and from the console.
percentage with suitable messages.
def fib (n):
name = input ('Enter name of the student:') a=0
USN = input ('Enter the USN of the student:') b =1
m1 = int (input ('Enter the marks in the first subject:')) if n == 1:
m2 = int (input ('Enter the marks in the second subject:')) print (a)
m3 = int (input ('Enter the marks in the third subject:')) else:
print(a)
total_marks = m1 + m2 + m3 print(b)
percentage = (total_marks/300) *100
for i in range (2,n):
c=a+b
print ('Student details are:')
a=b
print ('Name is:', name)
b=c
print ('USN is:', USN) print (c)
print ('Marks in the first subject:', m1) fib(5)
print ('Marks in the second subject:', m2)
print ('Marks in the third subject:', m3)
print ('Total Marks obtained:', total_marks) 2. b. Write a function to calculate factorial of a number. Develop a program to
print ('Percentage of marks:', percentage) compute binomialcoefficient (Given N and R).
def fact(num):
1. b. Develop a program to read the name and year of birth of a person.
Display whether the person is asenior citizen or not. if num == 0:
return 1
name = input ('Enter the name of a person:')
year_of_birth = int (input ('Enter the birth year:')) else:
age = 2023-year_of_birth return num * fact(num-1)
print ('The age is:', age) n = int(input("Enter the value of N : "))
if age>60:
r = int(input("Enter the value of R (R cannot be negative or greater than N): "))
print ('The person is senior citizen')
else: nCr = fact(n)/(fact(r)*fact(n-r))
print ('The person is not senior citizen')
print(n,'C',r," = ","%d"%nCr,sep=" ")
3. Read N numbers from the console and create a list. Develop a program to 4. Read a multi-digit number (as chars) from the console. Develop a program
print mean, variance andstandard deviation with suitable messages. to print the frequency of each digit with suitable message.
num = input("Enter a number : ")
from math import sqrt
print("The number entered is :", num)
myList = []
freq = set(num)
num = int(input("Enter the number of elements in your list : "))
for digit in freq:
for i in range(num):
val = int(input("Enter the element : ")) print(digit, "occurs", num.count(digit), "times")
myList.append(val)
print('The length of list is', len(myList))
print('List Contents', myList)
# Mean Calculation
total = 0
for elem in myList:
total += elem
mean = total / num
# Variance Calculation
total = 0
for elem in myList:
total += (elem - mean) * (elem - mean)
variance = total / num
# Standard Deviation Calculation
stdDev = sqrt(variance)
print("Mean =", mean)
print("Variance =", variance)
print("Standard Deviation =", stdDev)
5. Develop a program to print 10 most frequently appearing words in a text text = open("sample.txt", "r")
file. [Hint: Use dictionary with distinct words and their frequency of d = dict()
occurrences. Sort the dictionary in the reverse order of frequency and display for line in text:
dictionary slice of first 10 items] line = line.strip()
line = line.lower()
# Open the file in read mode words = line.split(" ")
text = open("sample.txt", "r") for word in words:
# Create an empty dictionary if word in d:
d = dict() d[word] = d[word] + 1
# Loop through each line of the file else:
d[word] = 1
for line in text:
for key in list(d.keys()):
# Remove the leading spaces and newline character print(key, ":", d[key])
line = line.strip()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Split the line into words
words = line.split(" ")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
# Print the contents of dictionary
for key in list(d.keys()):
print(key, ":", d[key])
6. Develop a program to sort the contents of a text file and write the sorted 7. Develop a program to backing Up a given Folder (Folder in a current
contents into a separate text file. [Hint: Use string methods strip(), len(), list working directory) into a ZIP File by using relevant modules and suitable
methods sort(), append(), and file methods open(), readlines(), and write()]. methods.
import os.path
import sys
import os
fname = input("Enter the filename whose contents are to be sorted : ")#sample file import sys
unsorted.txt also provided import pathlib
import zipfile
infile = open(fname, "r")
dirName = input("Enter Directory name that you want to backup : ")
myList = infile.readlines()
# print(myList) curDirectory = pathlib.Path(dirName)
#Remove trailing \n characters with zipfile.ZipFile("myZip.zip", mode="w") as archive:
lineList = [] for file_path in curDirectory.rglob("*"):
for line in myList: archive.write(file_path, arcname=file_path.relative_to(curDirectory))
lineList.append(line.strip()) if os.path.isfile("myZip.zip"):
print("Archive", "myZip.zip", "created successfully")
lineList.sort() else:
#Write sorted contents to new file sorted.txt print("Error in creating zip archive")
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("sorted.txt contains", len(lineList), "lines")
print("Contents of sorted.txt")
rdFile = open("sorted.txt","r")
for line in rdFile:
print(line, end="")
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)