Write a module that consists only of function definitions.
Import the module and use the functions in a program.
Defining a module
Modules are defined by putting code in a .py file. The area module below is in a file named area.py. This module provides functions for calculating area.
Importing a module
The module defined in area.py can be used in other programs. When importing the area module, the suffix.py is removed:
import area
print("Area of a basketball court:", area.rectangle(94, 50))
print("Area of a circus ring:", area.circle(21))
The output is:
Area of a basketball court: 4700
Area of a circus ring: 1385.4423602330987
Adapted from Introduction to Python Programming by OpenStax (openstax.org), licensed under CC BY-NC-SA 4.0. Changes were made. License: CC-BY-NC-SA-4.0.
These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.