CHAPTER 6 · 7 lessons · 91 exercises

Modules and the standard library

Split your code across files, and use the tools Python comes with. You stop having to write everything yourself.

0 / 91 solved

So far every program has lived in one file. That is fine while you are learning, but real programs do not fit in one file — at a few thousand lines, even the person who wrote it cannot find the bit they want.

Modules are how you split code across files. In Python a .py file simply *is* a module. Put some functions in calc.py and another file can use them by writing import calc. No declaration, no config file. Put the file there and it is already a module.

There are three ways to bring one in. import calc takes the whole thing and you call calc.add(). from calc import add takes just what you need and you call add() directly. And as gives it a short name, like import numpy as np. All three turn up mixed together in other people's code, so we will look at each.

The second half is the treat. Python ships with a standard library — a pile of tools already there, no installing, no setting up, just import and go. People say Python comes with batteries included, and it is one of the reasons it is used so widely.

We cover four. math gives you square roots, pi and the rest. random covers anything whose answer changes each run, like a dice roll or a draw. datetime does dates and times, so you never have to work out what date it is in thirty days. And json is the shared way applications pass data to each other — the common tongue of the web.

The point is not to memorise those four. It is to know that whatever you want to do, somebody has probably already made it. Look before you write. Once that habit sticks, the range of what you can build widens fast.

One aside: Python also has pip, which fetches tools people around the world have made. That is for your own computer, and it cannot run inside this course's browser sandbox. The slides show you the steps, so come back to them when you are working on a PC.

Lessons in this chapter

Split your code into files

  1. 68One file keeps growingAt a few thousand lines, even the person who wrote the file cannot find the bit they want. Start by cutting it up by role.Go to the exercises
  2. 69Divide the files and call acrossMake calc.py, write import calc, and you can use it as calc.add().Go to the exercises
  3. 70Three ways to bring one inThe whole thing, only what you need, or under a short name. Other people's code has all three mixed together.Go to the exercises

The toolbox that comes with Python

  1. 71Leave the sums to somebody elseSquare roots, pi — you do not have to supply them. Just import math.Go to the exercises
  2. 72Roll a dieDraws and shuffles both live in random — though you need a fixed seed to get the same result twice.Go to the exercises
  3. 73Work out a dateYou never have to count out "what date is it in thirty days". Leap years and month ends included.Go to the exercises
  4. 74A shape for passing data aboutTurn a dictionary or a list into a string, and back again. JSON is the common tongue of the web.Go to the exercises

Next up is “Reading and writing files”. Leave data outside your program, so your values are still there once it has finished running.