{"id":25459,"date":"2021-11-16T08:39:18","date_gmt":"2021-11-16T03:09:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=25459"},"modified":"2021-11-16T08:39:18","modified_gmt":"2021-11-16T03:09:18","slug":"python-statistics-median-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-statistics-median-method-with-examples\/","title":{"rendered":"Python statistics.median() Method with Examples"},"content":{"rendered":"

statistics.median() Method in Python:<\/strong><\/p>\n

The statistics.median() method computes the given data set’s median (middle value). Before calculating the median, this method sorts the data in ascending order.<\/p>\n

Median is calculated as:<\/p>\n

Median = (n + 1) \/ 2\u00a0<\/strong><\/p>\n

where n is the number of values in a set of data.<\/p>\n

The data must first be sorted in ascending order before the median can be calculated. The median is the number that falls in the middle.<\/p>\n

Note:<\/strong> It should be noted that if the number of data values is odd, it will return the exact middle value. If the number of data values is even, the average of the two middle values is returned.<\/p>\n

Syntax:<\/strong><\/p>\n

statistics.median(data)<\/pre>\n

Parameters<\/strong><\/p>\n

data:<\/strong> This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).<\/p>\n

Note:<\/strong> It is to be noted that if the data is empty, it returns a StatisticsError.<\/p>\n

Return Value:<\/strong><\/p>\n

Returns a float value representing the data’s median (middle value).<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given list = [10, 20, 40, 15, 30]<\/pre>\n

Output:<\/strong><\/p>\n

The median of the given list items [10, 20, 40, 15, 30]  =  20<\/pre>\n

Note:<\/strong><\/p>\n

The given list is first sorted in ascending order before the calculating \r\nthe median.<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given list = [2, 3, 1, 6, 5, 4]<\/pre>\n

Output:<\/strong><\/p>\n

The median of the given list items [2, 3, 1, 6, 5, 4] = 3.5<\/pre>\n

Explanation:<\/strong><\/p>\n

Here median = (6+1)\/2 = 3.5<\/pre>\n

statistics.median() Method with Examples in Python<\/span><\/p>\n