Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python statistics fmean() Function



The Pythonstatistics.fmean() function converts data to floats by using the arithmetic mean.

The fmean function runs faster than the mean() and always returns a float. This functions contains a sequence of iterable data. Whenever the input data is empty then it throwsStatisticsError.

Syntax

Following is the basic syntax of thestatistics.fmean() function −

statistics.fmean(data, weights=None)

Parameters

Here, the data & weight values can be used as any sequence, list or iterator.

Return Value

This function always returns float arithmetic mean of data which can be a sequence of iterations.

Example 1

In the below example we are calculating the average of the given float data usingstatistics.fmean() function.

import statisticsx = statistics.fmean([3.5, 4.0, 5.25])print(x)

Output

The output obtained is as follows −

4.25

Example 2

Here, we are calculating the fmean of the given weights usingstatistics.fmean() function.

import statisticsweights = [0.20, 0.20, 0.30, 0.30]x = statistics.fmean(weights)print(x)

Output

This produces the following result −

0.25

Example 3

Now, we are passing decimal values to find their fmean usingstatistics.fmean() function.

import statisticsfrom decimal import Decimal as Dx = statistics.fmean([D("0.15"), D("0.175"), D("0.65"), D("0.35")])print(x)

Output

The result is obtained as follows −

0.33125

Example 4

If we pass datatype other than double or integer tostatistics.fmean() function, then it will throw error. In the below example we are passing string value, hence the code returns TypeError.

import statisticsx = statistics.fmean("welcome to tutorialspoint")print(x)

Output

The output obtained is as follows −

Traceback (most recent call last):  File "/home/cg/root/61842/main.py", line 2, in <module>    x = statistics.fmean("welcome to tutorialspoint")  File "/usr/lib/python3.10/statistics.py", line 354, in fmean    total = fsum(data)TypeError: must be real number, not str
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp