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.
(01AStudDetails.py)
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 : pavan
Enter the USN of the student : 4gm20cs001
Enter marks in Subject 1 : 23
Enter marks in Subject 2 : 25
Enter marks in Subject 3 : 22
Student Details
=========================
Name : pavan
USN : 4gm20cs001
Marks 1 : 23
Marks 2 : 25
Marks 3 : 22
Total : 70
Percent : 23.33
=========================
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.
(01BChkSnrCitzn.py)
from datetime import date
perName = input("Enter the name of the person : ")
perDOB = int(input("Enter his year of birth : "))
curYear = date.today().year
perAge = curYear - perDOB
if (perAge > 60):
print(perName, "aged", perAge, "years is a Senior Citizen.")
else:
print(perName, "aged", perAge, "years is not a Senior Citizen.")
Output:
Enter the name of the person : kiran
Enter his year of birth : 1983
kiran aged 42 years is not a Senior Citizen.
2a). Develop a program to generate Fibonacci sequence of length (N). Read N from the
console.
(02AFibonacci.py)
num = int(input("Enter the Fibonacci sequence length to be generated : "))
firstTerm = 0
secondTerm = 1
print("The Fibonacci series with", num, "terms is :")
print(firstTerm, secondTerm, end=" ")
for i in range(2,num):
curTerm = firstTerm + secondTerm
print(curTerm, end=" ")
firstTerm = secondTerm
secondTerm = curTerm
Output:
Enter the Fibonacci sequence length to be generated : 10
The Fibonacci series with 10 terms is : 0 1 1 2 3 5 8 13 21 34
2b). Write a function to calculate factorial of a number. Develop a program to compute
binomial coefficient (Given N and R).
(02BFactNCR.py)
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="")
Output:
Enter the value of N : 5
Enter the value of R (R cannot be negative or greater than N): 2
5C2 = 10
3). Read N numbers from the console and create a list. Develop a program to print mean,
variance and standard deviation with suitable messages.
(03MeanVarSD.py)
from math import sqrt
myList = [ ]
num = int(input("Enter the number of elements in your list : "))
for i in range(num):
val = int(input("Enter the element : "))
myList.append(val)
print('The length of list1 is', len(myList))
print('List Contents', myList)
total = 0
for elem in myList:
total += elem
mean = total / num
total = 0
for elem in myList:
total += (elem - mean) * (elem - mean)
variance = total / num
stdDev = sqrt(variance)
print("Mean =", mean)
print("Variance =", variance)
print("Standard Deviation =", "%.2f"%stdDev)
Output:
Enter the number of elements in your list : 6
Enter the element : 1
Enter the element : 2
Enter the element : 3
Enter the element : 4
Enter the element : 5
Enter the element : 6
The length of list1 is 6
List Contents [1, 2, 3, 4, 5, 6]
Mean = 3.5
Variance = 2.9166666666666665
Standard Deviation = 1.71
4). Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.
(04DigitFreq.py)
num = input("Enter a number : ")
print("The number entered is :", num)
uniqDig = set(num)
#print(uniqDig)
for elem in uniqDig:
print(elem, "occurs", num.count(elem), "times")
Output :
Enter a number : 1010101222
The number entered is : 1010101222
1 occurs 4 times
0 occurs 3 times
2 occurs 3 times
5). Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use
dictionary with distinct words and their frequency of occurrences. Sort the dictionary in the
reverse order of frequency and display dictionary slice of first 10 items]
(05WordFreq.py)
import sys
import string
import os.path
fname = input("Enter the filename : ") #sample file text.txt also provided
if not os.path.isfile(fname):
print("File", fname, "doesn't exists")
sys.exit(0)
infile = open(fname, "r")
filecontents = ""
for line in infile:
for ch in line:
if ch not in string.punctuation:
filecontents = filecontents + ch
else:
filecontents = filecontents + ' ' #replace punctuations and newline with a space
wordFreq = {}
wordList = filecontents.split()
for word in wordList:
if word not in wordFreq.keys():
wordFreq[word] = 1
else:
wordFreq[word] += 1
sortedWordFreq = sorted(wordFreq.items(), key=lambda x:x[1], reverse=True )
print("\n===================================================")
print("10 most frequently appearing words with their count")
print("===================================================")
for i in range(10):
print(sortedWordFreq[i][0], "occurs", sortedWordFreq[i][1], "times")
Output:
Enter the filename : test.py
print occurs 9 times
12s occurs 7 times
input occurs 5 times
Enter occurs 5 times
the occurs 4 times
stMarks1 occurs 4 times
stMarks2 occurs 4 times
stMarks3 occurs 4 times
int occurs 3 times
marks occurs 3 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()].
(06SortLinesFile.py)