{"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 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 Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n statistics.median() Method with Examples in Python<\/span><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n statistics.median() Method in Python: 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. Median is calculated as: Median = (n + 1) \/ 2\u00a0 where n is the number of values in a set of data. The data must first be …<\/p>\nstatistics.median(data)<\/pre>\n
Given list = [10, 20, 40, 15, 30]<\/pre>\n
The median of the given list items [10, 20, 40, 15, 30] = 20<\/pre>\n
The given list is first sorted in ascending order before the calculating \r\nthe median.<\/pre>\n
Given list = [2, 3, 1, 6, 5, 4]<\/pre>\n
The median of the given list items [2, 3, 1, 6, 5, 4] = 3.5<\/pre>\n
Here median = (6+1)\/2 = 3.5<\/pre>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import statistics module using the import keyword.\r\nimport statistics\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [10, 20, 40, 15, 30]\r\n# Pass the given list as an argument to the statistics.median() method that\r\n# computes the median (middle value) of the given list items.\r\n# Store it in another variable.\r\nrslt = statistics.median(gvn_lst)\r\n# Print the median (middle value) of the given list items.\r\nprint(\"The median of the given list items\", gvn_lst, \" = \", rslt)\r\n<\/pre>\n
The median of the given list items [10, 20, 40, 15, 30] = 20<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import statistics module using the import keyword.\r\nimport statistics\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Pass the given list as an argument to the statistics.median() method that\r\n# computes the median (middle value) of the given list items.\r\n# Store it in another variable.\r\nrslt = statistics.median(gvn_lst)\r\n# Print the median (middle value) of the given list items.\r\nprint(\"The median of the given list items\", gvn_lst, \" = \", rslt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 2 3 1 6 5 4\r\nThe median of the given list items [2, 3, 1, 6, 5, 4] = 3.5<\/pre>\n","protected":false},"excerpt":{"rendered":"