Sling Academy
Home/Python/Working with statistics.fmean() function in Python

Working with statistics.fmean() function in Python

Last updated: February 07, 2023

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

Next Article: Python: Defining Functions with Type Hints

Previous Article: Python Linked Lists: Explanation & Examples

Series: Basic Python Tutorials

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types