CBSE Class 11 Informatics Practices Question 25 of 33

Lists in Python — Question 25

Back to all questions
25
Question

Question 20

Write a program to calculate mean of a given list of numbers.

Solution
numbers = eval(input("Enter a list: "))
if len(numbers) == 0:
    print("List is empty, cannot calculate mean.")
else:
    total = sum(numbers)
    n = len(numbers)
    mean = total / n
    print("Mean of the numbers:", mean)
Output
Enter a list: [10, 30, 25, 34, 45, 98]
Mean of the numbers: 40.333333333333336
Answer

numbers
=
eval
(
input
(
"Enter a list: "
))
if
len
(
numbers
)
==
0
:
print
(
"List is empty, cannot calculate mean."
)
else
:
total
=
sum
(
numbers
)
n
=
len
(
numbers
)
mean
=
total
/
n
print
(
"Mean of the numbers:"
,
mean
)
Output
Enter a list: [10, 30, 25, 34, 45, 98]
Mean of the numbers: 40.333333333333336