[Tutor] {Possible Spam?} How to use the import math function

Jimmy ghost04 at mwr.is
Wed Jul 20 21:53:59 CEST 2005

My assignment is this:

4. Read chapter 3 from "How to Think Like a Computer Scientist" (see the
background materials.) Implement all the examples and exercises in
Python-Eclipse and after you have successfully run them, export them to be
combined in a zip with with the other assignments above.


The course material I have read is this:

3.4 Math functions

In mathematics, you have probably seen functions like sin and log, and you
have learned to evaluate expressions like sin(pi/2) and log(1/x). First, you
evaluate the expression in parentheses (the argument). For example, pi/2 is
approximately 1.571, and 1/x is 0.1 (if x happens to be 10.0). 

Then, you evaluate the function itself, either by looking it up in a table
or by performing various computations. The sin of 1.571 is 1, and the log of
0.1 is -1 (assuming that logindicates the logarithm base 10). 

This process can be applied repeatedly to evaluate more complicated
expressions like log(1/sin(pi/2)). First, you evaluate the argument of the
innermost function, then evaluate the function, and so on. 

Python has a math module that provides most of the familiar mathematical
functions. A module is a file that contains a collection of related
functions grouped together. 

Before we can use the functions from a module, we have to import them: 

>>> import math 

To call one of the functions, we have to specify the name of the module and
the name of the function, separated by a dot, also known as a period. This
format is called dot notation. 

>>> decibel = math.log10 (17.0) 
>>> angle = 1.5 
>>> height = math.sin(angle) 

The first statement sets decibel to the logarithm of 17, base 10. There is
also a function called log that takes logarithm base e. 

The third statement finds the sine of the value of the variable angle. sin
and the other trigonometric functions (cos, tan, etc.) take arguments in
radians. To convert from degrees to radians, divide by 360 and multiply by
2*pi. For example, to find the sine of 45 degrees, first calculate the angle
in radians and then take the sine: 

>>> degrees = 45 
>>> angle = degrees * 2 * math.pi / 360.0 
>>> math.sin(angle) 

The constant pi is also part of the math module. If you know your geometry,
you can verify the result by comparing it to the square root of two divided
by two: 

>>> math.sqrt(2) / 2.0 

But I have no clue on how to make it work.  I tried many different
variations to get it to work but in the end nothing happened.  Any help you
guys can provide would be awesome, I am such a noob to programming it ain't
even funny.  Thanks....




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050720/2650f9aa/attachment.htm

More information about the Tutor mailing list