Jupyter Notebooks

Notebooks let you write code, see output, and take notes in one interactive document.

Most data science happens in Jupyter notebooks. A notebook is a document made of cells that you run one at a time.

Two kinds of cell

  • Code cells run Python and show the result right below them.
  • Markdown cells hold formatted notes, headings and explanations.

Why notebooks fit AI work

You explore data step by step — load it, peek at it, plot it, tweak, repeat. The last expression in a cell is displayed automatically, so you constantly see what your data looks like.

Example

Example · python
# In a notebook the last line is auto-displayed
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
df.head()   # shows the table without needing print()

When to use it

  • A data scientist uses a Jupyter notebook to iteratively explore a new dataset, running one cell at a time and immediately seeing tabular and chart output.
  • An ML instructor shares a notebook with students so they can read explanations in markdown cells and run model code in code cells without switching tools.
  • A research team stores experiments as versioned notebooks, making it easy to reproduce any prior result by re-running cells top to bottom.

More examples

Install and launch Jupyter

Installs Jupyter and starts the notebook server, opening a browser interface where you can create and run notebooks.

Example · bash
pip install jupyter
jupyter notebook

Typical notebook cell sequence

Shows three sequential notebook cells: imports, data load with a preview, and a bar chart, mirroring the step-by-step exploration style notebooks encourage.

Example · python
# Cell 1 - imports
import pandas as pd
import matplotlib.pyplot as plt

# Cell 2 - load
df = pd.read_csv('sales.csv')
df.head()

# Cell 3 - plot
df['revenue'].plot(kind='bar')
plt.title('Monthly Revenue')
plt.show()

Inline magic commands

Uses the %matplotlib inline magic so plots render directly in the notebook output area without a separate window.

Example · python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.title('Sine wave')
plt.show()

Discussion

  • Be the first to comment on this lesson.