Skip to content

Instantly share code, notes, and snippets.

@eLtronicsVilla
Created February 27, 2019 12:38
Show Gist options
  • Save eLtronicsVilla/733a7e478700679f31128312dcff407c to your computer and use it in GitHub Desktop.
Save eLtronicsVilla/733a7e478700679f31128312dcff407c to your computer and use it in GitHub Desktop.
Calculate the median of the given data set
# This is sample code,function to calculate the median of given dataset
data = [2,3,4,5,6,8,9]
def median(value):
# find middle most value for even or odd data set
n = len(value) # find out the length
sorted_value = sorted(value) #sort the value
mid_p = n // 2 # find out the mid point
if n % 2 == True:
# if odd return the middle value
return sorted_value[mid_p]
else:
# if even value then take the average of middle values
low = mid_p-1
high = mid_p
return (sorted_value[low] + sorted_value[high]) / 2
median(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment