Login
📚 Introduction to Python Programming
Chapters ▾
⇩ Download ▾

7.1 Module basics

Learning objectives

By the end of this section you should be able to

  • 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