5.1. Modules

A module is a file containing Python definitions and statements intended for use in other Python programs. There are many Python modules that come with Python as part of the standard library. We have already used one of these quite extensively, the turtle module. Recall that once we import the module, we can use things that are defined inside.

Before we move on to exploring other modules, we should say a bit more about what modules are and how we typically use them. One of the most important things to realize about modules is the fact that they are data objects, just like any other data in Python. Module objects simply contain other Python elements.

The first thing we need to do when we wish to use a module is perform an import. In the example above, the statement import turtle creates a new name, turtle, and makes it refer to a module object.

In order to use something contained in a module, we use the dot notation, providing the module name and the specific item joined together with a “dot”. For example, to use the forward function, we say turtle.forward. You should read this as: “In the module turtle, access the Python element called forward”.

We will now turn our attention to a few other modules that you might find useful.

You have attempted of activities on this page
5. Python Modules"> 5.2. The math module">Next Section - 5.2. The math module