statistics.harmonic_mean() Method:
The statistics.harmonic_mean() method determines the harmonic mean (central location) of a given data set.
Harmonic mean = The reciprocal of the arithmetic mean() of the data’s reciprocals.
The harmonic mean is computed in the following way:
If you have four values (a, b, c, and d), the result is 4 / (1/a + 1/b + 1/c + 1/d).
Syntax:
statistics.harmonic_mean(data)
Parameters
data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).
Note:
- Negative values are not permitted.
- If the data is null or empty, it throws a StatisticsError.
Return Value:
Returns a float value corresponding to the harmonic mean of the given data.
Examples:
Example1:
Input:
Given list = [30, 50, 100]
Output:
The harmonic mean of the given list items [30, 50, 100] = 47.36842105263158
Example2:
Input:
Given list = [20, 40, 60, 80]
Output:
The harmonic mean of the given list items [20, 40, 60, 80] = 38.4
statistics.harmonic_mean() 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.harmonic_mean() method that determines the harmonic mean (central location) of a given data set (given list items).
- Store it in another variable.
- Print the harmonic mean (central location) 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 = [30, 50, 100] # Pass the given list as an argument to the statistics.harmonic_mean() method # that determines the harmonic mean (central location) of a given # data set (given list items). # Store it in another variable. rslt = statistics.harmonic_mean(gvn_lst) # Print the harmonic mean (central location) of the given list items. print("The harmonic mean of the given list items", gvn_lst, " = ", rslt)
Output:
The harmonic mean of the given list items [30, 50, 100] = 47.36842105263158
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.harmonic_mean() method that determines the harmonic mean (central location) of a given data set (given list items).
- Store it in another variable.
- Print the harmonic mean (central location) 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.harmonic_mean() method # that determines the harmonic mean (central location) of a given # data set (given list items). # Store it in another variable. rslt = statistics.harmonic_mean(gvn_lst) # Print the harmonic mean (central location) of the given list items. print("The harmonic mean of the given list items", gvn_lst, " = ", rslt)
Output:
Enter some random List Elements separated by spaces = 20 40 60 80 The harmonic mean of the given list items [20, 40, 60, 80] = 38.4