Math Functions

Python has built-in math module that provides most of the familiar mathematical functions. Before using a module in Python you have to import it by typing:
>>> import NAME OF THE PYTHON MODULE
So for math module we have to type:
>>>import math 
The previous statement will create a module object named math. If you apply print function on math module you’ll get the additional information about module such as name of the module, address were the module is stored on your local hard drive.
The module contains functions and variables that are defined inside the module. To access module functions you have to specify the name of the module and the name of the function separated by a dot. The format is called dot notation.
>>> import math 
>>>math.sin(math.pi/2)
1.0
>>>x=10
>>>y = math.log10(x)
>>>print y
1.0
The first example finds the sine of radians. The argument inside the math.sin function is in radians (pi/2 radians = 90°). This is an indication that trigonometric functions such as sin, cos, tan etc take arguments in radians. The expression math.pi gets the variable pi from the math module. To convert degrees into radians do the following:
>>> degrees = 60
>>> radians=degrees/180.0 * math.pi
>>> radians
1.0471975511965976
>>> math.sin(radians)
0.8660254037844386

Nema komentara:

Objavi komentar