Friday, October 17, 2025

Jupyter Notebooks Tutorial: A Beginner's Guide

 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):
    pip install notebook
    Then launch it:
    jupyter notebook
    This opens a web browser at http://localhost:8888 with the Jupyter dashboard.
  • Using Anaconda (includes Jupyter + 200+ data science packages):
    1. Download from anaconda.com.
    2. Install and open Anaconda Navigator.
    3. Click "Launch" under Jupyter Notebook.
Option 2: Cloud-Based (No Install Needed)
  • 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.
Pro Tip: For Windows/Mac/Linux, use virtual environments (e.g., venv or conda) to avoid package conflicts.2. Creating Your First Notebook
  1. Launch Jupyter: Run jupyter notebook in your terminal. You'll see the file browser dashboard.
  2. New Notebook: Click "New" > "Python 3" (or your kernel/language).
  3. Save It: Go to File > Save As, name it my_first_notebook.ipynb (.ipynb is the extension).
  4. Kernel: The "kernel" is the computation engine. Restart it via Kernel > Restart if things glitch.
Your notebook opens with an empty cell—the building block of notebooks.3. Working with CellsCells are where the magic happens. There are two main types:Code Cells
  • 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.
Markdown Cells
  • 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).
Cell Shortcuts (in Command Mode: hit Esc first):
  • A: Insert cell above
  • B: Insert cell below
  • DD: Delete cell
  • M/Y: Markdown/Code toggle
  • Ctrl + ]: Indent
4. Basic Workflow ExampleLet's build a simple data analysis notebook. Copy these into sequential cells:
  1. Code Cell: Import Libraries
    python
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline  # Magic command: Embed plots
  2. Markdown Cell: Introduction
    ## Simple Plot Demo
    We'll generate random data and plot it.
  3. Code Cell: Generate Data
    python
    data = np.random.randn(100)
    plt.plot(data.cumsum())
    plt.title("Cumulative Sum of Random Data")
    plt.show()
  4. Code Cell: Compute Stats
    python
    mean = np.mean(data)
    std = np.std(data)
    print(f"Mean: {mean:.2f}, Std Dev: {std:.2f}")
Run all cells: Kernel > Restart & Run All. Outputs persist, making notebooks reproducible.5. Key Features and Tips
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.
6. Sharing and Exporting
  • 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.
Common Pitfalls and Fixes
  • 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).
For more depth, check the official docs at jupyter.org or try the interactive tutorial in a new notebook with !pip install jupyterlab-tutorial followed by jupyter lab --notebook-dir=. and opening the tutorial.If you have a specific aspect (e.g., JupyterLab vs. Classic, ML workflows, or troubleshooting), let me know for a deeper dive! Ready to try? Open a notebook and experiment

Lecture Notes: Optimising Numerical Code

Lecture Notes: Optimising Numerical Code Prerequisites: Basic Python programming Understanding of NumPy arrays and vector ...