Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 01

Modules in Python — Visual & Easy to Understand

Understand why modules exist, how import works, and the difference between built-in and custom modules — with a toolbox analogy.

Intermediate25 min
Modules in Python toolbox hero — Module 14 reuse code

Real-life analogy: A module is like a toolbox. Instead of rebuilding tools every time, you keep them in one box and use them whenever you need them.

Use the playground and quiz at the bottom to turn these visuals into runnable code.

What you will learn

  • Why modules exist — write once, use everywhere
  • Function vs module — one file vs shared across projects
  • What lives inside a module (variables, functions, classes)
  • Built-in vs custom modules and how import works
  • Your path through Module 14: custom modules → import → reload → multi-file apps

Write once. Use everywhere.

Instead of copying the same functions into every program, save them in one .py file and import them whenever you need them.

python
# Without a module — copy logic into every file (bad)
c1 = 10 + 20
c2 = 30 + 40

# With a module — write once in arithops.py, import everywhere (good)
import arithops
arithops.sumop(10, 20)

Why do modules exist?

Imagine you're building three different apps. All of them need the same calculator.

Without modules With modules
Copy the calculator into every project Everyone shares one toolbox
Fix a bug in 3 places Fix it once in the module
Code drifts out of sync One source of truth
Without module copy paste everywhere vs with module one shared toolbox
App A and App B import shared arithops.py toolbox module

Key idea: One module can be imported by many programs.

Definition: A module is a collection of global variable names, function names, and class names stored in a .py file.


Function vs module

Students often confuse these two.

Function reuse inside one file vs module shared across projects
Function one file arrow module shared across projects
Function Module
Scope One file Many files / programs
Analogy One chef, one pot Recipe book in a shared kitchen
Code def sumop(a, b): ... # arithops.py then import arithops

Function — reuse inside one file:

python
def sumop(a, b):
    return a + b

print(sumop(10, 20))

Module — reuse across many files:

python
# arithops.py
def sumop(a, b):
    return a + b
python
# main.py
import arithops
print(arithops.sumop(10, 20))

What is inside a module?

A module is simply a Python file (.py) containing reusable code.

mathconstants.py contains variables functions classes layers

Example

python
# mathconstants.py

PI = 3.14
E = 2.71

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

Two types of modules

Built-in Python library shelf vs custom module you create
Type Who writes it Examples
Built-in Python developers math, sys, csv, calendar, re, functools
Custom You or your team arithops.py, bankinfo.py, mathconstants.py

Python also auto-imports builtins — so print(), len(), and input() work without an explicit import.


How import works

Imagine this project folder:

project folder main.py imports arithops.py and bankinfo.py

main.py:

python
import arithops

arithops.sumop(10, 20)

Python looks for arithops.py in the same project folder and loads it.


Built-in module example

math is a toolbox already installed with Python.

import math math.pi math.sqrt flow
python
import math

print(math.pi)
print(math.sqrt(16))

Custom module example

You create your own reusable toolbox.

File Code
arithops.py def sumop(a, b): return a + b
main.py import arithops → print(arithops.sumop(10, 20))
Output 30
Custom module arithops.py imported by main.py output 30

Pick a toolbox — math, csv, or custom

Think of Python modules as different toolboxes. Try each in the playground:

Toolbox Import Example call Result
Math import math math.sqrt(25) 5.0
CSV import csv csv.__name__ 'csv'
Custom (inline def sumop) sumop(10, 20) 30
python
import math
print(math.sqrt(25))   # 5.0

import csv
print(csv.__name__)    # csv

Learning roadmap

Modules learning roadmap custom import from-import reload packages

You're currently here:

# Lesson Focus Time
2 Custom Modules Build .py files, __pycache__ 35 min
3 import Statement 4 import syntaxes 40 min
4 from … import 3 from-import syntaxes 35 min
5 Module Reload importlib.reload() 25 min
6 Multi-Module Project Menu + operations split 45 min
7 Packages Overview Function vs module vs package 35 min
8 Creating Packages Folder + __init__.py + modules 40 min
9 Reusing Packages sys.path.append + PYTHONPATH 35 min
10 PYTHONPATH Global Setup Windows · macOS · Linux 40 min

Interview memory card

Interview memory card function module builtin custom

One-line definition: A module is a Python .py file containing reusable variables, functions, and classes that can be imported into other programs.

Key takeaways

  • Toolbox analogy — keep tools once, use them in many projects
  • Functions → one file; modules → shared across programs
  • Built-in (math, csv) vs custom (arithops.py)
  • import arithops → Python loads arithops.py from the same folder
  • Next: build your own module in Custom Modules

Loading playground…

Check your understanding

Modules overview quiz

What is the main purpose of Python modules?

1 / 10