[Tutor] mathematical and statistical functions in Python

Danny Yoo dyoo at hashcollision.org
Sun Sep 27 20:48:12 CEST 2015


On Fri, Sep 25, 2015 at 3:58 AM, Michel Guirguis
<guirguismichel at yahoo.co.uk> wrote:
> Good afternoon,
>
> Thanks for your e-mail. I did not receive e-mail from Mr Otten.Basically, the problem that I am facing is that python 3.4 does not recognise the mathematical and statistical functions.


Many mathematical functions are in the auxiliary "math" library:

    https://docs.python.org/3.3/library/math.html


At the head of your program, add

######
import math
######

to tell Python to include support for the mathematical library.  Then,
later on in your program, you can use the square-root function by
referring to "math.sqrt".

######
print(math.sqrt(25))
######


See the documentation link above for the other common mathematical
functions that you can access through "math".



> For example, I am trying to find the square root, sqrt(25) but the program does not recognise the definition of sqrt(). The same problem i am facing with the mean, the variance and the standard deviation.

Other functions, such as variance and standard deviation, are more
specialized, and exist in other libraries.  In this case, I think you
want to look at the "statistics" library:

    https://docs.python.org/3/library/statistics.html

Like the 'math' library, you will need to import the library at the
head of your program before you can use it.


Also note that the reference will often omit the use of the library
name in its examples just to be brief.  So in the example presented
in:

    https://docs.python.org/3/library/statistics.html#statistics.stdev

you may need to change this:

    stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])

to this:

    statistics.stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])



See:

    https://docs.python.org/3.5/tutorial/stdlib.html#mathematics

for a few more examples.


If you have more questions, please feel free to ask the mailing list.
Good luck.


More information about the Tutor mailing list