Modules
Organize your code, reuse functionality, and explore Python's powerful built-in libraries.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A module is simply a Python file ( .py ) that contains code you can reuse. Instead of writing everything in one file, you split your code into organized pieces.
Python's built-in math module provides mathematical functions:
Generate random numbers for games, testing, or simulations:
Interact with your computer's operating system:
JSON (JavaScript Object Notation) is a universal format for storing and exchanging data. It's used by almost every API.
Any Python file can be a module. Just save your code and import it:
A package is a folder containing multiple modules. It must have an __init__.py file (can be empty).
pip is Python's package manager. It downloads packages from PyPI (Python Package Index) — a library of over 400,000 packages!
Use multiple modules to create a random password generator:
Calculate days between dates and future dates:
You now know how to organize your code into reusable modules, use Python's powerful standard library, and install external packages. This is how professional developers work!
Next up: Lesson 12 – Object-Oriented Programming — Learn to create classes and objects to model real-world concepts in your code.
🏅 Intermediate Track Complete! You now code like a real developer!
Functions, Lists, Dictionaries, File Handling, Exceptions, Modules — you've completed all 11 beginner and intermediate lessons. You now have the full Python toolkit that powers real applications.
🚀 Up next: Object-Oriented Programming — learn to model real-world things as classes and objects, the way all major software is built.
Practice quiz
What is a module in Python?
- A folder of files
- A function inside a class
- A single .py file containing reusable code
- An installed library only
Answer: A single .py file containing reusable code. A module is simply a Python (.py) file whose code you can import and reuse.
Which import lets you call sqrt(16) directly (without a prefix)?
- from math import sqrt
- import math
- import math as m
- import sqrt
Answer: from math import sqrt. from math import sqrt brings sqrt into your namespace so you call it without math..
After 'import math as m', how do you compute a square root?
- math.sqrt(16)
- sqrt(16)
- m(sqrt(16))
- m.sqrt(16)
Answer: m.sqrt(16). The alias m replaces the module name, so use m.sqrt(16).
What does math.ceil(4.2) return?
- 4
- 5
- 4.2
- 4.0
Answer: 5. math.ceil rounds UP to the next integer, so ceil(4.2) is 5.
What does math.floor(4.8) return?
- 4
- 5
- 4.8
- 5.0
Answer: 4. math.floor rounds DOWN, so floor(4.8) is 4.
Which random function picks one random item from a list?
- random.randint(list)
- random.shuffle(list)
- random.choice(list)
- random.pick(list)
Answer: random.choice(list). random.choice(list) returns a single randomly selected item.
What does random.shuffle(my_list) do?
- Returns a new shuffled list
- Shuffles the list in place and returns None
- Picks one item
- Sorts the list
Answer: Shuffles the list in place and returns None. random.shuffle reorders the list in place; it does not return a new list.
Why is 'from math import *' discouraged?
- It is slower
- It only imports pi
- It is a syntax error
- It can overwrite your own variables / cause naming conflicts
Answer: It can overwrite your own variables / cause naming conflicts. Star imports dump every name in, which can shadow your variables and cause conflicts.
What file makes a folder into a package?
- __main__.py
- __init__.py
- setup.py
- package.py
Answer: __init__.py. A folder needs an __init__.py file (it can be empty) to be treated as a package.
What is pip?
- A built-in math module
- A way to run scripts
- Python's package manager for installing packages from PyPI
- A type of loop
Answer: Python's package manager for installing packages from PyPI. pip installs external packages from PyPI, e.g. pip install requests.