Jupyter notebooks combine explanatory text, mathematical notation, executable code, and output in one document. A notebook is useful only when its code can be rerun from top to bottom and produce the intended results.
Choose a way to run the notebooks¶
You may use a course-provided JupyterHub or another hosted notebook service. To work locally, install Python 3.11 or newer and create an isolated environment:
python -m venv .venvActivate it on Windows:
.\.venv\Scripts\Activate.ps1Activate it on macOS or Linux:
source .venv/bin/activateThen install the course packages and start Jupyter:
python -m pip install -r requirements.txt
jupyter labCells and the kernel¶
A notebook has two cell types that we use often:
Markdown cells contain prose, headings, links, and mathematics.
Code cells send Python instructions to the kernel, the process that stores variables and performs calculations.
Run the next cell. The value below the cell is its output.
course = "Linear Algebra"
chapter = 0
f"{course}, Chapter {chapter}"'Linear Algebra, Chapter 0'The kernel remembers values created by earlier cells:
chapter + 11This memory can cause a common notebook error: code may appear to work only because an old variable remains in the kernel. Before submitting work, use Restart Kernel and Run All Cells. If the notebook fails, fix the first failing cell and run all cells again.
Errors are information¶
Read a traceback from its final line upward. The final line names the error and usually gives the most useful clue.
For example, this code would produce a NameError because chaptr was never defined:
chaptr + 1Useful first checks are:
Is every name spelled consistently?
Did the cell that defines the name run?
Are parentheses and brackets balanced?
Does the object have the shape and type you expected?
Imports¶
An import makes a package available. We use standard aliases so that course code is easy to read.
import numpy as np
import matplotlib.pyplot as pltImport packages near the beginning of a notebook. Avoid from numpy import *; it hides where names come from and can overwrite other names.
Markdown and mathematics¶
Use Markdown cells to explain the goal, method, and conclusion of a calculation. Write inline mathematics between dollar signs, such as $Ax=b$, and displayed mathematics between double dollar signs:
$$
A =
\begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}.
$$This renders as
Quick check¶
What is the difference between a code cell and a Markdown cell?
Why should you restart the kernel and run all cells before submitting?
What does
import numpy as npmakenpmean?
Check your answers
A code cell is executed by the Python kernel; a Markdown cell presents formatted text and mathematics.
Restarting removes hidden state and confirms that the notebook works in order from a clean session.
npis the conventional local alias for the importednumpypackage.