# Working with statistics.fmean() function in Python

In statistics, mean (also known as average) is a central tendency measurement of a set of values. Mean is calculated by adding up all the values in a set and dividing the result by the number of values. In Python, the statistics module provides a built-in function named fmean() to calculate the mean of a set of values.

## Overview

### Syntax

The fmean() function has the following syntax:

``statistics.fmean(data, weights=None)``

Where:

• data is a sequence (list, tuple, etc.) of numerical values. Note that if the data sequence is empty or contains non-numeric values, fmean() will raise a StatisticsError exception.
• weights: An optional parameter that is used to weigh the possibility for each value of data. If weights is not None, it must be the same length as the data.

The function will always return a float.

### fmean() vs mean(): The difference

Both of these functions return the arithmetic mean of data. However, fmean() runs faster and supports weights.

## Examples

Hereâ€™s an example of how to use the fmean() function to calculate the mean of a set of values:

``````import statistics

# List of values
data = [1, 2, 3, 4, 5, 6]

# Calculate the mean
mean = statistics.fmean(data)

# Print the mean
print("Mean:", mean)``````

Output:

``Mean: 3.5``

In the following example, weights will be useful. We will calculate the average salary of employees in a software company. This company has 24 software developers with a salary of 10000, 3 project managers with a salary of 14000, and 1 CEO with a salary of 30,000.:

``````import statistics

# 24 software developers with salary = 100000
# 3 project managers with salary = 140000
# 1 CEO with salary = 300000

mean_salary = statistics.fmean(
data=[10000, 14000, 30000],
weights=[24, 3, 1])
print(mean_salary)``````

Output:

``11142.857142857143``

Search tutorials, examples, and resources