Reusable programs · 11 min

Modules and imports

Reuse code from the Python standard library.

A module groups Python definitions. import statistics gives you the module name. Call a function such as statistics.mean() through that name. A regular Python file can become a module when another file imports it.

The standard library ships with Python. On your own computer, create an isolated environment for third-party packages with python -m venv .venv, then use that environment’s Python to install packages. This course uses standard-library modules so the browser examples run as written.

Follow the example

The statistics module becomes available under its name.

Try it yourself

Add 20 to the scores and print their median. Format it with one decimal place.

Target output

9.0
Show a hint

statistics.median uses the two middle numbers when the list has an even number of items.

Output

Ready

Run your code to see the output here.

The first run downloads Python. Code runs in your browser. Progress and drafts are stored on this device.

Show one solution
import statistics

scores = [6, 8, 10, 20]
print(f"{statistics.median(scores):.1f}")