Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 6.4 — A Tour of the Standard Library
Python · Example 3 of 3

A real use: summarising a web-server log

Five made-up log lines, each with a time, a request, a status code and how many milliseconds the reply took. Four small modules (datetime, statistics, collections, json) do the whole job in a few lines. line.split() cuts a line into words (Topic 3.2).

Python ships with hundreds of ready-made modules: maths, randomness, dates and time zones, the operating system, iteration tools, statistics and JSON. Knowing what exists, and how to read the documentation, saves you from rewriting what is already built, tested and fast.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Use statistics to print the mean and median of [4, 8, 15, 16, 23, 42].

02

Call random.seed(1), then print a list of three dice rolls made with random.randint(1, 6). Then explain in one sentence why running the program again gives the same three numbers.

03

Print how many days there are from date(2026, 10, 9) to date(2026, 12, 25) (subtract the earlier date from the later one and use .days).

04

Put the dictionary {"name": "Asha", "scores": [90, 85]} in a variable, then use json.dumps to print it with an indent of 2 and sorted keys.

Explain it without notes

01

What is the standard library, and how do you know a module belongs to it?

02

Why do tests and experiments call random.seed(...), and why not use random for passwords?

03

How should an application treat time zones?

04

How do you find out how to use a module you have never seen?

A real use: summarising a web-server log
Sign in to run this example in your browser.

Expected output

first request at: 09:15:02
average time (ms): 305
median time (ms): 120
status counts: {'200': 3, '500': 1, '404': 1}
{"requests": 5, "slowest_ms": 910, "errors": 2}