Lesson 14 of 19
Modules
Reuse code from files and Python's standard library.
Introduction
A module is a Python file that can be imported. Python includes useful modules such as math, random, datetime and pathlib.
Imports keep programs smaller by letting each file focus on one responsibility.
Key insight
Reuse code from files and Python's standard library.
Example
from math import sqrt
print(sqrt(81))This example shows the core modules idea in a small, runnable program.
Real-world example
Applications import modules for dates, file paths, HTTP requests and testing.
Test yourself
2-question check
1
What is the main idea in Modules?
2
Where can modules be useful?
Ready to check your understanding?
Answer every question, then submit your quiz.
Try it yourself
Python compiler
Runs in your browser. Nothing is sent to a server.
Output appears here
First run downloads the Python runtime, so it may take a moment.
Practice challenge
Import the random module and generate a number.