Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Into the real world
EXPERIMENT 19 / 101

Bring in the right tools.

Use the standard library and keep your own code organised.

BY NAVAN SAMI◷ 6 MIN READPYTHON 3

01 The idea

A module is a unit of Python code you can import. Python’s standard library includes tools for mathematics, paths, JSON, dates, and more. Imports give your program access to those tools. You can also place your own related functions in a .py file and import them.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

modules.py
import math
from pathlib import Path

print(math.ceil(17 / 5))
print(Path("notes") / "ideas.txt")
↳ EXPECTED OUTPUT
4
notes/ideas.txt

03 Under the hood

  1. 1

    math.ceil() rounds up. Here it answers how many batches are needed when a partial final batch still counts.

  2. 2

    Path builds a platform-appropriate path. On Windows, its printed separator may be a backslash rather than a slash; the path still represents the same file location.

  3. 3

    A project’s virtual environment keeps its installed dependencies separate. Create one with python3 -m venv .venv (or py -m venv .venv on Windows), then activate it using your shell’s command. Use python -m pip inside the active environment to install packages.

04 Your turn, builder

A SMALL MISSION

Import math and print the square root of 81.

Reveal one possible solution +
one_possible_solution.py
import math
print(math.sqrt(81))

05 Check your signal

QUICK SIGNAL CHECK

Which import makes math.ceil(...) available?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.