Python statistics.median() Method with Examples

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 

where n is the number of values in a set of data.

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.

Note: 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.

Syntax:

statistics.median(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the data’s median (middle value).

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The median of the given list items [10, 20, 40, 15, 30]  =  20

Note:

The given list is first sorted in ascending order before the calculating 
the median.

Example2:

Input:

Given list = [2, 3, 1, 6, 5, 4]

Output:

The median of the given list items [2, 3, 1, 6, 5, 4] = 3.5

Explanation:

Here median = (6+1)/2 = 3.5

statistics.median() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median() method that computes the median (middle value) of the given list items.
  • Store it in another variable.
  • Print the median (middle value) of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.median() method that
# computes the median (middle value) of the given list items.
# Store it in another variable.
rslt = statistics.median(gvn_lst)
# Print the median (middle value) of the given list items.
print("The median of the given list items", gvn_lst, " = ", rslt)

Output:

The median of the given list items [10, 20, 40, 15, 30]  =  20

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.median() method that computes the median (middle value) of the given list items.
  • Store it in another variable.
  • Print the median (middle value) of the given list items.
  • The Exit of the Program

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median() method that
# computes the median (middle value) of the given list items.
# Store it in another variable.
rslt = statistics.median(gvn_lst)
# Print the median (middle value) of the given list items.
print("The median of the given list items", gvn_lst, " = ", rslt)

Output:

Enter some random List Elements separated by spaces = 2 3 1 6 5 4
The median of the given list items [2, 3, 1, 6, 5, 4] = 3.5