Modules
Organize your code, reuse functionality, and explore Python's powerful built-in libraries.
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.
What You'll Learn in This Lesson
What Are Modules?
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.
Term
What It Is
Example
Module
A single .py file
math.py, helpers.py
Package
A folder of modules
my_app/ folder
Library
Collection of packages
requests, numpy
Why Use Modules?
Benefit
Description
Reusability
Write once, use everywhere
Organization
Keep related code together
Maintainability
Easier to find and fix bugs
Collaboration
Teams can work on different files
Importing Modules
Style
Syntax
Usage
Import whole module
import math
math.sqrt(16)
Import specific item
from math import sqrt
sqrt(16)
Import with alias
import math as m
m.sqrt(16)
Import multiple
from math import sqrt, pi
sqrt(16), pi
The math Module
Python's built-in math module provides mathematical functions:
Function
What It Does
math.sqrt(x)
Square root
sqrt(16) → 4.0
math.ceil(x)
Round up
ceil(4.2) → 5
math.floor(x)
Round down
floor(4.8) → 4
math.pow(x, y)
x to the power of y
pow(2, 3) → 8.0
math.pi
Pi constant
3.14159...
The random Module
Generate random numbers for games, testing, or simulations:
random.randint(a, b)
Random integer from a to b (inclusive)
random.random()
Random float between 0 and 1
random.choice(list)
Pick one random item
random.shuffle(list)
Shuffle list in place
random.sample(list, n)
Pick n random items
The datetime Module
The os Module
Interact with your computer's operating system:
The json Module
JSON (JavaScript Object Notation) is a universal format for storing and exchanging data. It's used by almost every API.
json.dumps(obj)
Convert Python → JSON string
json.loads(string)
Convert JSON string → Python
json.dump(obj, file)
Write Python to JSON file
json.load(file)
Read JSON file to Python
Creating Your Own Module
Any Python file can be a module. Just save your code and import it:
Understanding Packages
A package is a folder containing multiple modules. It must have an __init__.py file (can be empty).
Installing External Packages with pip
pip is Python's package manager. It downloads packages from PyPI (Python Package Index) — a library of over 400,000 packages!
Command
pip install requests
Install a package
pip uninstall requests
Remove a package
pip list
Show installed packages
pip freeze
List packages with versions
- • requests — make HTTP requests to APIs
- • flask — build web applications
- • pandas — data analysis
- • pygame — create games
Common Mistakes to Avoid
Mistake
Problem
Fix
from math import *
Can overwrite your variables
Naming file same as module
math.py shadows built-in math
Use unique file names
Circular imports
A imports B, B imports A
Restructure your code
Forgetting __init__.py
Folder not recognized as package
Add empty __init__.py
Practical Example: Password Generator
Use multiple modules to create a random password generator:
Practical Example: Date Calculator
Calculate days between dates and future dates:
Practical Example: Data Analyzer
Summary: Quick Reference
Concept
import x
from x import y
import x as y
import pandas as pd
pip install x
Install package
Key Built-in Modules:
- • math — mathematical functions
- • random — random number generation
- • datetime — dates and times
- • os — operating system interaction
- • json — JSON data handling
- • string — string constants and utilities
What's Next?
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.
Continue this course
- Previous: Exception Handling
- Next: Object-Oriented Programming