Answer to Question #218171 in Python for Krish

Question #218171

Write a program for mean,median,mode


1
Expert's answer
2021-07-18T05:54:29-0400
val = [int(x) for x in input('input space separated integers: ').split()]
val = sorted(val)
if len(val) > 0:
    mean = sum(val) / len(val)
if len(val) == 0:
    median = 0
elif (len(val) % 2) != 0:
    median = val[len(val)//2]
else:
    median = (val[len(val)//2]+val[len(val)//2-1]) / 2
mode = []
m = 2
for el in set(val):
    if val.count(el) > m:
        mode = [el]
        m = val.count(el)
    elif val.count(el) == m:
        mode.append(el)
if len(mode) == len(set(val)):
    mode = []


print(f'mean : {mean}')
print(f'median : {median}')
print(f'mode : {mode}')

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