TL;DR Magics output three ways, and the difference explains most notebook confusion: returned values land in
Out[]; printed text only goes to stdout (grab it with%%capture); magics that display markup —%%markdown,%%html, baredisplay()— render it and returnNone; a magic that returns a display object renders it too and lands inOut[]. The ten magics that do 90% of the work:%runexecutes a file (fresh namespace by default, results copied back;-ishares yours);%loadpastes a file into your cell to edit;%%time/%%timeittime it;%%capturesilences and stores the output;%%markdown/%%htmlrender markup;%envsets kernel-wide variables;%whoinventories the namespace (via%%captureif you want the text);%storepersists variables across sessions;%autoreload 2reloads edited modules on the next call;%debugdrops you into pdb at the exact raise site. Every behaviour here was executed on IPython 8.18.1 (2026-12-02) — 92 line magics and 28 cell magics ship; these are the ten.
Type %lsmagic in any notebook and you get 92 line magics and 28 cell magics on IPython 8.18.1 (a few more once extensions like autoreload are loaded). You use about ten. This is the tour of the ones that earn their keep — each behaviour verified on IPython 8.18.1 (2026-12-02) rather than remembered, because magics have three different output channels and the differences are the actual skill.
The one distinction that explains half the confusions #
Magics output three ways, and the notebook treats each differently:
- Returned values land in
Out[n]— one per cell, last-wins. - Printed text goes to stdout — always visible, never in
Out[], capturable. - Display objects (
HTML,Markdown) go to the display hook and render. What survives depends on the source: a magic that returns the object renders it and lands inOut[]— your variable sees the object; a magic that displays it outright (%%markdown,%%html, adisplay()call) renders but leavesNone.
Verified example of the split: a cell running %%markdown **hello** renders bold text and its result is None — the built-in display-paths render but keep Out[] empty … Contrast a custom magic that returns the Markdown: then result is the object and it lands in Out[] — %%capture cap shows the capture holds it as a mime record (a text/markdown payload, not a plain string), while the capture cell’s own value is None only because %%capture itself returns nothing. Once you separate renders from returns, both %%capture and every “why is my output invisible?” mystery resolve instantly.
Running and pulling code: %run vs %load
#
They look interchangeable and are not:
%run script.py— executes the file now, in-process, in a fresh empty namespace by default: the script cannot see your notebook variables, and after it finishes its own variables are copied back into the notebook namespace. Verified:%run demo_src.pyruns the file’sprintimmediately. Add-ito run in your namespace so it can use your variables;-nsets__name__to the file’s stem, skipping anyif __name__ == "__main__"block;-tappends a CPU/wall timing report.%load demo_src.py— queues the file’s source into the cell so you can read and edit before executing. Verified: the queued input is exactly# %load demo_src.pyfollowed by the live source — only the marker line is a comment.
Practical split: %run = “execute this experiment”; %load = “grab last week’s notebook recipe and adapt it”.
Timing without pain: %%time / %timeit
#
- Add
%%timeas the first line of any cell to see CPU/Wall time once. - Switch to
%%timeitto loop the cell and report a distribution — and remember it’s a benchmark, not a run: variables assigned inside a%%timeitbody don’t exist after the cell.
Use %%time during development and %%timeit the moment you catch yourself caring about milliseconds.
Silencing, capturing #
%%capture redirects everything a cell emits into a variable instead of the notebook:
%%capture cap
import noisy_module
cap.stdout # the printed text
cap.outputs # rich display records (markdown/HTML/images) — one record per outputVerified: a print inside the cell is absent from the notebook yet present in cap.stdout, and a displayed %%markdown **hello** shows up in cap.outputs as a text/markdown record. The killer combo: capture a rendered cell’s rich output and re-display or post-process it later — display pipelining without touching the cell above.
Markdown and HTML as first-class output #
%%markdown
**bold** and *italics* # rendered — the notebook's display hook does the work%%html
<div style="color:#e07a38">copper orange box</div>Verified: %%markdown produces a Markdown display record (rendered by the display hook, text/markdown in the message payload), and %%html an HTML record. Perfect for one-off report headers, styled callouts, or embedding a chart export. Note %-prefixed line variants don’t exist for these — they are cell-only, bodies and all.
Environment and state: %env, %store, %who
#
%env LAB=on— set (or read, with%env LAB) environment variables inside the running kernel — libraries reading env at import time behave. Verified:%env LABechoes the variable you set lines earlier.%who/%whos— the honest namespace inventory. Verified caveat:%whoprints to stdout (it’s not a returned value), so to grab it programmatically:%%capture wthenw.stdout— it printedqtyafter aqty = 7.%store qty— persist a variable between sessions (IPython’s pickleshare shelf — files under the profile’sdb/directory, not sqlite). Notebooks load it automatically; a bare IPython shell needs%load_ext storemagic. Verified:%store qtyanswersStored 'qty' (int); the restore,%store -r qty, requires thepicklesharepackage — IPython 8.18.1 no longer lists it as a dependency, and without it the restore raisesTypeError: 'PickleShareDB' object is not subscriptable(pip install pickleshareif your environment is minimal). Small — but the difference between re-running a 40-minute data pull and not.
Reload loops: %autoreload
#
The two-liner that makes notebook-driven library development sane:
%load_ext autoreload
%autoreload 2Now every edit to your imported .py modules is picked up on the next call — verified: the extension loads, autoreload 2 activates, a function returns 1, the file is edited, the very next call returns 2. Turn it on before your imports (activating it after an import leaves that module stale until it re-imports). (Caveat, same as any reload scheme: objects holding references to old instances, open files, or decorators with cached state get weird. Restart the kernel when the weird starts.)
A last-resort favourite: %debug
#
When a cell raises, type %debug in the next one — you drop into pdb at the point of the exception, with locals intact. Paired with %xmode context (verified) versus %xmode plain, it turns post-mortem debugging from archaeology into conversation. No breakpoint gymnastics needed after the fact.
The tour, one table #
| Magic | Verified behaviour on IPython 8.18.1 |
|---|---|
%run file.py |
executes now, fresh namespace by default, results copied back (-i shares; -n skips __main__ guards; -t adds timing) |
%load file.py |
pastes source into the cell for editing (only the # %load marker is a comment) |
%%time / %%timeit |
one-shot CPU+wall / benchmarked distribution (assignment inside %%timeit doesn’t outlive the cell) |
%%capture cap |
stdout → .stdout; rich outputs → .outputs; the capture itself returns None |
%%markdown / %%html |
display-render; result None, never in Out[] (cell-only — no %-line variants) |
%env KEY=VAL / %env KEY |
set / read kernel-wide env |
%who, %whos |
namespace inventory — prints, not returns |
%store x, %store -r x |
persist across kernel sessions — -r needs the pickleshare package |
%load_ext autoreload + %autoreload 2 |
live module reload (activate before imports) |
%debug, %xmode |
post-mortem pdb at the raise site |
Your dashboard #
| Symptom | Magic that fixes it |
|---|---|
| “It printed but I lost the text” | %%capture name → name.stdout |
| “It rendered but the variable is None” | a display() call or a bare capture — returned display objects do land in Out[]; use %%capture name → name.outputs |
| “I keep editing a .py and re-importing” | %load_ext autoreload + %autoreload 2 |
| “Which magics exist again?” | %lsmagic — and %magic for the full manual |
| “The traceback is a wall of context” | %xmode plain |
| “Same setup lines at the top of every notebook” | a custom magic — see the companion piece |
Behaviours verified live in a shell: IPython 8.18.1, Python 3.9, 2026-12-02 (counts 92 line / 28 cell).
And when these ten stop being enough, the next rung is writing your own: Jupyter Magic — custom: write your own %magics.