Run a Jupyter notebook end to end and learn to avoid hidden state with restart and run all.
Why it matters
Most real analysis happens in a notebook: a document that mixes runnable code, its output, tables, charts and written notes. Jupyter is the standard format (.ipynb), and Google Colab, JupyterLab, VS Code and Kaggle all open it. Being comfortable in a notebook, and knowing its traps, is a day-one skill for analysts and scientists.
The browser exercises in this course run Python for you, so you do not need to install anything. This lesson gets you running a real notebook as well, because that is where you will do your own projects.
Three ways to get a notebook
- JupyterLite (no account, nothing to install). Visit https://jupyter.org/try-jupyter and open a Python notebook. It runs in your browser using the same Pyodide engine as our exercises, and pandas is available.
- Google Colab (free Google account). https://colab.research.google.com gives you a notebook on Google's servers. Files are saved to your Google Drive. The free tier is enough for this course; you never need to pay or add a card.
- Local Jupyter (install). Install Python, then in a terminal run
python -m pip install jupyterlab pandasandjupyter lab. This is what you will use for private or large data.
Cells and the kernel
A notebook is a list of cells:
- Code cells run Python. The value of the last line is displayed automatically, which is why
df.head()on its own shows a table. - Markdown cells hold notes, headings and explanations.
Behind the notebook is a kernel: a running Python process that remembers every variable you have created. Press Shift+Enter to run a cell and move to the next one. The number in [3] next to a cell shows the order cells were run in.
# Cell 1
import pandas as pd
data = {"sample": ["A", "B", "C"], "reading": [4.2, 3.9, 5.1]}
df = pd.DataFrame(data)
df
# Cell 2
df["reading"].mean()
Cell 2 works only because Cell 1 already ran in this kernel.
The big trap: hidden state
Because the kernel remembers everything, you can run cells out of order, edit a cell and rerun it, or delete a cell whose variable still exists in memory. The notebook can then show results that no longer match the code on the page.
Example: you create df, then filter it with df = df[df["reading"] > 4], then run that cell twice while experimenting. Nothing looks wrong, but df has been filtered twice and a later average is different from what a fresh run would give.
The fix is a habit: Restart the kernel and run all (in Jupyter: Kernel > Restart Kernel and Run All Cells; in Colab: Runtime > Restart session and run all) before you trust or share a result. If it runs top to bottom without errors and gives the same numbers, your notebook is reproducible.
A tidy notebook layout
A notebook others can follow usually looks like this:
- A title and a one-paragraph purpose in markdown.
- One cell with all the imports.
- Loading the data (with the file name or URL written down).
- Cleaning steps, each with a short markdown note on why.
- Analysis and charts.
- A short "Findings" markdown cell at the end.
Avoid giant cells. Small cells that each do one thing are easier to rerun and debug.
Useful notebook features
df.head(),df.info()anddf.describe()give a quick look at any table.?after a name shows help:pd.read_csv?.- Tab completion suggests column names and methods.
%timeitmeasures how long a line takes (a "magic" command, notebook only).
Common mistakes
- Trusting a notebook that has never been run top to bottom. Restart and run all first.
- Variables defined in deleted cells. The code is gone but the value is still in memory. A restart exposes this.
- Printing a 100,000-row table. Use
.head()or.sample(5). - Uploading private or personal data to a cloud notebook. Check your organisation's rules first; use local Jupyter for sensitive data.
Quick recap
- A notebook = code cells + markdown cells + a kernel that remembers state.
- Shift+Enter runs a cell; the last expression is displayed.
- Out-of-order runs create hidden state; restart and run all before trusting results.
- JupyterLite and Colab are free ways to start; local Jupyter for private data.
Try it
The Do tab walks you through a short notebook session from a blank page to a restart-and-run-all check.