Notebook Basics Beginner

Master the fundamentals of Jupyter Notebook: cell types, running cells, keyboard shortcuts, Markdown formatting, magic commands, and understanding cell execution order.

Cell Types

  • Code cells: Execute Python (or other kernel language) code. Results appear below the cell. Indicated by In [n]: prompt.
  • Markdown cells: Write formatted text using Markdown syntax. Double-click to edit, Shift+Enter to render.
  • Raw cells: Plain text that is not executed or formatted. Useful for nbconvert workflows.

Running Cells

  • Shift+Enter: Run the cell and move to the next cell (create one if at the end)
  • Ctrl+Enter: Run the cell and stay on it
  • Alt+Enter: Run the cell and insert a new cell below
  • Cell → Run All: Run all cells from top to bottom
  • Cell → Run All Above: Run all cells above the current cell

Keyboard Shortcuts

Jupyter has two modes: Command mode (press Esc, blue cell border) and Edit mode (press Enter, green cell border).

ShortcutModeAction
Shift+EnterBothRun cell, select below
Ctrl+EnterBothRun cell
Alt+EnterBothRun cell, insert below
ACommandInsert cell above
BCommandInsert cell below
DDCommandDelete cell (press D twice)
MCommandChange to Markdown cell
YCommandChange to code cell
ZCommandUndo cell deletion
LCommandToggle line numbers
Shift+MCommandMerge selected cells
Ctrl+SBothSave notebook
HCommandShow keyboard shortcuts
TabEditCode completion / indent
Shift+TabEditShow tooltip / docstring
Ctrl+/EditComment / uncomment

Markdown Formatting in Cells

# Heading 1
## Heading 2
### Heading 3

**Bold**, *italic*, ~~strikethrough~~, `inline code`

- Unordered list item
  - Nested item

1. Ordered list item
2. Second item

[Link text](https://jupyter.org)
![Image alt text](image.png)

> Blockquote

---

| Column A | Column B |
|----------|----------|
| Cell 1   | Cell 2   |

Math (LaTeX): $E = mc^2$

$$\sum_{i=1}^{n} x_i = x_1 + x_2 + \cdots + x_n$$

Code Output Types

Code cells can produce different types of output:

  • Text output: print() statements and return values
  • Rich display: DataFrames render as HTML tables, images display inline
  • Plots: Matplotlib, seaborn, and plotly figures render inline
  • HTML: Display arbitrary HTML with IPython.display.HTML()
  • LaTeX: Render math equations with IPython.display.Math()
  • Errors: Tracebacks display with syntax highlighting

Magic Commands

Magic commands are special commands that extend Python's capabilities:

# Line magics (% prefix)
%matplotlib inline       # Enable inline matplotlib plots
%timeit sum(range(1000)) # Time a single expression
%who                     # List all defined variables
%whos                    # List variables with details
%pwd                     # Print working directory
%cd /path/to/dir         # Change directory
%load script.py          # Load file contents into a cell
%run script.py           # Run a Python script
%env VAR=value           # Set environment variable
%load_ext autoreload     # Load an extension

# Cell magics (%% prefix)
%%timeit                 # Time the entire cell
result = sum(range(1000))

%%bash                   # Run bash commands
echo "Hello from bash"
ls -la

%%html                   # Render HTML
<h2 style="color:blue">Blue heading</h2>

%%writefile myfile.py    # Write cell to file
def hello():
    print("Hello!")

Shell Commands

# Use ! to run shell commands
!ls -la
!pip install some-package
!cat myfile.txt

# Capture output in a variable
files = !ls *.csv
print(files)

Tab Completion and Help

# Tab completion
import pandas as pd
pd.read_  # Press Tab to see all methods starting with "read_"

# Quick help with ?
pd.read_csv?   # Shows docstring
pd.read_csv??  # Shows source code

# Help function
help(pd.read_csv)

Cell Execution Order Matters

💡
Important: Cells can be run in any order, but variables reflect the most recent execution. The In [n]: number shows execution order. Running cells out of order is a common source of bugs. Always verify your notebook runs correctly from top to bottom using Cell → Run All.