473,394 Members | 1,843 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,394 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 13502
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.