473,508 Members | 4,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22033
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4319
by: Thomas Brathans | last post by:
Hi, when I try to execute the obove mentioned to synchronise the servertime by a php-script, it doesn't work. Other shell-commands work fine. Executing ntpdate <server> directly in the shell...
4
2115
by: edg | last post by:
This works: $b = $a->method1(); $c = $b->method2(); I'd like to get this all on one expression, something like: $c = ${$a->method1()}->method2(); which doesn't work.
3
84567
by: Paul Thompson | last post by:
When I put a <div ...> inside a <table> specification, functionality is not there. When I put the <table> inside the <div> everything works. Why is that?
2
2487
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
1
3287
by: Alex VanderWoude | last post by:
I am trying to <include> some text into an XML documentation topic, but that text is stored in a file that is in a different directory than the "current" XML file. Using a relative path does not...
2
1456
by: Maureen | last post by:
Hi: Our ASP.NET webapplication works on our development webserver, but fails on our production webserver - I cannot get any detailed error information: -It is an application -Mode in...
1
12759
by: Alex Chun | last post by:
I have an Access database -- a .mdb file -- on a server on our network. When the file is in use -- i.e. there is an associated .ldb file -- I cannot open the database by double-clicking its icon;...
6
17345
by: Stef Mientki | last post by:
hello, I want to find all files with the extension "*.txt". From the examples in "Learning Python, Lutz and Asher" and from the website I see examples where you also may specify a wildcard...
1
3468
by: DukeRev | last post by:
I made a box where if you hover over the DIV tag it changes the color of the link text. It works in firefox and opera, but it doesn't work in IE. Doesn't anyone know why it doesn't work and how to...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.