Discussion:
[Tutor] mathematical and statistical functions in Python
Michel Guirguis
2015-09-25 10:58:23 UTC
Permalink
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.

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.

This is the main problem. Once I solve it, I can input the parameters of the mathematical financial model.

Thanks for your help,

Michel

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Martin A. Brown
2015-09-27 18:48:28 UTC
Permalink
Greetings Michel,
Post by Michel Guirguis
Thanks for your e-mail. I did not receive e-mail from Mr
Otten.
Nobody in their right mind wants to receive an email from Mr. Otten. ;)
Post by Michel Guirguis
Basically, the problem that I am facing is that python 3.4 does
not recognise the mathematical and statistical functions.
https://docs.python.org/3/library/math.html#module-math
Post by Michel Guirguis
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.
In your code, include:

import math
eleven = math.sqrt(121)
Post by Michel Guirguis
This is the main problem. Once I solve it, I can input the
parameters of the mathematical financial model.
You also said statistics. If you are looking for the basic tools,
then you can find them here:

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

Thggere are other, richer tools in third-party libraries if you are
looking for more advanced tools. But, perhaps you only need to
start with the above.

Good luck,

-Martin
--
Martin A. Brown
http://linux-ip.net/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-09-27 21:46:27 UTC
Permalink
Post by Martin A. Brown
Greetings Michel,
Post by Michel Guirguis
Thanks for your e-mail. I did not receive e-mail from Mr
Otten.
Nobody in their right mind wants to receive an email from Mr. Otten. ;)
Post by Michel Guirguis
Basically, the problem that I am facing is that python 3.4 does
not recognise the mathematical and statistical functions.
https://docs.python.org/3/library/math.html#module-math
Post by Michel Guirguis
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.
import math
eleven = math.sqrt(121)
Post by Michel Guirguis
This is the main problem. Once I solve it, I can input the
parameters of the mathematical financial model.
You also said statistics. If you are looking for the basic tools,
https://docs.python.org/3/library/statistics.html#module-statistics
Thggere are other, richer tools in third-party libraries if you are
looking for more advanced tools. But, perhaps you only need to
start with the above.
Good luck,
-Martin
The tutor list is sending mail out late.
Michel had 2 problems. The first is that he hadn't subscribed to
python list, which meant that he wasn't getting the replies that
weren't explicitly emailed to him.

The second problem was that he didn't know he needed to
import math and statistics.

I think things are working ok now, and he knows about the tutor list,
too. :) great, welcome.

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-27 23:19:39 UTC
Permalink
Post by Laura Creighton
The tutor list is sending mail out late.
Partly due to me being on vacation and only checking
the moderation Q twice last week.
Post by Laura Creighton
Michel had 2 problems. The first is that he hadn't subscribed to
python list, which meant that he wasn't getting the replies that
weren't explicitly emailed to him.
Plus any messages sent went to the Q.

Apologies, normal service should now be restored. :-)
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-09-27 18:48:12 UTC
Permalink
On Fri, Sep 25, 2015 at 3:58 AM, Michel Guirguis
Post by Michel Guirguis
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".
Post by Michel Guirguis
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.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-27 18:50:48 UTC
Permalink
Post by Michel Guirguis
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.
Post by Michel Guirguis
Post by Martin A. Brown
import math
math.sqrt(25)
5.0
Post by Michel Guirguis
Post by Martin A. Brown
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']
Post by Michel Guirguis
Post by Martin A. Brown
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...