Using libraries

About this lesson: Using libraries

In short

A library is a collection of ready-made, tested routines. Making random numbers yourself takes tricky code; a library does it in one line. Compare the two programs.

Using libraries

What you'll explore

Libraries: Don't reinvent the wheel
A library is a collection of ready-made, tested routines. Making random numbers yourself takes tricky code; a library does it in one line.
Calling a function: Roll the dice
import random loads the library; random.randint(1, 6) calls one of its functions and gives back a whole number from 1 to 6.
In a game: A new question every time
Games use random numbers so they are different every time. This maths room makes a new times-table question with two calls to randint.
Check yourself: Which line?
Sara wants her program to pick a random number from 1 to 100.

Also in this lesson

  • Common mix-ups
  • Python libraries to try
  • A complete dice game
  • Rock, paper, scissors

Words to know

library
A collection of ready-made, tested routines.
module
Python's name for a library file.
import
Load a library so its functions can be used.
function
A named routine that does a job and may give back a value.
call
Run a function.

Sources

  • Python documentation: random module

An interactive lesson with a live experiment: move the sliders, press the buttons and answer quick questions. 4 short steps and a 6-question quiz. Part of the Family plan. Written and checked by Shalo Lab. Last updated 11 November 2026.

← Combining conditions in code · Planning a database →

All lessons in Term 3 · Computing · Age 12