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

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots