A module is a file containing pre-written Python code — functions, classes, and constants — that you can import and use in your own programs without writing it from scratch. Modules encourage code reuse, reduce errors, and are a key technique for keeping large programs manageable.

What problem do modules solve?

Imagine you need to generate a random number in your program. You could write the mathematical algorithm yourself — but that requires understanding pseudo-random number generation, which is complex and error-prone. Instead, Python's random module provides a thoroughly tested randint() function that you can use instantly.

This is the core purpose of modules: don't reinvent the wheel. A module is a self-contained file of code that solves a specific set of related problems. By importing it, you gain access to everything in it without duplicating the code into your own file.

In computing, this principle is called code reuse — one of the most important software engineering concepts, and explicitly assessed at GCSE.

How do you import a module in Python?

There are two main ways to import a module:

Method 1 — Import the whole module:

import math

result = math.sqrt(144)   # 12.0
rounded = math.floor(3.7) # 3
pi_value = math.pi        # 3.141592653589793

print(result)     # 12.0

You access functions using module_name.function_name() — the prefix makes it clear which module the function came from.

Method 2 — Import specific names:

from math import sqrt, pi

result = sqrt(49)    # 7.0  — no "math." prefix needed
print(pi)            # 3.141592653589793

This brings the named items directly into your namespace. Useful for frequently used functions, but risks name conflicts if two modules define the same name.

What modules are most useful for GCSE projects?

Module What it provides Common use at GCSE
math Mathematical functions: sqrt, floor, ceil, pi, log Calculations, geometry
random Pseudo-random numbers: randint, choice, shuffle, random Games, simulations, quizzes
datetime Dates and times: date.today(), datetime.now() Timestamps, age calculations
os Operating system interface: file paths, directory listing File management
csv Read and write CSV files Data storage in projects
string String constants: string.ascii_letters, string.digits Input validation, password generation

The most commonly examined are math and random. If your GCSE project uses a quiz, game, or simulation, you will almost certainly import random.

How does the random module work in practice?

import random

# Random integer between 1 and 6 (inclusive)
dice = random.randint(1, 6)
print("You rolled:", dice)

# Random choice from a list
colours = ["red", "green", "blue", "yellow"]
chosen = random.choice(colours)
print("Chosen colour:", chosen)

# Shuffle a list in place
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(deck)
print("Shuffled:", deck)

random.randint(a, b) returns a random integer N such that a <= N <= b. Both endpoints are inclusive — unlike Python slice notation.

What is the difference between a module, a library, and a package?

These terms are related and sometimes used interchangeably, but they have precise meanings:

Term Definition Example
Module A single Python file (.py) containing code math.py, random.py
Package A directory containing multiple related modules os (contains os.path, os.stat, etc.)
Library A general term for a collection of pre-written code (could be one or many modules/packages) "The Python standard library"
Standard library The collection of modules that comes built into Python — no installation needed math, random, csv, datetime
Third-party library External code you install separately (e.g. via pip) pygame, requests, numpy

At GCSE, the distinction that matters most is standard library (already available) vs third-party (must be installed). Your GCSE project should use only standard library modules unless your teacher has confirmed a third-party module is available in the exam environment.

How do modules support modular design?

Modular design (also called modularisation) is the practice of breaking a large program into smaller, self-contained sections — each handling one specific task. Modules are the natural unit of modular design in Python.

Benefits:

  • Easier testing — you can test a module independently of the rest of the program.
  • Easier maintenance — a bug in the random module only needs to be fixed once, not in every program that uses it.
  • Team collaboration — different people can work on different modules without interfering with each other.
  • Readability — a program that imports calculate_score from a separate module is much clearer than one 500-line file.

Frequently asked questions

Do I need to know all the functions in every module for GCSE?

No. Exam questions about modules typically either tell you which module to use and ask you to write the correct import and call, or show you module code and ask you to trace it. You should know the most common functions from math (sqrt, floor, ceil, pi) and random (randint, choice, shuffle). Memorising complete module documentation is not required.

What happens if you try to use a module function without importing it first?

Python raises a NameError. For example, attempting result = sqrt(25) without importing math or from math import sqrt gives NameError: name 'sqrt' is not defined. The function exists — it just isn't in your program's namespace until you import it.

Can you write your own module?

Yes. Any .py file is a module. If you create a file called my_functions.py containing function definitions, you can import it in another file with import my_functions. This is the proper GCSE-level definition of modular design: splitting your program across multiple files, each responsible for a distinct area of functionality.

Is import random the same as from random import *?

No. import random imports the module and requires random.randint() syntax. from random import * imports every name from the module directly into your namespace, letting you write randint() without a prefix. The second form is generally discouraged because it can cause name conflicts and makes it hard to tell where a function came from — import random with the random. prefix is clearer and safer.


Learn to use Python's module system and apply modular design to your GCSE project with Professor Turing at aitutors.me.