Professional Documents
Culture Documents
3 - 4 - 5th Program
3 - 4 - 5th Program
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
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]
Program:
def word_count(file_name):
with open(file_name, 'r') as f:
text = f.read()
words = text.split()
word_frequency = {}
for word in words:
if word in word_frequency:
word_frequency[word] += 1
else:
word_frequency[word] = 1
sorted_words = sorted(word_frequency.items(), key=lambda x: x[1],
reverse=True)
return sorted_words[:10]
file_name = 'example.txt'
print(word_count(file_name))
Output
[('object-oriented', 2), ('programming', 2), ('It', 2), ('dynamic', 2), ('and', 2), ('Python', 1), ('is',
1),
('an', 1), ('interpreted,', 1), ('interactive,', 1)]