Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 Statistics module - NameError: name 'statistics' is not defined

While learning to use "statistics module" in python 3.6 I am facing the following error: NameError: name 'statistics' is not defined I am just testing statistics basic functions which should return mean, median, mode, stdev, variance. I am new to Python and I can't find where the error is.

Code:

from statistics import *

example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]

x = statistics.mean(example_list)
print(x)

y = statistics.median(example_list)
print(y)

z = statistics.mode(example_list)
print(z)

a = statistics.stdev(example_list)
print(a)

b = statistics.variance(example_list)
print(b)

What am I doing wrong?

like image 386
Claudio Lopez Avatar asked Dec 31 '17 19:12

Claudio Lopez


2 Answers

If I do this in IDLE, all works as expected.

>>> from statistics import *
>>> example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]
>>> x = mean(example_list)
>>> x
4.230769230769231

So I don't get the error you report at x = mean(example_list).

You haven't reported your stack trace (why not?) so it's not possible for me to tell, but I suspect you have named your test program statistics.py, and that is hiding the real statistics module.

like image 123
BoarGules Avatar answered Sep 17 '22 11:09

BoarGules


"from" module "import" * brings in all the names defined in __all__ if that exists and all names except for those starting with an underscore if __all__ doesn't exist.

You don't need to qualify the names imported (that is, prefix them with statistics). Just used them directly, median, mode, stdev, variance.

like image 41
Dimitris Fasarakis Hilliard Avatar answered Sep 19 '22 11:09

Dimitris Fasarakis Hilliard