Home · Academy · Robotics & Coding · Python Fundamentals · Modules and Libraries

Modules and Libraries

Learn to use modules with import and standard libraries like math, random and datetime.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

Project: Number Guessing Game

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteRandomness and Simple Game Logic
ContentStandard lesson · 1,397 words
Last updated

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:

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

  1. What problem does a module solve in a growing Python project?
  2. Why is importing a specific name sometimes clearer than importing everything?
  3. What should be checked before installing a third-party library?
  4. How can a version difference make a working example fail?
  5. What is the purpose of a requirements file or dependency record?
  6. When should code become your own module?

Answers

  1. A module groups related code behind a reusable name, reducing duplication and making responsibilities easier to understand.
  2. It makes dependencies visible and avoids unexpected name collisions.
  3. Check the official source, maintenance status, licence, supported Python versions and whether the library is truly needed.
  4. Functions, parameter names or behaviour may have changed, so the example and installed library no longer describe the same interface.
  5. It records exact or compatible dependency versions so another person can recreate the project environment.
  6. When a coherent group of functions or classes is reused, independently testable or making the main file difficult to read.

Lesson summary

Check your understanding

  1. What does the import command do?
  2. What does math.sqrt(25) print?
  3. What is the difference between import math and from math import sqrt?
  4. Which module and command do we use to get today's date?
  5. Which tool do we usually use to install a new library, and what should we be careful about while doing it?

Answers

  1. It lets us call and use ready-made code from another file (a module) inside our own program.
  2. It prints 5.0, because the square root of 25 is 5 and sqrt returns a decimal result.
  3. import math brings in the whole module and we write math.sqrt; from math import sqrt brings in only the sqrt tool and we write sqrt directly. Both do the same job.
  4. We use the datetime module and get today's date with the datetime.date.today() command.
  5. We usually use pip (for example pip 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

Start QuizBack to Python Fundamentals
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.