statistics.median

Full name
statistics.median
Library
statistics
Syntax

statistics.median(data)

Description

The statistics.median function returns the median of the elements in data. For its calculation, the ordered elements are considered: If the number of elements is odd, the median is the value that occupies the central position. If the number of elements is even, the median is the mean value of the two values closest to the center position.

Parameters
  • data: Sequence or iterable from whose elements you want to calculate the median.
Result

The statistics.median function returns an integer or real number.

Examples

We can obtain the median of the numbers contained in a list with the following code:

statistics.median([1, 3, 6, 11, 11])
6

In this case, the number of elements is odd, so the function returns the value that occupies the central position if we consider them ordered (in the example the values are already ordered to make its interpretation easier).

We see how the result is an integer.

If the number of elements is even, the mean value of the two values closest to the center position is returned (values 3 and 4 in the following example):

statistics.median([1, 3, 4, 11])
3.5

In this case the result returned by the function is a real number.

Submitted by admin on Fri, 03/26/2021 - 08:34