OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
Python videos

Modules

This tutorial contains two interactive mini exercises and two review exercises. Try to solve them all!

The Python standard library

So far, we've only used functions that are built into Python, and that you can use straight away when you start a Python interpreter. However, you can import modules to get additional functionality. Many common modules are included in Python by default so that you don't need to install them—but you do have to import them! These modules make up the Python standard library:

import: importing modules

Let's consider the random module:

The random module contains many functions for randomization. For example, random.choice() takes an iterable, such as a list, and returns a single, randomly selected element. To use random.choice(), you need to import the random module.

# Preferred
import random
prime_numbers = [1, 3, 5, 7, 11]
print(random.choice(prime_numbers))

Output:

7

In the example above, you import the entire random module and then selectively call random.choice(). However, the import statement is flexible and allows you to import modules and functions in many different ways. These all have their place, but as a general rule, the import method shown above is clearest and therefore preferred.

Mini exercise

Import the string module, and print out all ASCII letters as defined in this module.

You can also selectively import the choice() function. The downside of this way of importing is that you cannot easily tell anymore that choice() is part of the random module.

# Not preferred
from random import choice
print(choice(prime_numbers))

Output:

1

Mini exercise

Selectively import the sample() function from the random module, and the ascii_letters attribute of the string module. Use these to randomly sample five letters and assign these to a variable called five_letters.

You can also directly import everything from the random module: a wildcard import. This is generally considered bad practice, because it makes it difficult to tell where a function comes from.

# Not preferred
from random import *
print(choice(prime_numbers))

Output:

3

Finally, the import statement allows you to rename the imported modules or functions. Again, this is generally not preferred because of potential confusion. Exceptions are libraries such as numpy for which an import-and-rename (import numpy as np) is so common that it is unlikely to result in confusion.

# Not preferred
import random as rnd
print(rnd.choice(prime_numbers))

Output:

3

Review exercises

Some simple statistics

Use the statistics module to calculate the mean of all prime numbers below 10.

This exercise is not checked automatically, because there are several possible solutions. Click here to see one solution!

Files and folders

Use the os and os.path modules to list the contents of your Downloads folder, and indicate for each item whether it's a file or a folder.

This exercise is not checked automatically, because there are several possible solutions. Click here to see one solution!

You're done with this section!

Continue with Exceptions >>