Problem to calculate the mean in version 3.4

Peter Otten __peter__ at web.de
Fri Sep 25 03:09:16 EDT 2015


Michel Guirguis wrote:

> I have downloaded the version 3.4 and I have a problem to calculate the
> mean. The software does not recognise the function mean(). I am getting
> the following error.
> 
>>>> mean([1, 2, 3, 4, 4])
> Traceback (most recent call last):
>   File "<pyshell#4>", line 1, in <module>
>     mean([1, 2, 3, 4, 4])
> NameError: name 'mean' is not defined
> 
> Could you please help. Is it possible to send me the version that
> calculate statistics.

The help for the statistics and other modules assumes basic knowledge of 
Python. I recommend you read the tutorial or any other introductory text on 
Python before you proceed.

Regarding your actual question: before you can use a function you have to 
import it. There are two ways:

(1) Recommended: import the module and use the qualified name:

>>> import statistics
>>> statistics.mean([1, 2, 3, 4, 4])
2.8

(2) Import the function. Typically done when you want to use it in the 
interactive interpreter, not in a script.

>>> from statistics import mean
>>> mean([1, 2, 3, 4, 4])
2.8





More information about the Python-list mailing list