The first time I needed a square root, I got as far as writing my own — fifteen lines of guessing and averaging. A friend looked over my shoulder, typed import math, and deleted the whole function. That afternoon is what this post is about: Python ships with an enormous toolbox, and import is how you open the drawer.
import math
math.sqrt(144) # 12.0 — the whole module, every name labeled
from random import randint
randint(1, 6) # one name pulled out, no label
import math brings the module in whole, and six months from now math.sqrt still tells you where it came from. from random import randint is shorter, but the bare name has to explain itself. My habit: plain import by default, from-import for the two or three names I use constantly. And never from random import * — that pours the entire drawer onto the floor, and your own variables can end up shadowed by module names with nothing to warn you.
The standard library is the part of Python nobody told me about soon enough. Before installing anything for a small script, it's worth ten seconds to check whether the language already ships it:
import random
from datetime import date
die = random.randint(1, 6) # a die roll
coin = random.choice(["heads", "tails"]) # a coin flip
stamp = date.today().strftime("%Y-%m") # 2026-05-28
Dates, random numbers, zip archives, sending email, path handling — the box is deep, and half of my early installs were re-importing things Python already had. The two I reach for weekly without thinking: datetime for timestamps and pathlib for everything about paths. I've written up pathlib separately.
The same trick works on code you wrote yourself. Suppose this lives in helpers.py:
# helpers.py
def slugify(title):
return title.lower().replace(" ", "-")
Then any file in the same folder can borrow it:
from helpers import slugify
slugify("The Quiet Terminal") # the-quiet-terminal
That's the whole ceremony: a .py file becomes importable by its own name. The error when borrowing goes wrong is ModuleNotFoundError: No module named 'helpers' — and nine times out of ten that's a folder problem, not a spelling one: the file doing the importing and helpers.py need to sit in the same folder. I once lost twenty minutes to that while the file sat one directory up, perfectly fine, smugly out of reach.
One caveat keeps this from biting you later: any demo or test code at the bottom of the file goes under if __name__ == "__main__":, a one-line guard that runs it only when you execute the file directly, not when someone imports it.
My favorite cheap exercise from the early days: open the docs for random or datetime, pick three functions I'd never used, and try them. It's the fastest way I know to learn what's already in the toolbox — and it's how I stopped missing that fifteen-line square-root function.