Calculate Mean, Median, and Mode Python | Assignment with solution

assignment advertisement

Introduction

In this article, you will get the python code solution for mean, median, and mode in python for your assignment or homework.

  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.

Solution for mean, median, and mode in python

#!/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)
    else:
        return sum(arr)/len(arr)

def median(arr):
    if arr == []:
        return None
    arr.sort()
    if len(arr)%2 != 0:
        return arr[len(arr)//2]
    else:
        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())]

Mean, median, and mode python homework solution.

mean median mode in python assignment expert


Share:
Avatar of Harry

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 admin@copyassignment.com Thank you