Python · Intermediate level

Intermediate level · Module 3: Modules, Packages and Virtual Environments · Lesson 1 of 3

Using the Standard Library

Solve everyday problems with modules that ship with Python, without installing anything.

In this lesson you will

  • Import and use datetime, random, math and statistics
  • Find documentation for a module

Key concepts

import / from-importdatetimestatisticsofficial documentation

The lesson

Python ships with a large standard library, so many everyday tasks need no extra installation: datetime for dates, statistics for averages and medians, math for calculations, random for sampling, csv and json for files.

Import what you need at the top of the file: from datetime import date. Subtracting two dates gives a timedelta whose .days attribute is the number of days between them.

The official documentation at docs.python.org explains every module with examples and can be downloaded for offline use. Learning to read it is one of the most valuable programming skills.

Step by step

  1. Import date from datetime and calculate the days until a deadline.
  2. Import statistics and calculate the mean and median of a list of scores.
  3. Look up statistics.mode in the documentation and use it.
  4. Print all results in a short report.

Practice activity

Write a script that prints how many days remain until a programme deadline and the mean and median of a list of test scores.

Expected result: Correct day count and statistics printed.

Check your understanding

  1. What is the difference between 'import math' and 'from math import sqrt'?

    Show answer

    The first imports the module (use math.sqrt); the second imports only sqrt into the current namespace.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.