What is statistics.fmean(data) in Python?

Overview

This fmean() method is used to compute the float mean of input values. It converts input data into float values and calculates the arithmetic mean. This method is faster than the mean() method in older versions of Python.

Note: The new version of Python is version 3.8.

Syntax


statistics.fmean(data)

Parameters

It takes a single argument value:

  • data: It can be an iterable or sequence.

Return value

It returns an arithmetic mean as a floating-point value:

  • StatisticsError: If the dataset or data sequence is empty, then it raises a statistics error for a single data point.

Code example 1

# load statistice module
import statistics
# demo data as list
data = [3.52, 4.0, 5.25]
result = statistics.fmean(data)
print(result)

Explanation

In the code snippet above:

  • Line 2: We load the statistics module in our program.
  • Line 4: We create a list with demo data points.
  • Line 5: We use the statistics.fmean() method to compute floating mean.
  • Line 6: We print result (mean value) on the console as a float value.

Code example 2

# load statistice module
import statistics
# empty data sequence or list
data = []
result = statistics.fmean(data) # raises exception
# statistics.StatisticsError:
# fmean requires at least one data point
print(result)

Explanation

In the code snippet above:

  • Line 2: We load the statistics module in our program.
  • Line 4: We create an empty list.
  • Line 5: We use the statistics.fmean() method to compute the floating mean.

Due to an empty list, it raises the statistics.StatisticsError error. The fmean() requires at least one data point.

Free Resources