3.9 Using library functions

Python contains a large library of standard functions which can be used for common programming tasks. You can also create your own (see Section 3.11, ``Making Functions''). A function is just some Python code which is seperated from the rest of the program. This has several advantages: Repeated sections of code can be re-used without rewriting them many times, making your program clearer. Furthermore, if a function is separated from the rest of the program it provides a conceptual separation for the person writing it, so they can concentrate on either the function, or the rest of the program.

Python has some built-in functions, for example type() and range() that we have already used. These are available to any Python program.

To use a function we call it. To do this you type its name, followed by the required parameters enclosed in parentheses. Parameters are sometimes called arguments, and are similar to arguments in mathematics. In maths, if we write $\sin(\pi/4)$, we are using the $\sin$ function with the argument $\pi/4$. Functions in computing often need more than one variable to calculate their result. These should be separated by commas, and the order you give them in is important. Refer to the discussion of the individual functions for details.

Even if a function takes no parameters (you will see examples of such functions later), the parentheses must be included.

However, the functions in the library are contained in separate modules, similar to the ones you have been writing and saving in the editor so far. In order to use a particular module, you must explicitly import it. This gives you access to the functions it contains.

The most useful module for us is the math library3.11. If you want to use the functions it contains, put the line from math import * at the top of your program.

The math functions are then accesible in the same way as the built in functions. For example, to calculate the $\sin$ and $\cos$ of $\pi/3$ we would write a module like this:

    from math import *   
    mynumber = pi / 3
    print sin(mynumber)
    print cos(mynumber)

The math module contains many functions, the most useful of which are listed below. Remember that to use them you must from math import *.

Function  Description 
sqrt($x$) Returns the square root of $x$
exp($x$) Return $e^{x}$
log($x$) Returns the natural log, i.e. $\ln x$
log10($x$) Returns the log to the base 10 of $x$
sin($x$) Returns the sine of $x$
cos($x$) Return the cosine of $x$
tan($x$) Returns the tangent of $x$
asin($x$) Return the arc sine of $x$
acos($x$) Return the arc cosine of $x$
atan($x$) Return the arc tangent of $x$
fabs($x$) Return the absolute value, i.e. the modulus, of $x$
floor($x$) Rounds a float down to its integer

The math library also contains two constants: pi, $\pi$, and e, $e$. These do not require parentheses (see the above example).

Note the floor function always rounds down which can produced unexpected results! For example

    >>> floor(-3.01)

Use the math library to write a program to print out the $\sin$ and $\cos$ of numbers from 0 to $2\pi$ in intervals of $\pi/6$. You will need to use the range() function.


... library3.11
Sorry about the spelling of ``math''. Modern computer languages are generally written according to US English spelling conventions. You do the math!