Python statistics.median_high() Method with Examples

statistics.median_high() Method in Python:

The statistics.median_high() method computes the data set’s high median. Before calculating the high median, the data is also sorted in ascending order using this method.

Note: It should be noted that if the number of data values is odd, it will return the exact middle value. If there are an even number of data values, it returns the larger of the two middle values.

Syntax:

statistics.median_high(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 high median (middle value).

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

Given list = [2, 1, 3, 5, 7, 8]

Output:

The high median of the given list items [2, 1, 3, 5, 7, 8] = 5

statistics.median_high() 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_high() method that computes the high median of the given list items.
  • Store it in another variable.
  • Print the high median 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, 13, 17]
# Pass the given list as an argument to the statistics.median_high() method that
# computes the high median of the given list items.
# Store it in another variable.
rslt = statistics.median_high(gvn_lst)
# Print the high median of the given list items.
print("The high median of the given list items", gvn_lst, "= ", rslt)

Output:

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

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_high() method that computes the high median of the given list items.
  • Store it in another variable.
  • Print the high median 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_high() method that
# computes the high median of the given list items.
# Store it in another variable.
rslt = statistics.median_high(gvn_lst)
# Print the high median of the given list items.
print("The high median of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 2 1 3 5 7 8
The high median of the given list items [2, 1, 3, 5, 7, 8] = 5