Python statistics.median() Method
Python statistics.median()Used to calculate the median of a given data set.
Syntax
The syntax of the statistics.median() method is as follows:
statistics.median(data)
Parameter description:
- data-- An iterable containing numeric values, such as a list or tuple.
Return Value
The function calculates and returns the median of the data set as the result.
Example
The following example demonstrates the use of the median() function:
Example
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 calculated using the median() function is 3.
The median is the value located in the middle of a data set. When the size of the data set is odd, the median is the middle value after sorting; when the size of the data set 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 it is more robust than the mean (insensitive to outliers).
Other Extensions
Python statistics Module