Python Lab
Python Lab
1
22plc15b python
Question 5
Word Frequency
Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use dictio-
nary 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]
12 import sys
13 import string
14 import os.path
18 if not os.path.isfile(fname):
24 filecontents = ""
27 for ch in line:
28 if ch not in string.punctuation:
29 filecontents = filecontents + ch
30 else:
31 filecontents = filecontents + ’ ’
33 wordFreq = {}
34 wordList = filecontents.split()
49 print("\n===================================================")
50 print("10 most frequently appearing words with their count")
51 print("===================================================")
52 for i in range(10):
53 print(sortedWordFreq[i][0], "occurs", sortedWordFreq[i][1], "times")
9
22plc15b python
Question 6
Sort File Contents
Develop a program to sort the contents of a text file and write the sorted contents into a separatetext
file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods open(),
readlines(), and write()].
15 import os.path
16 import sys
18 fname = input("Enter the filename whose contents are to be sorted : ")
20 if not os.path.isfile(fname):
26 myList = infile.readlines()
30 lineList = []
32 lineList.append(line.strip())
34 lineList.sort()
38 outfile = open("sorted.txt","w")
42 outfile.write(line + "\n")
44 infile.close()
45 outfile.close()
48 if os.path.isfile("sorted.txt"):
11
22plc15b python
Question 10
Student Class Demo
Develop a program that uses class Student which prompts the user to enter marks in three sub-
jects 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, USNand
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.]
13 class Student:
14 def init (self, name = "", usn = "", score = [0,0,0,0]):
15 self.name = name
16 self.usn = usn
17 self.score = score
19 def getMarks(self):
20 self.name = input("Enter student Name : ")
21 self.usn = input("Enter student USN : ")
22 self.score[0] = int(input("Enter marks in Subject 1 : "))
23 self.score[1] = int(input("Enter marks in Subject 2 : "))
24 self.score[2] = int(input("Enter marks in Subject 3 : "))
25 self.score[3] = self.score[0] + self.score[1] + self.score[2]
27 def display(self):
28 percentage = self.score[3]/3
29 spcstr = "=" * 81
30 print(spcstr)
31 print("SCORE CARD DETAILS".center(81))
print(spcstr)
33 print("%15s"%("NAME"), "%12s"%("USN"), "%8s"%"MARKS1","%8s"%"MARKS2","%8s"%"
MARKS3","%8s"%"TOTAL","%12s"%("PERCENTAGE"))
34 print(spcstr)
35 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)
36 print(spcstr)
39 def main():
40 s1 = Student()
41 s1.getMarks()
42 s1.display()
main()