Modules

One of the strengths of Python is that there are many built-in add-ons - or modules - which contain existing functions, classes, and variables which allow you to do complex tasks in only a few lines of code. In addition, there are many other third-party modules (e.g. Numpy, Scipy, Matplotlib) that can be installed, and you can also develop your own modules that include functionalities you commonly use.

The built-in modules are referred to as the Standard Library, and you can find a full list of the available functionality in the Python Documentation.

To use modules in your Python session or script, you need to import them. The following example shows how to import the built-in math module, which contains a number of useful mathematical functions:

In [1]:
import math

You can then access functions and other objects in the module with math.<function>, for example:

In [2]:
math.sin(2.3)
Out[2]:
0.7457052121767203
In [3]:
math.factorial(20)
Out[3]:
2432902008176640000
In [4]:
math.pi
Out[4]:
3.141592653589793

Because these modules exist, it means that if what you want to do is very common, it means it probably already exists, and you won't need to write it (making your code easier to read).

For example, the numpy module, which we will talk about next week, contains useful functions for finding e.g. the mean, median, and standard deviation of a sequence of numbers:

In [5]:
import numpy as np
In [6]:
li = [1,2,7,3,1,3]
np.mean(li)
Out[6]:
2.8333333333333335
In [7]:
np.median(li)
Out[7]:
2.5
In [8]:
np.std(li)
Out[8]:
2.0344259359556172

Notice that in the above case, we used:

import numpy as np

instead of:

import numpy

which shows that we can rename the module so that it's not as long to type in the program.

Finally, it's also possible to simply import the functions needed directly:

In [9]:
from math import sin, cos
sin(3.4)
cos(3.4)
Out[9]:
-0.9667981925794611

You may find examples on the internet that use e.g.

from module import *

but this is not recommended, because it will make it difficult to debug programs, since common debugging tools that rely on just looking at the programs will not know all the functions that are being imported.

Where to find modules and functions

How do you know which modules exist in the first place? The Python documentation contains a list of modules in the Standard Library, but you can also simply search the web. Once you have a module that you think should contain the right kind of function, you can either look at the documentation for that module, or you can use the tab-completion in IPython:

In [2]: math.<TAB>
math.acos       math.degrees    math.fsum       math.pi
math.acosh      math.e          math.gamma      math.pow
math.asin       math.erf        math.hypot      math.radians
math.asinh      math.erfc       math.isinf      math.sin
math.atan       math.exp        math.isnan      math.sinh
math.atan2      math.expm1      math.ldexp      math.sqrt
math.atanh      math.fabs       math.lgamma     math.tan
math.ceil       math.factorial  math.log        math.tanh
math.copysign   math.floor      math.log10      math.trunc
math.cos        math.fmod       math.log1p      
math.cosh       math.frexp      math.modf    

Exercise 1

Does the math.cos funtion take radians or degrees? Are there functions that can convert between radians and degrees? Use these to find the cosine of 60 degrees, and the sine of pi/6 radians.

In [10]:
# enter your solution here