One-sentence summary
A module is a file full of ready-made code that other people wrote earlier; with the import command we bring that code into our own program and use it without writing it again.
Why does this matter?
Until now, you wrote every line yourself. That is a great start, but writing things like a square root, a random number or today's date from scratch every single time would be exhausting.
Here is the good news: other people have already written most of these tasks and packed them into tidy bundles. We call these bundles modules and libraries. When we call them, we get to use ready-made, tested code inside our own program.
Think of it like cooking. In the kitchen you do not make your own salt, flour and sugar each time. You take the ready ones from the cupboard and focus on your dish. Modules are like the cupboard of the coding world. When I was learning this, the thing that surprised me most was that a single line of import suddenly makes hundreds of ready commands available.
Using modules has two big benefits. First, we save time, because we do not build complex tasks from scratch. Second, we make fewer mistakes, because this code has already been tried and fixed by thousands of people before us. So using a module is not laziness; it is working smartly.
What is a module, and how does import work?
A module is a Python file that holds ready-made functions and values. To bring that file into our program, we use the import command.
The most basic form: import
Let's call the math module that comes with Python. After calling a module, we use a tool inside it by writing module name + dot + tool name.
import math
result = math.sqrt(16)
print(result)
This program prints 4.0. The expression math.sqrt means "the sqrt tool of the math module"; sqrt stands for "square root". The dot means "inside of".
The same module holds other ready-made values too:
import math
print(math.pi)
print(math.sqrt(144))
This program first prints about 3.141592653589793, then 12.0. math.pi is the number pi, already calculated for us, so we never have to type it ourselves.
Another form: from ... import ...
Sometimes we only need one tool from a module. Then we can take just that tool, so we do not have to write the module name every time.
from math import sqrt
print(sqrt(81))
This program prints 9.0. Notice that here we wrote sqrt directly, not math.sqrt, because we imported the tool by name. Both forms are correct and do the same job.
Three useful modules from the standard library
When Python is installed, many ready-made modules come with it. All of them together are called the standard library. A library is a larger collection made of related modules. Let's meet three of them.
1. random — randomness
In the previous lesson you met randomness. That randomness came from the random module.
import random
dice = random.randint(1, 6)
print("Dice:", dice)
Every time it runs, random.randint(1, 6) gives a random number between 1 and 6 (both included). That is why you may see a different result each time you run this program. When you roll a die you cannot know the result in advance; the random module gives the computer that same uncertainty and makes our games full of surprises.
2. math — mathematics
The math module offers ready-made math tasks such as square root, power and rounding.
import math
print(math.pow(2, 5))
print(math.floor(3.8))
This program prints 32.0 (2 to the power of 5) and 3 (rounded down). We could write these tools ourselves, but using ready ones is both faster and safer.
3. datetime — date and time
The datetime module gives us today's date, the time and time calculations.
import datetime
today = datetime.date.today()
print("Today's date:", today)
This program prints the current date on your computer, for example in the form 2026-07-25. So if the computer clock shows 25 July 2026, that is the output. Because the module brings us the date, we do not have to count the month and day ourselves.
Notice that datetime.date.today() has two dots. First we reach the datetime module, then the date tool inside it, then the today command. Each dot means "inside of", so you can picture it like opening the shelves of a cupboard one by one.
A short look at libraries and pip
The standard library comes with Python, but people have also written thousands of other libraries. To add these to a computer later, we usually use a tool called pip. For example, you type pip install ... in the terminal and the library is downloaded.
For this lesson you do not need to install anything with pip, because math, random and datetime already come with Python. Installing an outside library means downloading a file from the internet. For that reason, always use the pip install command together with an adult, and only for trusted, well-known libraries.
Mini practice
Combine the three modules you learned in a single program. Type the program below into a file and run it.
import math
import random
import datetime
number = random.randint(1, 100)
root = math.sqrt(number)
today = datetime.date.today()
print("Chosen number:", number)
print("Square root:", root)
print("Date:", today)
Each time it runs, the program picks a different number, finds its square root and prints today's date. Things to try:
- Replace
random.randint(1, 100)withrandom.randint(1, 10); how does the range of results change? - Instead of
math.sqrt, trymath.pow(number, 2)to find the square of the number.
Common mistakes
Forgetting the import line
If you try to use a module without calling it, Python gives a NameError. Make sure the import line is at the top before you use a tool.
Using a space instead of a dot
Wrong: math sqrt(16) Right: math.sqrt(16) We join the tool name to the module name with a dot, not a space.
Misspelling the module name
Typos like import maths or import radnom mean the module cannot be found. The correct names are: math, random, datetime.
Mixing up the two forms
If you wrote from math import sqrt, you now use sqrt(16), not math.sqrt(16). If you wrote import math, the opposite is true.
Safety note
Only run code that you wrote yourself or that comes from trusted, well-known sources. Before installing a library you found online with pip install, always ask an adult. Do not write or share personal information such as your name, address or password in your programs.
Review questions
- What problem does a module solve in a growing Python project?
- Why is importing a specific name sometimes clearer than importing everything?
- What should be checked before installing a third-party library?
- How can a version difference make a working example fail?
- What is the purpose of a requirements file or dependency record?
- When should code become your own module?
Answers
- A module groups related code behind a reusable name, reducing duplication and making responsibilities easier to understand.
- It makes dependencies visible and avoids unexpected name collisions.
- Check the official source, maintenance status, licence, supported Python versions and whether the library is truly needed.
- Functions, parameter names or behaviour may have changed, so the example and installed library no longer describe the same interface.
- It records exact or compatible dependency versions so another person can recreate the project environment.
- When a coherent group of functions or classes is reused, independently testable or making the main file difficult to read.
Lesson summary
- A module is a file of ready-made code that we call into our program with
import. - In uses like
math.sqrt(16), we join the tool name to the module name with a dot. - With the
from math import sqrtform, we can take one tool and use it directly by name. math,randomanddatetimeare parts of the standard library that comes with Python.- To install an outside library we use
pip, and this should always be done with adult supervision.
Check your understanding
- What does the
importcommand do? - What does
math.sqrt(25)print? - What is the difference between
import mathandfrom math import sqrt? - Which module and command do we use to get today's date?
- Which tool do we usually use to install a new library, and what should we be careful about while doing it?
Answers
- It lets us call and use ready-made code from another file (a module) inside our own program.
- It prints
5.0, because the square root of 25 is 5 andsqrtreturns a decimal result. import mathbrings in the whole module and we writemath.sqrt;from math import sqrtbrings in only thesqrttool and we writesqrtdirectly. Both do the same job.- We use the
datetimemodule and get today's date with thedatetime.date.today()command. - We usually use
pip(for examplepip install ...). We should do this only for trusted libraries and together with an adult.
Source and verification note
For “Modules and Libraries”, verification focuses on whether the relationship between Why does this matter? and The most basic form: import remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
Project: Number Guessing Game