[Tutor] mathematical and statistical functions in Python

Alan Gauld alan.gauld at btinternet.com
Sun Sep 27 20:50:48 CEST 2015


On 25/09/15 11:58, Michel Guirguis wrote:
> the problem that I am facing is that python 3.4 does not recognise the
 > mathematical and statistical functions.

That's because they are in the math and statistics modules which you 
have to import:

Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> import math
 >>> math.sqrt(25)
5.0
 >>> import statistics
 >>> dir(statistics)
['Decimal', 'Fraction', 'StatisticsError', '__all__', '__builtins__', 
'__cached__', '__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__spec__', '_check_type', '_counts', 
'_decimal_to_ratio', '_exact_ratio', '_ss', '_sum', 'collections', 
'math', 'mean', 'median', 'median_grouped', 'median_high', 'median_low', 
'mode', 'pstdev', 'pvariance', 'stdev', 'variance']
 >>> statistics.stdev([1,2,3,4,5,6,7,6,5,4,3,2,1])
1.9644272343292228
 >>>

Python comes with literally hundreds of modules containing
functions and classes which are not built in to the language.
You need to import the module and then preced4e the
function you require with the module name.

See the module index page in the docs:

https://docs.python.org/3/py-modindex.html

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list