`math`

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

abs

int

min

round

str

float

len

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

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 `math`

line:**import**

**import** `math`

From there, you can use the built-in functions, all of which
would be identified by preceding the function name with
“

”: For example, one could compute the
natural logarithm of 10 with “`math`.

”.`math`.`log`(`10`)

A sampling of the functions found in the

module:`math`

math.atan(x)Returns the arctangent of x, in radians

math.cos(x)Returns the cosine of xradians

math.degrees(x)Returns the number of degrees equivalent to xradians

math.exp(x)Returns e^{x}, whereeis 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) ofx

math.log10(x)Returns base-10 logarithm of x

math.radians(x)Returns the number of radians equivalent to xdegrees

math.sin(x)Returns the sine of xradians

Additionally, the

module includes two useful
constants.`math`

math.eEuler's constant e= 2.71828…

math.piThe 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`)

`print`(`'{0:5.3f}'`.`format`(`area`))

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

module provides functions related to
generating pseudorandom numbers.`random`

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(a,b)Returns a pseudorandom integer between `and`

a`(not including`

b`)`

b

random.shuffle(values)Rearranges the list `randomly`

values

The

module provides functions related to regular
expressions.`re`

re.findall(needle,haystack)Finds nonoverlapping occurrences of sequences from `matching the regular expression`

sentence`, returning the found sequences.`

separator

re.split(separator,sentence)Finds nonoverlapping occurrences of sequences from `matching the regular expression`

sentence`, returning the intervening sequences.`

separator

For example, “

”
would return the list [`re`.`findall`(`'[aeiou]+'`, `'turquoise'`)*u*, *uoi*, *e*],
while “

”
would return the list [`re`.`split`(`'[aeiou]+'`, `'beautiful'`)*t*, *rq*, *s*, '']
(the last being the empty string).