Notebook fundamentals

Cells, kernels and execution order — how a notebook actually works and the habit that keeps results trustworthy.

Cells and kernels

A notebook is a document of cells plus a kernel: a live Python process holding all your variables. Cells can be code or Markdown, and the kernel keeps state between them.

PieceWhat it is
Code cellPython that runs against the kernel
Markdown cellNarrative text, headings, LaTeX, tables
KernelThe interpreter process and its memory
In[ ] / Out[ ]Execution counter and result of the last expression
.ipynbJSON file holding cells and outputs
pip install notebook jupyterlab
jupyter lab                     # the modern interface
jupyter notebook                # the classic interface
jupyter nbconvert --to html report.ipynb

Execution order is not document order

Cells run whenever you press Shift+Enter, so the state can end up depending on a sequence that exists nowhere in the file. The notebook then "works" only on your machine, in that order.

[3]  total = 0
[1]  total += 10        # ran BEFORE the line above
[7]  print(total)       # 10 - which line defines total?
⚠️
Before sharing or committing a notebook, use Restart Kernel and Run All. If it fails, the notebook was never reproducible — it merely happened to have the right variables in memory.
  • Number order in the brackets tells you the real execution history.
  • A notebook that only runs top-to-bottom is a notebook you can hand to someone else.
  • Delete the exploratory cells you no longer need; keep the narrative linear.

Magics worth knowing

%timeit sum(range(1000))     # benchmark a line
%%time                       # time a whole cell
%matplotlib inline
%run script.py               # execute a file in the kernel
!pip install pandas          # shell command
%reload_ext autoreload
%autoreload 2                # pick up edits to imported modules
💡
%autoreload 2 saves an enormous amount of time: without it, editing a module you imported has no effect until you restart the kernel.

FAQ

Notebook or script?
Explore and explain in a notebook; ship logic in modules your notebook imports. Code that is going to production should not live only in cells.
How do I share results?
jupyter nbconvert --to html produces a self-contained report. For version control, strip outputs (--ClearOutputPreprocessor) so diffs stay readable.

A notebook workflow that scales Python: getting started

Last refreshed 2026-09-18.