Jupyter Notebooks
Notebooks are the lab bench of AI engineering. You prototype here, then move what works into production.
Lesson 5 of 13Build~30 minutesPython
Before this: Lesson 1 of Phase 0
On this page
Learning Objectives
- Install and launch JupyterLab, Jupyter Notebook, or VS Code with the Jupyter extension
- Use magic commands (
%timeit,%%time,%matplotlib inline) to benchmark and visualize inline - Distinguish when to use notebooks vs scripts and apply the "explore in notebooks, ship in scripts" workflow
- Identify and avoid common notebook traps: out-of-order execution, hidden state, and memory leaks
The Problem
Every AI paper, tutorial, and Kaggle competition uses Jupyter notebooks. They let you run code in pieces, see outputs inline, mix code with explanations, and iterate fast. If you try to learn AI without notebooks, you're doing math homework without scratch paper.
But notebooks have real traps. People use them for everything, including things they're terrible at. Knowing when to use a notebook and when to use a script will save you from debugging nightmares later.
The Concept
A notebook is a list of cells. Each cell is either code or text.
The kernel is a Python process running in the background. When you run a cell, it sends the code to the kernel, which executes it and sends back the result. All cells share the same kernel, so variables persist between cells.
That "whatever order you click" part is both the superpower and the foot-gun.
s0-cell-order
Build It
Step 1: Pick your interface
Three options, one format:
| Interface | Install | Best for |
|---|---|---|
| JupyterLab | pip install jupyterlab then jupyter lab |
Full IDE experience, multiple tabs, file browser, terminal |
| Jupyter Notebook | pip install notebook then jupyter notebook |
Simple, lightweight, one notebook at a time |
| VS Code | Install "Jupyter" extension | Already in your editor, git integration, debugging |
All three read and write the same .ipynb file. Pick whatever you like. JupyterLab is the most common in AI work.
pip install jupyterlab
jupyter lab
Step 2: Keyboard shortcuts that matter
You operate in two modes. Press Escape for command mode (blue bar on the left), Enter for edit mode (green bar).
Command mode (most used):
| Key | Action |
|---|---|
Shift+Enter |
Run cell, move to next |
A |
Insert cell above |
B |
Insert cell below |
DD |
Delete cell |
M |
Convert to markdown |
Y |
Convert to code |
Z |
Undo cell operation |
Ctrl+Shift+H |
Show all shortcuts |
Edit mode:
| Key | Action |
|---|---|
Tab |
Autocomplete |
Shift+Tab |
Show function signature |
Ctrl+/ |
Toggle comment |
Shift+Enter is the one you'll use a thousand times a day. Learn it first.
Step 3: Cell types
Code cells run Python and show the output:
import numpy as np
data = np.random.randn(1000)
data.mean(), data.std()
Output: (0.0032, 0.9987)
Markdown cells render formatted text. Use them to document what you're doing and why. Supports headers, bold, italic, LaTeX math ($E = mc^2$), tables, and images.
Step 4: Magic commands
These aren't Python. They're Jupyter-specific commands that start with % (line magic) or %% (cell magic).
Time your code:
%timeit np.random.randn(10000)
Output: 45.2 us +/- 1.3 us per loop
%%time
model.fit(X_train, y_train, epochs=10)
Output: Wall time: 2.34 s
%timeit runs the code many times and averages. %%time runs it once. Use %timeit for microbenchmarks, %%time for training runs.
Enable inline plots:
%matplotlib inline
Every plt.plot() or plt.show() now renders directly in the notebook.
Install packages without leaving the notebook:
!pip install scikit-learn
The ! prefix runs any shell command.
Check environment variables:
%env CUDA_VISIBLE_DEVICES
Step 5: Display rich output inline
Notebooks auto-display the last expression in a cell. But you can control it:
import pandas as pd
df = pd.DataFrame({
"model": ["Linear", "Random Forest", "Neural Net"],
"accuracy": [0.72, 0.89, 0.94],
"training_time": [0.1, 2.3, 45.6]
})
df
This renders a formatted HTML table, not a text dump. Same with plots:
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title("Inline Plot")
plt.show()
The plot appears right below the cell. This is why notebooks dominate AI work. You see the data, the plot, and the code together.
For images:
from IPython.display import Image, display
display(Image(filename="architecture.png"))
Step 6: Google Colab
Colab is a free Jupyter notebook in the cloud. It gives you a GPU, pre-installed libraries, and Google Drive integration. No setup required.
- Go to colab.research.google.com
- Upload any
.ipynbfile from this course - Runtime > Change runtime type > T4 GPU (free)
Colab differences from local Jupyter:
- Files don't persist between sessions (save to Drive or download)
- Pre-installed: numpy, pandas, matplotlib, torch, tensorflow, sklearn
from google.colab import filesto upload/download filesfrom google.colab import drive; drive.mount('/content/drive')for persistent storage- Sessions time out after 90 minutes of inactivity (free tier)
Use It
Notebooks vs Scripts: When to use which
| Use notebooks for | Use scripts for |
|---|---|
| Exploring a dataset | Training pipelines |
| Prototyping a model | Reusable utilities |
| Visualizing results | Anything with if __name__ |
| Explaining your work | Code that runs on a schedule |
| Quick experiments | Production code |
| Course exercises | Packages and libraries |
The rule: explore in notebooks, ship in scripts.
A common workflow in AI:
- Explore data in a notebook
- Prototype your model in the notebook
- Once it works, move the code to
.pyfiles - Import those
.pyfiles back into the notebook for further experiments
Common traps
Out-of-order execution. You run cell 5, then cell 2, then cell 7. The notebook works on your machine but breaks when someone runs it top to bottom. Fix: Kernel > Restart & Run All before sharing.
Hidden state. You delete a cell but the variable it created is still in memory. The notebook looks clean but depends on a ghost cell. Fix: Restart the kernel regularly.
Memory leaks. Loading a 4GB dataset, training a model, loading another dataset. Nothing gets freed. Fix: del variable_name and gc.collect(), or restart the kernel.
Ship It
This lesson produces:
outputs/prompt-notebook-helper.mdfor debugging notebook issues
Exercises
- Open JupyterLab, create a notebook, and use
%timeitto compare list comprehension vs numpy for creating an array of 100,000 random numbers - Create a notebook with both markdown and code cells that loads a CSV, displays a dataframe, and plots a chart. Then run Kernel > Restart & Run All to verify it works top to bottom
- Take the code from
code/notebook_tips.py, paste it into a Colab notebook, and run it with a free GPU
Hints
Exercise 1. Start JupyterLab with jupyter lab, then put each version in its own cell that begins with %timeit. Use the same 100,000 size in both cells so that the two times measure the same work.
Exercise 2. Use pd.read_csv for the data, show the dataframe as the last line of a cell, and plot with df.plot() or matplotlib. After Kernel > Restart & Run All, every cell must show a number in its execution counter and no cell may show an error.
Exercise 3. In Colab, select Runtime > Change runtime type > GPU before you paste the code. The run is correct when each function in code/notebook_tips.py prints its section, and torch.cuda.is_available() returns True in a cell.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Kernel | "The thing running my code" | A separate Python process that executes cells and keeps variables in memory |
| Cell | "A code block" | An independently runnable unit in a notebook, either code or markdown |
| Magic command | "Jupyter tricks" | Special commands prefixed with % or %% that control the notebook environment |
.ipynb |
"Notebook file" | A JSON file containing cells, outputs, and metadata. Stands for IPython Notebook |
Further Reading
- JupyterLab Docs for the full feature set
- Google Colab FAQ for Colab-specific limits and features
- 28 Jupyter Notebook Tips for power-user shortcuts
Check yourself
Quick quiz
What is a Jupyter notebook?
- An interactive document that mixes executable code cells with markdown text and inline outputs
- A package manager for Python libraries
- A version control system for data science projects
- A text editor for writing Python scripts
Show the answer
A. An interactive document that mixes executable code cells with markdown text and inline outputs A Jupyter notebook (.ipynb) is an interactive environment where you can write and execute code in cells, see outputs inline, and mix code with formatted text explanations.
What does the Jupyter kernel do?
- Runs the code in cells and keeps variables in memory between executions
- Connects the notebook to GitHub for version control
- Manages file storage for the notebook
- Formats markdown cells into HTML
Show the answer
A. Runs the code in cells and keeps variables in memory between executions The kernel is a separate Python process that executes cell code and maintains state (variables, imports) in memory. All cells in a notebook share the same kernel.
What is the difference between %timeit and %%time magic commands?
- There is no difference, they are aliases
- %timeit runs code many times and averages, %%time runs code once
- %timeit measures wall time, %%time measures CPU time
- %timeit works on single lines, %%time works on the whole notebook
Show the answer
B. %timeit runs code many times and averages, %%time runs code once %timeit runs the statement many times for a statistical average (good for microbenchmarks). %%time runs the cell once and reports wall time (good for training runs).
What is the most common cause of a notebook working on your machine but failing when someone runs it top to bottom?
- Missing the %matplotlib inline magic command
- Using too many markdown cells
- Out-of-order cell execution creating hidden dependencies on execution order
- Using a different Python version
Show the answer
C. Out-of-order cell execution creating hidden dependencies on execution order Running cells out of order creates hidden state dependencies. A variable might exist because you ran cell 5 before cell 2. Kernel > Restart & Run All verifies linear execution.
When should you move code from a notebook into a .py script?
- Never -- notebooks are always better for AI work
- Only when you need to run on a GPU
- As soon as you write more than 10 lines of code
- When the code is a reusable utility, training pipeline, or production code
Show the answer
D. When the code is a reusable utility, training pipeline, or production code The rule is 'explore in notebooks, ship in scripts.' Reusable utilities, training pipelines, and anything running in production should be .py files. Notebooks are for exploration and prototyping.
Questions people ask
Is Jupyter Notebook or Google Colab better for beginners?
Both use the same notebook format. Colab runs in the browser with free, limited GPU time and nothing to install. Local Jupyter or VS Code notebooks give you full control of files and packages. Many people start in Colab and move local later.
Why do my notebook results change when I rerun it?
Cells can run in any order, so hidden state builds up. A variable may come from a cell you edited or deleted. Restart the kernel and run all cells from the top to check that the notebook works in order, and set random seeds for repeatable results.
Lessons from AI Engineering from Scratch by Rohit Ghumare and contributors, MIT license. Notes for owners by Lozo. License