The Python statistics module has a number of useful functions to calculate mode, such as median(), median_high(), median_low() and median_grouped() function. Lets write some code snippets and understand how to use the median function in Python statistics module.

Calculating median value of given data

The median value of a given data set is the middle value of the data. Median can be calculated by sorting the numbers in ascending order and finding the value in the middle of the list. There are various functions available in the Python statistics module to calculate the median as given below.

  • median() function
  • median_high() function
  • median_low() function
  • median_grouped() function

Using the median() function

The median() function calculates the median value of a given list of numbers. The numbers are sorted in ascending order by the function before it calculates the median. If the count of the number of values is odd, the middle value is returned as the median. If the count of the number of values is even, then there are 2 values in the middle and their average is returned as the median. The below code snippet explains the usage and syntax of median() function.

# using the median() function on a given list of numbers
import statistics
numbers1 = [10, 20, 30, 40, 50]               #Scenario 1 - odd number of values
median1 = statistics.median(numbers1)
print("The given numbers are ", numbers1)
print("The median value is ", median1)        # returns 30
numbers2 = [10, 20, 30, 40, 50, 60]
median2 = statistics.median(numbers2)
print("The given numbers are ", numbers2)
print("The median value is ", median2)        # returns 35.0 [(30+40)/2=35]

Using the median_low() function

The median_low() function can be used in scenarios where the median value should be a part of the data values. In case of odd number of data values, the middle value is returned as the median. In case of even number of data values, there are 2 values in the middle, and the function returns the smaller of the 2 middle values.

# using the median_low() function on a given list of numbers
import statistics
numbers1 = [10, 20, 30, 40, 50]                 #Scenario 1 - odd number of values
median1 = statistics.median_low(numbers1)
print("The given numbers are ", numbers1)
print("The median value is ", median1)          # returns 30
numbers2 = [10, 20, 30, 40, 50, 60]             #Scenario 2 - even number of values
median2 = statistics.median_low(numbers2)
print("The given numbers are ", numbers2)
print("The median value is ", median2)          # returns 30 [lower of 30 and 40]

Using the median_high() function

The median_high() function can be used in scenarios where the median value should be a part of the data values. In case of odd number of data values, the middle value is returned as the median. In case of even number of data values, there are 2 values in the middle, and the function returns the higher of the 2 middle values.

# using the median_high() function on a given list of numbers
import statistics
numbers1 = [10, 20, 30, 40, 50]                 #Scenario 1 - odd number of values
median1 = statistics.median_high(numbers1)
print("The given numbers are ", numbers1)
print("The median value is ", median1)          # returns 30
numbers2 = [10, 20, 30, 40, 50, 60]             #Scenario 2 - even number of values
median2 = statistics.median_high(numbers2)
print("The given numbers are ", numbers2)
print("The median value is ", median2)          # returns 40 [higher of 30 and 40]

Similar Programs

Last modified: March 22, 2023

Author