Table of Contents

About

The median is a measure of center. The middle number of a set of data is the median.

This measure is resistant.

The median is a 50th percentile (or “middle” quartile). Half of the data is below the median.

How to calculate a median ?

With an uneven number of numbers

  • Score: 7, 5, 8, 9, 5
  • Ordered Score (after a sort): 5, 5, 7, 8, 9

The middle number is the third score, or 7, so the median of this data is 7.

With an even number of numbers

  • Score: 5, 8, 9, 5
  • Ordered Score: 5, 5, 8, 9

When there is an even number of numbers, there is no true value in the middle.

The median is then the mean of the two middle numbers of (5 + 8)=13/2 = 6,5.

If there are four students sitting in a row, the middle of the row is halfway between the second and third students.

Example

Python

def median(sequence):
    isEven = True
    if len(sequence) % 2 == 0:
        isEven = True
    else:
        isEven = False
    
    sequence.sort()
    # - 1
    index_average = int(round(len(sequence)/2))
    
    if isEven:
        median = (sequence[index_average-1]+sequence[index_average])/2.00
    else:
        median = sequence[index_average]
    return median

Documentation / Reference