Write a program for mean,median,mode
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}')
Comments
Leave a comment