Re: Python Math libraries - How to?
En Thu, 08 May 2008 13:17:51 -0300, delta2 [EMAIL PROTECTED] escribió: I am also using Zelle's book to teach myself programming and Python. I also had a problem with import math , but the alternative of from math import * is working for me. I don't know why one works and the other doesn't. Note that they do different things. When you execute `import math`, the math module is loaded, but the ONLY new name you get is `math`. If you want the sqrt function, you have to use math.sqrt(2) When you execute `from math import *`, the math module is loaded the same as above, but ALL the public names defined in that module are now available in the current namespace. So the sqrt function is now available simply as sqrt Note that the form `from xxx import *` is intended mainly for usage in the interactive interpreter, for testing and playing only. If you use that form from a few modules, the namespace becomes too cluttered, then you don't know from where some name is coming, and a later import may inadvertidely replace previous names. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
I am also using Zelle's book to teach myself programming and Python. I also had a problem with import math , but the alternative of from math import * is working for me. I don't know why one works and the other doesn't. Cousin Stanley-3 wrote: Here is the arithmetic program I made that it worked before I added the import math line. #volumen.py # A program to compute the volume and surface area of a sphere import math NameError: global name 'pi' is not defined from math import * def surface( r ) : ... return 4 * pi * r ** 2 ... def volume( r ) : ... return ( 4. / 3. ) * pi * r ** 3 ... for n in range( 1 , 11 ) : ... s = surface( n ) ... v = volume( n ) ... print '%2d %9.4f %9.4f ' % ( n , s , v ) ... -- Stanley C. Kitching Human Being Phoenix, Arizona -- http://mail.python.org/mailman/listinfo/python-list -- View this message in context: http://www.nabble.com/Python-Math-libraries---How-to--tp16952032p17127534.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Python Math libraries - How to?
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle An Introduction to Computer Science where he uses a import math statement to calculate a square root. I tried the pi library function but it didn´t work. I tried using def Pi() it did not work either. I am yet to find a tutorial that explains how to declare (or initialize) and pass numbers to the functions such as cos(x) and the pi which does not have a variable in it. Is just a constant. Here is the arithmetic program I made that it worked before I added the import math line. I erased the constant p = 3.1416 and added the i for the library function pi in the algorithms. But I get an error message not recognizing pi #volumen.py # A program to compute the volume and surface area of a sphere import math def main(): print This program calculates the volume and surface area of a sphere print r = input(Please enter the radious: ) print r3 = r*r*r volume = 4/3*pi*r3 r2 = r*r surface = 4*pi*r2 print The Volume is, volume, Cubic centimeters print print The Surface area is, surface, square centimeters main() *** Error message * Traceback (most recent call last): File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 20, in module main() File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 13, in main volume = 4/3*pi*r3 NameError: global name 'pi' is not defined -- http://mail.python.org/mailman/listinfo/python-list
Python Math libraries - How to?
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle An Introduction to Computer Science where he uses a import math statement to calculate a square root. I tried the pi library function but it didn´t work. I tried using def Pi() it did not work either. I am yet to find a tutorial that explains how to declare (or initialize) and pass numbers to the functions such as cos(x) and the pi which does not have a variable in it. Is just a constant. Here is the arithmetic program I made that it worked before I added the import math line. I erased the constant p = 3.1416 and added the i for the library function pi in the algorithms. But I get an error message not recognizing pi #volumen.py # A program to compute the volume and surface area of a sphere import math def main(): print This program calculates the volume and surface area of a sphere print r = input(Please enter the radious: ) print r3 = r*r*r volume = 4/3*pi*r3 r2 = r*r surface = 4*pi*r2 print The Volume is, volume, Cubic centimeters print print The Surface area is, surface, square centimeters main() *** Error message * Traceback (most recent call last): File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 20, in module main() File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 13, in main volume = 4/3*pi*r3 NameError: global name 'pi' is not defined -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
[EMAIL PROTECTED] wrote: Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle An Introduction to Computer Science where he uses a import math statement to calculate a square root. I tried the pi library function but it didn´t work. I tried using def Pi() it did not work either. I am yet to find a tutorial that explains how to declare (or initialize) and pass numbers to the functions such as cos(x) and the pi which does not have a variable in it. Is just a constant. Here is the arithmetic program I made that it worked before I added the import math line. I erased the constant p = 3.1416 and added the i for the library function pi in the algorithms. But I get an error message not recognizing pi You have several ways to import a module, and your choice determines how you access things. Method 1: import math Then use: math.pi, math.sqrt, math.sin, math.cos, ... Method 2: from math import pi, sqrt Then use pi, and sqrt. But other module attributes (like sin, and cos) are not accessible. Method 3: from math import * Then use pi, sqrt, cos, sin, and anything else defined by the module. (This is sometime frowned upon, but there's no reason to do so here.) Gary Herron #volumen.py # A program to compute the volume and surface area of a sphere import math def main(): print This program calculates the volume and surface area of a sphere print r = input(Please enter the radious: ) print r3 = r*r*r volume = 4/3*pi*r3 r2 = r*r surface = 4*pi*r2 print The Volume is, volume, Cubic centimeters print print The Surface area is, surface, square centimeters main() *** Error message * Traceback (most recent call last): File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 20, in module main() File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 13, in main volume = 4/3*pi*r3 NameError: global name 'pi' is not defined -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
[EMAIL PROTECTED] wrote: Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle An Introduction to Computer Science where he uses a import math statement to calculate a square root. I tried the pi library function but it didn´t work. I tried using def Pi() it did not work either. I am yet to find a tutorial that explains how to declare (or initialize) and pass numbers to the functions such as cos(x) and the pi which does not have a variable in it. Is just a constant. Here is the arithmetic program I made that it worked before I added the import math line. I erased the constant p = 3.1416 and added the i for the library function pi in the algorithms. But I get an error message not recognizing pi #volumen.py # A program to compute the volume and surface area of a sphere import math Change that line, import math, to: from math import * and it'll work. I'm learning too, so some proficient person might be able to explain the difference to both of us :) John def main(): print This program calculates the volume and surface area of a sphere print r = input(Please enter the radious: ) print r3 = r*r*r volume = 4/3*pi*r3 r2 = r*r surface = 4*pi*r2 print The Volume is, volume, Cubic centimeters print print The Surface area is, surface, square centimeters main() *** Error message * Traceback (most recent call last): File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 20, in module main() File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 13, in main volume = 4/3*pi*r3 NameError: global name 'pi' is not defined -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
On Mon, Apr 28, 2008 at 10:07 PM, [EMAIL PROTECTED] wrote: Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle An Introduction to Computer Science where he uses a import math statement to calculate a square root. I tried the pi library function but it didn´t work. I tried using def Pi() it did not work either. I am yet to find a tutorial that explains how to declare (or initialize) and pass numbers to the functions such as cos(x) and the pi which does not have a variable in it. Is just a constant. Here is the arithmetic program I made that it worked before I added the import math line. I erased the constant p = 3.1416 and added the i for the library function pi in the algorithms. But I get an error message not recognizing pi #volumen.py # A program to compute the volume and surface area of a sphere import math def main(): print This program calculates the volume and surface area of a sphere print r = input(Please enter the radious: ) print r3 = r*r*r volume = 4/3*pi*r3 r2 = r*r surface = 4*pi*r2 print The Volume is, volume, Cubic centimeters print print The Surface area is, surface, square centimeters main() *** Error message * Traceback (most recent call last): File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 20, in module main() File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 13, in main volume = 4/3*pi*r3 NameError: global name 'pi' is not defined -- http://mail.python.org/mailman/listinfo/python-list pi is not a global name. When you do import math,you aren't adding everything to the name space, you are just telling python that you are going to be using that file. You then refer to it as math.*, such as math.pi, or math.pow(r,3). To use it the way you want to, you would have to do from math import pi instead of import math -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
Hi, thak you. I get the following: *** Error message * Traceback (most recent call last): File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 20, in module main() File C:/Python25/z - MIS PROGRAMAS/volumen-b.py, line 13, in main volume = 4/3*pi*r3 NameError: global name 'pi' is not defined -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
Thank you. I´ll try all methods to figure out the convenience of each Adolfo -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
Thank you :-), I´ll do Adolfo pi is not a global name. When you do import math,you aren't adding everything to the name space, you are just telling python that you are going to be using that file. You then refer to it as math.*, such as math.pi, or math.pow(r,3). To use it the way you want to, you would have to do from math import pi instead of import math -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
Gary Herron [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] [EMAIL PROTECTED] wrote: You have several ways to import a module, and your choice determines how you access things. Method 1: import math Then use: math.pi, math.sqrt, math.sin, math.cos, ... Method 2: from math import pi, sqrt Then use pi, and sqrt. But other module attributes (like sin, and cos) are not accessible. Method 3: from math import * Then use pi, sqrt, cos, sin, and anything else defined by the module. (This is sometime frowned upon, but there's no reason to do so here.) | = | There are two good reasons for the frown. One is for the potential conflict between builtin names, imported names (from possibly multiple modules), and names defined in the module. Builtins and math both have 'pow' functions that I believe are slightly different (or maybe once were). Math and cmath have 13 functions with duplicate names but different input and output (2.5). Importing * from both would be disasterous. The other is that someone not familiar with the imported module(s) will not know where a particular name came from when reading the code. Both problems are obvious worse with multiple imports. Method 4: (my favorite when using multiple names from a module) import math as m Then use m.pi, etc. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Math libraries - How to?
Here is the arithmetic program I made that it worked before I added the import math line. #volumen.py # A program to compute the volume and surface area of a sphere import math NameError: global name 'pi' is not defined from math import * def surface( r ) : ... return 4 * pi * r ** 2 ... def volume( r ) : ... return ( 4. / 3. ) * pi * r ** 3 ... for n in range( 1 , 11 ) : ... s = surface( n ) ... v = volume( n ) ... print '%2d %9.4f %9.4f ' % ( n , s , v ) ... -- Stanley C. Kitching Human Being Phoenix, Arizona -- http://mail.python.org/mailman/listinfo/python-list