All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Modularity: subprograms, libraries and files

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Modular code is built from blocks: your own subprograms, Python's built-in ones and libraries of subprograms that you import.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Kinds of module (spec 9.1.3)

Importing

The four import forms in the PLS:

import math
import random as rnd
from math import sqrt
from math import pi as PI_VALUE
print(math.floor(4.7), sqrt(81), round(PI_VALUE, 3))
rnd.seed(1)
print(rnd.randint(1, 1))

Output:

4 9.0 3.142
1

Dates and times

The PLS uses ISO 8601 text such as 2026-11-08 14:30:00, in UTC. time.time() and timestamp() count seconds since 1 January 1970 00:00:00 UTC (the epoch).

from datetime import datetime, timezone
FORMAT = "%Y-%m-%d %H:%M:%S"
start = datetime.fromtimestamp(0, timezone.utc)
print(start.strftime(FORMAT))
t = datetime.strptime("2026-11-08 14:30:00", FORMAT).replace(tzinfo=timezone.utc)
print(t.timestamp())
print(datetime.fromtimestamp(t.timestamp() + 3600, timezone.utc).strftime("%H:%M:%S"))
print(t.date())

Output:

1970-01-01 00:00:00
1794148200.0
15:30:00
2026-11-08

datetime.now(timezone.utc) gives the current time; time.sleep(sec) pauses the program.

Turtle graphics

The turtle library draws by moving a pen. In the default 'standard' mode the turtle starts facing east and left() turns anticlockwise. Common calls: turtle.Turtle(), forward(d), back(d), left(a), right(a), penup(), pendown(), pencolor(), fillcolor(), begin_fill(), end_fill(), circle(r), setposition(x, y), setheading(a), turtle.done().

import turtle
t = turtle.Turtle()
for side in range(6):
    t.forward(50)
    t.left(60)
turtle.done()

This draws a regular hexagon: to close a regular polygon with n sides, the turtle turns 360 / n degrees at each corner.

Worked example

How many seconds after the epoch is 1970-01-02 00:00:00 UTC, and what does the timestamp 90061 display as?

  1. One day = 24 x 60 x 60 = 86400 seconds
  2. datetime.fromtimestamp(90061, timezone.utc).strftime("%Y-%m-%d %H:%M:%S") gives 1970-01-02 01:01:01

Answer: 86400 seconds; 1970-01-02 01:01:01

Key idea: Use in-built subprograms and import libraries rather than rewriting them; know the four import forms; dates are ISO 8601 strings in UTC, converted with strptime, strftime and timestamps.

Check yourself

  1. What is displayed when this code runs? ```python from math import ceil, floor print(ceil(2.1) + floor(2.9)) ```
    Show answer

    5. ceil(2.1) = 3 and floor(2.9) = 2: 5.

  2. Which exception is raised when this code runs? ```python import math print(sqrt(16)) ```
    • NameError
    • ValueError
    • TypeError
    • AttributeError
    Show answer

    NameError. import math only creates the name math; sqrt must be written math.sqrt, so NameError.

  3. What is displayed when this code runs? ```python import math as m print(m.trunc(-3.8) + m.fabs(-2)) ```
    Show answer

    -1. m.trunc(-3.8) is -3 and m.fabs(-2) is 2.0, so the result is -1.0.

  4. What is displayed when this code runs? ```python from datetime import datetime, timezone print(datetime.fromtimestamp(3600 * 5, timezone.utc).strftime("%H:%M:%S")) ```
    Show answer

    05:00:00. 18000 seconds after midnight on 1 January 1970 is 5 hours: 05:00:00.

  5. Two readings were taken at 2026-11-08 09:15:00 and 2026-11-08 10:05:30 (UTC). Using timestamps, how many seconds apart are they?
    Show answer

    3030. Convert each to a timestamp and subtract: 50 minutes 30 seconds = 3030 seconds.

  6. A turtle draws a regular pentagon with five forward() moves. Through what angle, in degrees, must it turn left at each corner?
    Show answer

    72 degrees. The turns add up to one full turn, so each is 360 / 5 = 72 degrees.

  7. Why should you use a library function such as math.sqrt() instead of writing your own square root code?
    • Library functions never raise exceptions
    • Your own code would not be allowed in the exam
    • It makes the program use less memory in every case
    • It is already written and tested, so it saves time and is reliable
    Show answer

    It is already written and tested, so it saves time and is reliable. Reusing tested code is quicker and less error-prone.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.