Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python statistics.median() Method

Python statistics.median() Method

Python statistics Module Python statistics Module

Python statistics.median() Used to compute the median of a given data set.

Syntax

The syntax of the statistics.median() method is as follows:

statistics.median(data)

Parameter Description:

Return Value

The function computes and returns the median of the data set as the result.

Example

The following example demonstrates the median() function:

Example

import

statistics

data

=

[

1

,

2

,

3

,

4

,

5

]

median

=

statistics.

median

(

data

)

print

(

median

)

Output result:



3

In the above example, the given data set is [1, 2, 3, 4, 5], and the median() function computes a median of 3.

The median is the value in the middle of a set of data. When the data set has an odd size, the median is the middle value after sorting; when the size is even, the median is the average of the two middle values after sorting.

The median is often used to measure the central tendency of a data set and is more robust than the mean (insensitive to outliers).

Python statistics Module Python statistics Module