Lab Manual
Lab Manual
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:
Output:
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
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)
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)
uniqDig = set(num)
#print(uniqDig)
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