write a program find mean,median and mode by using functions in python
def mean(numbers):
return round(sum(numbers) / len(numbers), 2)
def median(numbers):
index = len(numbers) // 2
if len(numbers) % 2:
return sorted(numbers)[index]
else:
return sum(sorted(numbers)[index - 1:index + 1]) / 2
def mode(numbers):
counts = dict.fromkeys(set(numbers))
for key in counts:
counts[key] = numbers.count(key)
mode_value = sorted(counts.items(), key=lambda x: x[1], reverse=True)[0]
if mode_value[1] != 1:
return mode_value[0]
else:
return "No mode!"
def main(numbers):
print(f"Mean - {mean(numbers)}")
print(f"Median - {median(numbers)}")
print(f"Mode - {mode(numbers)}")
if __name__ == "__main__":
numbers = [3, 5, 1, 4, 5, 20, 6]
main(numbers)
Comments
Leave a comment