By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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.