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?
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.
"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With