Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 07 / Topic 1 of 9

What is a module?

A module is just a .py file. Other files can use its functions and variables. Think of a module as a toolbox: you open the box (import it) and use the tools inside. Python ships with hundreds of ready modules called the standard library (no install needed). You can also write your own modules, or install other people's with pip.

Real-world example: use the built-in math module to count tables for a party.

import math

guests = 23
print(math.ceil(guests / 4))   # 6   (round UP: 23 people need 6 tables of 4)
print(math.sqrt(144))          # 12.0
print(math.pi)                 # 3.141592653589793
Kind of module Example Where it comes from
Built-in / standard library math, random, datetime Comes with Python
Your own shop_utils.py A file you wrote
Third-party requests, pandas Installed with pip

๐Ÿ” Going deeper: a module's code runs only once, on the first import. Python stores the loaded module in a dictionary called sys.modules; every later import of the same name just reuses it. Put imports at the top of the file, in three groups: standard library, third-party, your own (PEP 8).

Read more: the official tutorial's modules chapter.

Done here? The practice set for Part 7 โ€” 7 exercises with solutions โ€” is on the part page.

Search the guide