Calculate Mean, Median, and Mode Python

assignment advertisement
  1. Mean– mean is usually the average of all the elements(number) divided by the count of elements.
  2. Median– the median is the middle number among all the numbers divided by the count or frequency of the number. Like in 2, 3, 4, 5, 5, 6, 7, and 8, 5/2 will be the median.
  3. Mode– the mode is the middle number among all the numbers. Like in 2, 3, 4, 5, 5, 6, 7, and 8, 5 will be the mode.

#!/usr/bin/env python

def mean(arr, geometric= False):
    if arr == []:
        return None
    if geometric:
        prod = 1
        for i in arr:
            prod *= i
        return round(prod**(1/len(arr)), 2)
        return sum(arr)/len(arr)

def median(arr):
    if arr == []:
        return None
    if len(arr)%2 != 0:
        return arr[len(arr)//2]
        return (arr[len(arr)//2 - 1] + arr[len(arr)//2])/2

def mode(arr):
    if arr == []:
        return None
    counts = {}
    for i in set(arr):
        count = arr.count(i)
        counts[count] = i
    return counts[max(counts.keys())]

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at Thank you