Python 3 Programming Tutorial – Statistics (Mean, Standard Deviation)

In this Python 3 programming tutorial, we cover the statistics module. This module is a built-in module that comes with Python’s installation, and it lets you do some basic statistic calculations. These include: Mean (average), median, mode, standard deviation, and variance.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:…

Related Posts


  1. I'm using python version 3.3.3. When I typed the code in IDLE as you did to import statistics, I get an error module saying that there's no such module named "statistics". Should I create a module like that?

  2. statistics not working in 3.7 in my system. The error is showing as AttributeError: module 'statistics' has no attribute mean. I dont know why this error is showing. I will be glad if you help me out. Anyway thanks for the awsome tutorial.

  3. Hi My teacher what the wrong in this simple code:
    from statistics import *
    the PC send the following meesage 'ERROR running 'file name' java.ioEOFException'

  4. help me in sorting it out

    a = [int(x) for x in input().split()]
    x = statistics.mean(a)

    ValueError Traceback (most recent call last)
    <ipython-input-15-a764674e360f> in <module>()
    —-> 1 a = [int(x) for x in input().split()]
    2 x = statistics.mean(a)
    3 print(x)

    <ipython-input-15-a764674e360f> in <listcomp>(.0)
    —-> 1 a = [int(x) for x in input().split()]
    2 x = statistics.mean(a)
    3 print(x)

    ValueError: invalid literal for int() with base 10: '5,6,56,556'

  5. import statistics
    example_list ={2,3,4,5,6,5,445,554,54,5,55}
    x = statistcs.mean(example_list)

    popped out error is:

    === RESTART: C:/Users/KANNAN/AppData/Local/Programs/Python/Python36/ ===
    Traceback (most recent call last):
    File "C:/Users/KANNAN/AppData/Local/Programs/Python/Python36/", line 3, in <module>
    x = statistcs.mean(example_list)
    NameError: name 'statistcs' is not defined

    ANY one there please help me i am really finding it difficult

  6. well my long term goal, one of them is to innovate and pioneer AI. newer to python and not deep in math, though i have a great gift for math, lets see if i ever make it and how long it takes.

  7. There is an error in at the line 590: it should be divided by n and not by n-1. I checked it sice the results of variance in statictics module and in numpy one differ.

  8. can you please tell me how to take multiple inputs from user and find a mean of it.I have tried but I am unable to get the mean of it.import statistics
    #x=[2,4,6,8,9,2,4,5,6,7]—–mean for list
    print('the mean is ',y)
    I am able to split but unable get the mean.

  9. import statistics

    example_list = [4,3,65,3,2,5,6,5,3,24,56,9,5]
    x = statistics.mean(example_list)
    print ('mean is ',x)

    Traceback (most recent call last):
    File "C:/Users/Ehtesham/Desktop/Python Codes/", line 1, in <module>
    import statistics
    File "C:/Users/Ehtesham/Desktop/Python", line 4, in <module>
    x = statistics.mean(example_list)
    AttributeError: module 'statistics' has no attribute 'mean'

  10. Quick help for everyone. statistics module is preinstalled with Python 3, in case you dont have it go ahead and "pip install statistics" in the command line. Also, VERY IMPORTANT, do not name your file statistics since the import will be confused and throw the errors of the library not existing and the mean function not existing. If you have done this already just go to your folder where you saved the file, and delete it. An other option is to Save As, with an other name but then dont forget to set the configurator (the python symbol up right) to have the new script you are using. Hope it helps and once again thank you sentdex for this great series!

  11. Hey Sentdex,

    I'm using Python version 3.5.1 and I couldn't get the code working.

    import statistics

    Error obtained:
    Traceback (most recent call last):
    File "", line 1, in <module>
    import statistics
    File "", line 2, in <module>
    AttributeError: module 'statistics' has no attribute 'mean'

    I referred the doc and it said introduced in 3.4, hence I assumed it should work for me.
    Can you please help?

  12. hello there sentdex,

    i have a file which has import values like

    import copy as cp

    import numpy as np
    from scipy import linalg

    from import _BaseRaw
    from ..epochs import _BaseEpochs
    from .. import Covariance, EvokedArray, Evoked, EpochsArray
    from import pick_types
    from .ica import _get_fast_dot
    from ..utils import logger
    from ..decoding.mixin import TransformerMixin
    from ..cov import _regularized_covariance
    from ..channels.channels import ContainsMixin

    What does these . and .. mean
    and when i try to run it in my pc, it gives an error which i think is from the lack of some libraries. is there an easy way to download all related libraries for specific purpose in a single file like anaconda. (This is a file for eeg reading)

  13. How would I code a multi-user survey program that creates a multiple choice test that would then compare the answers of two users and show how similar they are?

  14. def run_std(x):
        total = 0
        tot = 0
        for num in x:
            total += num
        median = total / float(len(x))
        print ("The median is %d" % (median))
        for num in x:
            dev = (num – median)**2
            tot += dev
        variance = tot / float(len(x))
        print ("The variance is %d" % (variance))
        std = variance**0.5
        print ("The standard deviation is %d" % (std))

    print (run_std(example_list))
    print (run_std(test_list))

    This is the code I made to perform the STD task. Can someone explain to me why my code rounds the number when inserted in the string. For example say the STD is 14.3, if I just tell the code to print std it will give me 14.3, but when the value is inserted into the string it rounds down printing out The standard deviation is 14. Is this something python does automatically and if yes is there a way to make it not perform the rounding action so it prints The standard deviation is 14.3

  15. Traceback (most recent call last):
      File "", line 1, in <module>
        import statistics
      File "", line 5, in <module>
        x = statistics.mean(list1)
    AttributeError: 'module' object has no attribute 'mean'

    plz help :3

  16. I had an error Traceback (most recent call last):
      File "D:/Sendex/", line 2, in <module>
        import statistics
    ImportError: No module named statistics
    I got around this by using Pycharm and settings – Project Interpreter – "+" then search for module statistics incase someone else having issues

  17. Traceback (most recent call last):
      File "C:/Python34/", line 1, in <module>
        import statistics
      File "C:/", line 5, in <module>
        x = statistics.stdev(example_list)
    AttributeError: 'module' object has no attribute 'stdev'

  18. Good, thanks.  However in the future you might want to include a short background on such math/stats topics to help people to understand just why this is interesting, e.g. relate to the grading bell curve, etc.

Leave a Reply

Your email address will not be published.

© 2022 Code As Pro - Theme by WPEnjoy · Powered by WordPress