Answer to Question #319771 in Python for Vivek

Question #319771

2 6 9 8 5 2 4 6 8 1 8


Find mean, median, mode.

1
Expert's answer
2022-03-28T14:28:57-0400
print("Enter list (separated by a space):")
a = input().split()
mean = 0
for i in range(len(a)):
    a[i] = int(a[i])
    mean += a[i]
mean = mean / len(a)
print(f"Mean: {mean:.2f}")
median = 0
a.sort()
print("Sort list:")
print(a)
if (len(a) % 2) == 0:
    median = (a[(len(a)-1)//2] + a[((len(a)-1)//2)+1]) / 2
else:
    median = a[((len(a)-1)//2) + 1]
print(f"Median: {median}")
mode = 0
mode_cnt = [0] * 10
for i in range(len(a)):
    mode_cnt[a[i]] += 1
max_cnt = mode_cnt[0]
for i in range(len(mode_cnt)):
    if max_cnt < mode_cnt[i]:
        max_cnt = mode_cnt[i]
print("Mode:",end=" ")
for i in range(len(mode_cnt)):
    if max_cnt == mode_cnt[i]:
        print(f"{i}", end=" ")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS