Notebook habits
Last updated
A notebook is easy to start and easy to make a mess of. In this lesson you'll learn the habits that keep one readable and trustworthy: notes between the code, imports at the top, one idea per cell, looking things up with help(), timing code with %timeit, and never trusting memory you can't see.
By the end of this lesson you can
- Lay out a notebook so someone else can follow it from top to bottom
- Look up what a function does with
help(),dir()andtype() - Time two ways of doing the same thing with
%timeit - Explain why a deleted cell can still affect the notebook until a restart
A notebook that reads like a report
Here's a small notebook written with good habits. Press Run all, then read it from the top as if you'd never seen it.
-
Batangas store: rice sales, first look
This notebook totals a week of rice sales and finds the busiest day.
Data: kilos sold each day, typed in from the store's log.
-
-
Total and average for the week:
-
-
The busiest day:
-
-
Found: Saturday was busiest, at 66 kg, a third more than the average.
What makes it easy to follow:
- A heading and a sentence at the top say what the notebook is for, and what data it uses.
- Imports and data come first, in one cell, so you can see what it depends on.
- One idea per cell, each with a short note above it.
- A finding at the end, written in words. Numbers on their own don't say what they mean.
- A file name that starts with a number,
01-, so notebooks sort in the order they're meant to be read.
Good habit or bad habit?
Sort each one.
0 of 6 placed right
All the
importlines in the first code cellOne cell that loads, cleans, charts and saves, 80 lines long
A sentence before each code cell saying what it does
A cell that changes a value, then gets deleted
Restart and run all before sharing
Variables named
x,x2anddf_final_v3
A 2019 study tried to rerun hundreds of thousands of notebooks published on GitHub. Only about a quarter ran from top to bottom without an error, and only about 4% gave the same results their authors had saved. Most failures came from the habits in the right-hand column.
Looking things up
You don't need to remember everything. Python can tell you about itself:
help(something)prints the documentation for a function or module.dir(something)lists the names inside it. The list comprehension in the second cell hides the names that start with_, which are for Python's own use.type(value)says what kind of value something is. You'll use it constantly to check that a "number" really is a number and not text.
Run each cell. Then try help(statistics.median) in a new cell.
In Jupyter on your own computer, there are shortcuts too: typing round? and running it shows the same help in a side panel, and pressing Tab after a dot (statistics.) lists what comes next. This page's notebook doesn't have those; help() and dir() work everywhere.
How long does it take?
Lines starting with % aren't Python. They're magics: commands for the notebook itself. The one you'll use most is %timeit, which runs a line many times and reports how long one run takes on average.
Run all four cells. The first two make a list of 10,000 numbers and a function that adds them with a loop. The last two time the loop and Python's built-in sum.
On most computers sum is several times faster, because it's written in C inside Python, not in Python itself. That's the idea behind NumPy in Stage 2, taken much further. Your numbers will differ from anyone else's: they depend on the computer and on what else it's doing. That's why %timeit runs the line many times and gives a spread (the ±).
Here's what the two numbers are. %timeit makes 7 runs and times each one, to (each run repeats the line many times and divides, so is the time for one loop). The first number is their mean, as in lesson 1.1:
The number after ± is their standard deviation: roughly, how far a typical run is from the mean.
A small next to means the runs agreed, and you can trust the comparison. Stage 8 explains standard deviation properly.
Wrong way: the cell you deleted
Run both cells. The second one works out a price with 12% VAT: for a price and a VAT rate ,
Now delete the line in the first cell, so it's empty, and run the second cell again. It still works, because vat_rate is still in memory. Nothing on the page says where it came from.
Now press Restart and run all.
NameError: name 'vat_rate' is not defined. The notebook only ever worked because of a cell that no longer exists. Anyone you send it to would get this error, and so would you tomorrow. Put the line back and Restart and run all again.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- JupyterLab: Notebooks: Markdown and code cells, restarting the kernel
- IPython: Built-in magic commands, %timeit: what %timeit measures and how it reports it
- The Python Standard Library: timeit: the module %timeit is built on
- The Python Standard Library: Built-in Functions, help(): Python's own help system
- PEP 8: Imports: imports go at the top
- Pimentel et al., A large-scale study about quality and reproducibility of Jupyter notebooks (MSR 2019): most notebooks on GitHub didn't give the same results when rerun from the top