Introduction to Jupyter Notebooks
Jupyter Notebook combines executable cells, their outputs, and Markdown narrative. A notebook's visible cell order is not proof of execution order, so reproducibility requires deliberate kernel and execution practices.
Install and launch
Create and activate a virtual environment, then install the classic Notebook interface:
python -m pip install notebook
jupyter notebook
JupyterLab is a separate interface installed with python -m pip install jupyterlab and launched with jupyter lab.
Confirm the kernel
A notebook executes through its selected kernel. In a code cell, inspect the interpreter:
import sys
print(sys.executable)
print(sys.version.split()[0])
The path should identify the intended environment. Installing a package into a terminal's Python does not guarantee that a different notebook kernel can import it.
Cells and execution state
Shift+Enterruns a cell and moves to the next one.Ctrl+Enterruns a cell and stays on it.- Markdown cells hold explanations, headings, equations, and links.
- Code cells share kernel state until the kernel restarts.
This example is deterministic when run after a fresh kernel start:
values = [2, 4, 8]
mean = sum(values) / len(values)
print(mean) # 4.666666666666667
Before sharing a notebook, restart the kernel and run all cells from top to bottom. Remove secrets and unnecessary large outputs, keep data-loading assumptions visible, and record dependency versions outside the notebook in the project environment declaration.
Notebook trust
Notebook files may contain stored HTML, JavaScript-capable output, and code. Open untrusted notebooks cautiously, inspect code before running it, and do not treat a rendered output as evidence that you executed the displayed source yourself.