median()
function is from Standard statistics Library of Python programming language. The basic purpose of this function is to calculate the median of given numeric data. The given data will always be in the form of sequence or iterator. A median is the middle value of data.
Following is the statistical formula to calculate the median.
Median = {(n + 1) / 2}th Value
The statistics median is a quick measure to find a central location of a data sequence, list or any iterator. It is less affected by the presence of outliers in your data.
Note:
- When the number of data points in given list or sequence or iterator is odd, the exact middle data point (number) is returned.
- When the number of data points in given list or sequence or iterator is odd, the exact middle data point (number) is returned.
Syntax of median()
function in Python
The syntax of
median()
function in Python is as follows:statistics.mean(data)
Parameters of median()
function in Python
data
– This parameter can be any sequence or iterator (lists, tuples, etc…)Note: If
data
is empty, median()
function raise a StatisticsError
Return Value of median()
in Python
Return value of
median()
is a floating point number after calculating median of given data
in iterator(lists, tuples)
median()
Function Examples in Python
# import statistics library import statistics print(statistics.mean([1,2,3,4,5])) print(statistics.mean([-1.0,2.7,-3.9,5.8]))
Output of median()
Function in Python
3
0.8500000000000001