Jupyter Notebooks Tutorial: A Beginner's Guide
Jupyter Notebooks are an interactive computing environment that allows you to create and share documents containing live code, execution results, visualizations, and narrative text. They're widely used in data science, machine learning, scientific computing, and education because they blend code, outputs, and explanations in one place. Originally developed as part of Project Jupyter (formerly IPython), they support over 40 programming languages, but Python is the most common.This tutorial covers the basics: installation, creating your first notebook, working with cells, and advanced tips. We'll focus on the classic Jupyter interface, but note that tools like Google Colab (cloud-based) or VS Code extensions offer similar experiences without local setup.1. Installation and SetupOption 1: Local Installation (Recommended for Offline Use)Jupyter is easy to install via pip or Anaconda.
- Using pip (if you have Python 3.6+ installed):Then launch it:
pip install notebookThis opens a web browser at http://localhost:8888 with the Jupyter dashboard.jupyter notebook - Using Anaconda (includes Jupyter + 200+ data science packages):
- Download from anaconda.com.
- Install and open Anaconda Navigator.
- Click "Launch" under Jupyter Notebook.
- Google Colab: Free, GPU access. Go to colab.research.google.com, sign in with Google, and create a new notebook.
- Binder: Share notebooks online via mybinder.org.
- JupyterLab (next-gen interface): Install with pip install jupyterlab and run jupyter lab.
- Launch Jupyter: Run jupyter notebook in your terminal. You'll see the file browser dashboard.
- New Notebook: Click "New" > "Python 3" (or your kernel/language).
- Save It: Go to File > Save As, name it my_first_notebook.ipynb (.ipynb is the extension).
- Kernel: The "kernel" is the computation engine. Restart it via Kernel > Restart if things glitch.
- Execute code interactively.
- Example: Click a cell, type:python
print("Hello, Jupyter!") x = 5 y = 10 print(f"The sum is {x + y}") - Run it: Press Shift + Enter (runs and moves to next cell) or Ctrl + Enter (runs in place).
- Output appears below the cell, including prints, plots, or errors.
- For text, headings, lists, equations, and embeds (using Markdown syntax).
- Change cell type: Select cell > Esc (command mode) > Press M for Markdown, Y for code.
- Example Markdown:
# Heading 1 ## Heading 2 - Bullet list - **Bold text** - `inline code` Equation: $E = mc^2$ - Render it: Run the cell (Shift + Enter).
- A: Insert cell above
- B: Insert cell below
- DD: Delete cell
- M/Y: Markdown/Code toggle
- Ctrl + ]: Indent
- Code Cell: Import Librariespython
import numpy as np import matplotlib.pyplot as plt %matplotlib inline # Magic command: Embed plots - Markdown Cell: Introduction
## Simple Plot Demo We'll generate random data and plot it. - Code Cell: Generate Datapython
data = np.random.randn(100) plt.plot(data.cumsum()) plt.title("Cumulative Sum of Random Data") plt.show() - Code Cell: Compute Statspython
mean = np.mean(data) std = np.std(data) print(f"Mean: {mean:.2f}, Std Dev: {std:.2f}")
Feature | Description | How-To |
|---|---|---|
Magic Commands | Special commands starting with % or %% for shortcuts (e.g., %timeit for timing code). | Run %lsmagic to list them. |
Variables Persist | Across cells—great for iterative development. | Check with %who or whos. |
Widgets | Interactive UI elements (sliders, buttons). | Install ipywidgets: pip install ipywidgets; use interact. |
Extensions | Add plugins like table of contents or spellchecker. | Install jupyter_contrib_nbextensions and enable via dashboard. |
Version Control | .ipynb files are JSON—git-friendly. | Use nbstripout to clean outputs for commits. |
- Debugging: Errors show in red; use ?function_name for docs or help() inline.
- Rich Outputs: Supports HTML, images, LaTeX natively.
- Kernels for Other Languages: Install IJulia for Julia, IRkernel for R, etc.
- Share via GitHub: Upload .ipynb to a repo; view rendered on nbviewer.jupyter.org.
- Export: File > Download As > PDF/HTML/Script (Python).
- Colab-Specific: Share links directly; integrate with Drive/GitHub.
- Convert to Slides: Use nbconvert CLI: jupyter nbconvert --to slides my_notebook.ipynb for presentations.
- Kernel Dies: Out of memory? Restart and optimize code.
- Indentation Errors: Python is picky—use spaces, not tabs.
- Slow Startup: Pre-load big libs in first cell.
- Security: Don't run untrusted notebooks (they can execute code).