473,805 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13551
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*****@island training.comwro te in message
news:48******** ******@islandtr aining.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
7311
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
2334
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 python2.3. Can someone point me to python2.4 version of scipy and help me install.
53
4396
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 would welcome comments and corrections, and would be happy to contribute some version of this to the Python website if it is of interest. ===
32
2883
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
8103
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
2539
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 error LNK1141: failure during build of exports file error: command '"C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\link.exe"' failed with exit status 1141.What shoud I
4
364
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 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...
0
788
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 excellent libraries fro anything you want, natural language processing, any kind of parsing, math, simulation, neural networks, genetic algorithms, data visualization etc. And everythign just works, download and go!
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
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 we have to send another system
2
3845
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.