468,765 Members | 1,094 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Learning Python : >>> import math doesn't work ?

Have carefully installed Python 2.5.1 under XP in dir E:\python25 .
ran set path = %path% ; E:\python25
Python interactive mode works fine for simple arithmetic .
Then tried >> import math
>> x = sqrt(100)
Get error Name error : name 'sqrt' is not defined
Same thing with sin(x) .
I'm unable to find "math" , "sqrt" , or "sin" anywhere in lib , Libs
or include directories .
The Tutorial does not clear this up .
Please help. Thanks Dave pd******@earthlink.net
Nov 19 '07 #1
4 21002
On Nov 18, 8:48 pm, pdlem...@earthlink.net wrote:
Have carefully installed Python 2.5.1 under XP in dir E:\python25 .
ran set path = %path% ; E:\python25
Python interactive mode works fine for simple arithmetic .
Then tried >> import math
>> x = sqrt(100)
Get error Name error : name 'sqrt' is not defined
Same thing with sin(x) .
I'm unable to find "math" , "sqrt" , or "sin" anywhere in lib , Libs
or include directories .
The Tutorial does not clear this up .
Please help. Thanks Dave pdlem...@earthlink.net
Hi,

I believe that should be:
x = math.sqrt(100)

'import math' makes available the functions in the math module, but
they are still inside the math namespace, so still require 'math.'
If you will be using sqrt a lot, you can create a copy of the function
in the local namespace using
sqrt = math.sqrt
x = sqrt(100)

[In principle, you can give it any name you want, but of course it is
sensible to use a name that makes sense.
E.g. if you were a Spanish speaker, you could say instead
raiz = math.sqrt
x = raiz(100)
]

For more details about how 'import'ing works, check out the Python
tutorial.

Hope that helps.
Best wishes,

David.


Nov 19 '07 #2
On Nov 19, 10:48 am, pdlem...@earthlink.net wrote:
Have carefully installed Python 2.5.1 under XP in dir E:\python25 .
ran set path = %path% ; E:\python25
Python interactive mode works fine for simple arithmetic .
Then tried >> import math
>> x = sqrt(100)
Get error Name error : name 'sqrt' is not defined
Same thing with sin(x) .
I'm unable to find "math" , "sqrt" , or "sin" anywhere in lib , Libs
or include directories .
The Tutorial does not clear this up .
Please help. Thanks Dave pdlem...@earthlink.net
use it like: x = math.sqrt (100) and math.sin(x)
Nov 19 '07 #3
On Nov 19, 3:46 pm, windspy <wind...@gmail.comwrote:
use it like: x = math.sqrt (100) and math.sin(x)
alternatively import like this:

from math import sqrt, sin

.... and use it like you have.

Nov 19 '07 #4

"Asun Friere" <af*****@yahoo.co.ukwrote in message
news:86**********************************@s36g2000 prg.googlegroups.com...
| On Nov 19, 3:46 pm, windspy <wind...@gmail.comwrote:
| use it like: x = math.sqrt (100) and math.sin(x)
|
| alternatively import like this:
|
| from math import sqrt, sin
|
| ... and use it like you have.

Or,
import math as m # to save typing, especially in interactive mode
x = m.sin(1) #etc

Nov 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Thomas Brathans | last post: by
4 posts views Thread by edg | last post: by
3 posts views Thread by Paul Thompson | last post: by
1 post views Thread by Alex VanderWoude | last post: by
2 posts views Thread by Maureen | last post: by
6 posts views Thread by Stef Mientki | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.