Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

3. Read N numbers from the console and create a list.

Develop a program to print mean,


variance and
standard deviation with suitable messages.

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 number of elements: 4


Enter number 1: 8
Enter number 2: 9
Enter number 3: 6
Enter number 4: 5
Mean: 7.0
Variance: 2.5
Standard deviation: 1.5811388300841898
4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of
each digit with suitable message.
Program:
number = input("Enter a multi-digit number: ")
# Create a dictionary to store the frequency of each digit
frequency = {}
for digit in number:
if digit.isdigit():
if digit in frequency:
frequency[digit] += 1
else:
frequency[digit] = 1

# Print the frequency of each digit


for digit, count in frequency.items():
print("The frequency of", digit, "is", count)
Output
Enter a multi-digit number: 111559888
The frequency of 1 is 3
The frequency of 5 is 2
The frequency of 9 is 1
The frequency of 8 is 3

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)]

You might also like