CODE PATH
PRO ACCOUNT

Master Code
On The Go

Learn. Practice. Build.

Quest 2 β€’ Lesson 2

πŸ“¦ Modules & Packages

Learn how to organise your code with modules and packages – essential for building real‑world Python projects.

A module is a single Python file (`.py`) containing functions, classes, and variables. A package is a collection of modules organised in folders. They help you structure code logically and reuse it across projects.

"Modules are like books on a shelf – each one covers a specific topic. Packages are like bookshelves that group related books together."

πŸ”§ Built-in Modules

Python comes with a wide range of built‑in modules. Let's explore a few.

Simulate importing and using built‑in modules:

Click a button to see a built‑in module in action.
πŸ“˜ Example: using the math module
import math

print(math.sqrt(16))   # 4.0
print(math.pi)         # 3.14159
print(math.floor(3.7)) # 3

πŸ› οΈ Creating Your Own Module

Create a simple module with helper functions.

Simulate a module file:

# my_utils.py
def greet(name):
    return f"Hello, {name}!"

def add(a, b):
    return a + b

def multiply(a, b):
    return a * b
Click a button to use the custom module.

πŸ“ Package Structure

A package is a folder with an `__init__.py` file (can be empty).

πŸ“ my_project/
  πŸ“ utils/
    πŸ“„ __init__.py
    πŸ“„ strings.py
    πŸ“„ numbers.py
  πŸ“ data/
    πŸ“„ __init__.py
    πŸ“„ database.py
  πŸ“„ main.py
# Importing from the package
from utils.strings import capitalize
from utils.numbers import add

result = add(5, 3)
print(capitalize("hello")) # "Hello"
modules_demo.py
# Importing built-in modules
import math
import random
from datetime import datetime

# Creating a custom module (my_utils.py)
# Then import and use it
import my_utils

print(my_utils.greet("Alice"))
print(my_utils.add(5, 3))

# Importing from a package
from my_package.utils import helper

✨ Challenge: Build a Simple Package

Create a package structure with:

➑️ Next Lesson

Lesson 2.3: Working with CSV & JSON – handle structured data.

Continue to Lesson 2.3 β†’

(Coming soon – check back or buy Pro Pack for early access)

❀️ Support Free Education

This course is 100% free. If it helps you, consider buying me a coffee.

β˜• Buy Me a Coffee
← Back to Python Course Hub