Modules and Imports
Imagine you need to repair something at home.
You need:
Screwdriver
Hammer
Wrench
Measuring tape
You have two choices.
Option 1
Build every tool yourself.
Option 2
Go to a store and buy a toolbox containing the tools you need.
Obviously, the second option is usually much more practical.
Python works in a similar way.
You do not need to build every useful feature yourself.
Python provides modules containing tools that you can reuse.
What Is a Module?
A module is a Python file containing reusable code.
It may contain:
- Functions
- Variables
- Classes
- Constants
- Other useful code
Instead of putting everything into one enormous Python file, developers can organize related code into separate modules.
Think:
Toolbox
↓
Collection of useful tools
A Python module is like a small toolbox of reusable programming tools.
The import Statement
Python uses import to bring a module into your program.
For example:
import mathNow you can use tools provided by the math module.
For example:
import mathprint(math.sqrt(25))Output:
5.0
The sqrt function calculates a square root.
The Toolbox Analogy
Imagine you purchase a toolbox.
The toolbox contains:
Hammer
Screwdriver
Wrench
You do not need to manufacture these tools.
You simply open the toolbox and use them.
Similarly:
import mathmakes the math module available to your program.
Then:
math.sqrt(25)means:
"Use the
sqrttool from themathtoolbox."
What Is math?
math is a Python standard-library module containing mathematical functions and constants.
For example:
import mathprint(math.sqrt(16))print(math.pi)Output:
4.0
3.141592653589793
You can think of math as a toolbox for mathematical operations.
What Is random?
Python also provides a random module.
For example:
import randomnumber = random.randint(1, 10)print(number)This generates a random integer between 1 and 10.
Each time you run the program, you may get a different number.
This is useful for:
- Games
- Simulations
- Random selections
- Testing
- Simple experiments
Module Name + Dot + Tool
You will often see syntax like:
module_name.tool_name()For example:
math.sqrt(25)Break it down:
math
↓
Module
sqrt
↓
Function inside the module
The dot means:
"Access something that belongs to this module."
Import Flow
The basic idea is:
flowchart LR
A["Your Python Program"] --> B["import math"]
B --> C["Python loads the module"]
C --> D["Use math.sqrt"]
D --> E["Get result"]
You can think of import as bringing the toolbox into your workspace.
Why Do Modules Exist?
Imagine putting every tool you own into one giant toolbox.
It would become difficult to organize.
Software has the same problem.
A large program could contain thousands or millions of lines of code.
Modules help organize related functionality.
For example:
project
│
├── calculations
├── users
├── payments
├── reports
└── notifications
Each module can focus on a particular responsibility.
Python's Standard Library
Python comes with a large collection of modules known as the standard library.
These provide tools for many common tasks.
Some examples include:
math → Mathematical operations
random → Random values and selections
datetime → Dates and times
os → Operating-system interaction
json → Working with JSON data
You will encounter many more as you continue learning.
You Can Create Your Own Module
Modules are not only things created by Python.
You can create your own.
Suppose you create:
calculator.py
Inside it:
def add(a, b): return a + bThen another Python file can import it:
import calculatorresult = calculator.add(10, 20)print(result)Output:
30
You created a reusable toolbox yourself.
Modules Encourage Reuse
Imagine you write a useful function for calculating discounts.
Instead of copying the same function into five different programs, you can put it into a module and import it wherever needed.
This gives us an important programming principle:
Build once, reuse many times.
It also makes maintenance easier.
If you improve the function in the module, programs using that module can benefit from the improved version.
import Is Not Magic
When you write:
import mathPython does not magically know every function in existence.
You are explicitly asking Python to make that module available.
Then you access its contents.
For example:
import mathradius = 5area = math.pi * radius * radiusprint(area)The program uses the value pi provided by the math module.
A Useful Mental Model
Whenever you see:
import somethingthink:
"I need the tools inside
something."
Then:
something.tool()means:
"Use this particular tool from that toolbox."
For example:
import randomnumber = random.randint(1, 100)Mental translation:
"Bring in the random toolbox and use its
randinttool."
Common Beginner Mistake
Do not assume that every function exists automatically.
For example, this:
sqrt(25)may fail if you have not imported sqrt directly.
Instead, you can write:
import mathprint(math.sqrt(25))The module name makes it clear where the function comes from.
Different Ways to Import
You will eventually encounter different import styles.
The most beginner-friendly style is:
import mathprint(math.sqrt(25))Python also allows:
from math import sqrtprint(sqrt(25))Both can work.
For now, prefer learning:
import modulebecause it makes the source of a function very obvious.
The Toolbox Mental Model
Think of your Python environment as a workshop.
You have:
Your code
+
Imported modules
↓
A larger collection of tools
You do not need to reinvent everything.
Someone has already built many useful tools for you.
Your job is to learn:
- What tools are available.
- Which tool solves your problem.
- How to import it.
- How to use it correctly.
Key Takeaways
- A module is a Python file containing reusable code.
importmakes a module available to your program.- Python provides many useful standard-library modules.
mathprovides mathematical tools.randomprovides tools for generating random values and making random selections.- The dot operator lets you access something inside a module.
- You can create your own modules.
- Modules help organize programs and encourage code reuse.
Beginner rule: Don't build every tool yourself. First check whether Python's toolbox already contains what you need.