- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# harmonic_mean() in Python

Calculating the average of a collection of data is done using the statistical measure of central tendency known as the harmonic mean. The harmonic mean of a given collection of integers may be determined in Python using the harmonic mean() function. Python 3.0 and subsequent versions of the language come with the statistics module, which has this function. We'll go through the **harmonic_mean()** function in Python's syntax, code methodology, and uses.

## Syntax

The statistics module is included in Python 3.0 and later versions, so no installation is required. To use the harmonic_mean() function, simply import the statistics module in your Python program using the following command −

import statistics statistics.harmonic_mean(data)

The “**data**” parameter is a sequence of numbers for which we want to calculate the harmonic mean.

It is nothing but a statistical measure of central tendency that is used to calculate the average of a set of numbers. It is defined as the reciprocal of the arithmetic mean of the reciprocals of a set of numbers. **The harmonic_mean() function calculates the harmonic mean of a given set of numbers using the following formula** −

harmonic_mean = n / (sum(1/x for x in data))

**Note **− n is the number of elements in the data sequence.

To calculate the harmonic mean of a set of numbers [2, 4, 6] −

Harmonic Mean = 3 / (1/2 + 1/4 + 1/6) = 3 / (0.5 + 0.25 + 0.1667) = 3 / 0.9167 = 3.27 (rounded to two decimal places)

## Example

import statistics data = [1, 2, 3, 4, 5] hm = statistics.harmonic_mean(data) print("Harmonic mean is :", hm)

## Output

Harmonic mean is : 2.18978102189781

Import the statistics package to determine the harmonic mean of the preceding set of numbers [1, 2, 3, 4, 5]. The data series are entered into the function harmonic mean(), which calculates a harmonic mean and saves it in the variable hm for display on the console. Using the harmonic mean, you can figure out the average rate of change of a number over time. This is very helpful. For instance, if the distance is not constant, it could be used to determine an automobile's average fuel economy over a specific distance.

## Conclusion

The Python function harmonic_mean(), which is a part of the statistics module and is used to determine the average rate of change of a number over time, can be used to calculate the harmonic mean from a set of integers. Due to its straightforward syntax, easy-tounderstand mechanism, and practical application, the function is a useful addition to any Python programming arsenal.