473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jun 27 '08 #1
5 13487
ag************@gmail.com 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
Jun 27 '08 #2
On Mon, Apr 28, 2008 at 10:07 PM, <ag************@gmail.comwrote:
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"
Jun 27 '08 #3
>
Thank you. I´ll try all methods to figure out the convenience of each
Adolfo
>
Jun 27 '08 #4
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"
Jun 27 '08 #5

"Gary Herron" <gh*****@islandtraining.comwrote in message
news:48**************@islandtraining.com...
ag************@gmail.com 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


Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mudd | last post by:
How do I build Python so that I get static libraries instead of dynamic libraries (e.g. build/lib.solaris-2.8-sun4u-2.3/math.so)? John
5
by: querypk | last post by:
Did anyone try to install Scipy package on python2.4 linux version. I see only python2.3 version of scipy released. When I try to install I get an dependency warning saying scipy cannot find...
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
32
by: siggi | last post by:
@Ben Sizer Hi Ben, in January I received your message re Pygame and Python 2.5: As a Python (and programming ) newbie allow me a - certainly naive - question:
17
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
4
by: vedrandekovic | last post by:
Hi, I have already install Microsoft visual studio .NET 2003 and MinGw, when I try to build a extension: python my_extension_setup.py build ( or install ) , I get an error: LINK : fatal...
4
by: aguirre.adolfo | last post by:
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...
0
by: cnb | last post by:
I have been testing different tools for cvs, math etc and I am constantly amazed how many great tools and libraries there are for Python. SAGE, Mercurial as 2 outstanding tools and there a re...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.