CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Importing modules

The math module

We've seen several useful functions that are built into Python.

abs int min round str
float len print sorted sum
input max range

There are many other useful functions for the language, but they are so numerous that Python finds it useful to divide them into modules, which is simply a collection of related identifiers.

The math module is a good example. It includes several mathematical functions, such as logarithms and sines. In a program that uses it, you must first have an import line:

import math

From there, you can use the built-in functions, all of which would be identified by preceding the function name with “math.”: For example, one could compute the natural logarithm of 10 with “math.log(10)”.

A sampling of the functions found in the math module:

math.atan(x) Returns the arctangent of x, in radians
math.cos(x) Returns the cosine of x radians
math.degrees(x) Returns the number of degrees equivalent to x radians
math.exp(x) Returns ex, where e is Euler's constant 2.718…
math.factorial(n) Returns the product of the integers 1 through n.
math.log(x) Returns natural logarithm (i.e., base e) of x
math.log10(x) Returns base-10 logarithm of x
math.radians(x) Returns the number of radians equivalent to x degrees
math.sin(x) Returns the sine of x radians

Additionally, the math module includes two useful constants.

math.e Euler's constant e = 2.71828…
math.pi The mathematical constant π = 3.14159…

As an example of a program using these, here is a program that computes the area of a regular n-gon inscribed into a circle with radius 1, where n is an integer typed by the user. At right is diagrammed such a nonagon (n = 9)

import math
num_sides = int(input())
angle = 2 * math.pi / num_sides
area = (num_sides / 2) * math.sin(angle)

Other modules

Over 200 modules are packaged with Python, to say nothing of additional “third-party libraries” that others have developed. There is no way we could cover them all. Two others are worth mentioning here.

The random module provides functions related to generating pseudorandom numbers.

random.choice(values) Returns a element chosen randomly from the list values
random.random() Returns a pseudorandom floating-point number between 0.0 and 1.0
random.randrange(ab) Returns a pseudorandom integer between a and b (not including b)
random.shuffle(values) Rearranges the list values randomly

The re module provides functions related to regular expressions.

re.findall(needlehaystack) Finds nonoverlapping occurrences of sequences from sentence matching the regular expression separator, returning the found sequences.
re.split(separatorsentence) Finds nonoverlapping occurrences of sequences from sentence matching the regular expression separator, returning the intervening sequences.

For example, “re.findall('[aeiou]+''turquoise')” would return the list [u, uoi, e], while “re.split('[aeiou]+''beautiful')” would return the list [t, rq, s, ''] (the last being the empty string).