median python 2 code example
Example 1: statistics mode python when no.s are same
import statistics
statistics.mode(x) #mean and median are also available.
Example 2: finding median on python
def calculate_median(n):
N = len(n)
n.sort()
#find the median
if N % 2 == 0:
#if N is even
m1 = N / 2
m2 = (N / 2) + 1
#Convert to integer, match post
m1 = int(m1) - 1
m2 = int(m2) - 1
median = (n[m1] + n[m2]) / 2
else:
m = (N + 1) / 2
# Convert to integer, match position
m = int(m) - 1
median = n[m]
return median
# Doing Math With Python