Introduction
Jupyter Notebook is an interactive computing environment that combines code, output, visualizations, and narrative text in a single document. It’s the standard tool for data science, machine learning, and exploratory analysis in Python. This guide covers the essential shortcuts, magic commands, and workflows that make Jupyter genuinely productive.
Installation and Launch
# Install
pip install notebook # classic Jupyter Notebook
pip install jupyterlab # JupyterLab (modern interface, recommended)
# Launch
jupyter notebook # opens at http://localhost:8888
jupyter lab # opens JupyterLab
jupyter notebook --no-browser # start without opening browser
jupyter notebook --port 8889 # use a different port
Keyboard Shortcuts
Jupyter has two modes: Command mode (blue border, press Esc) and Edit mode (green border, press Enter).
Command Mode (Esc)
| Shortcut | Action |
|---|---|
A |
Insert cell above |
B |
Insert cell below |
D, D |
Delete cell |
Z |
Undo cell deletion |
M |
Change cell to Markdown |
Y |
Change cell to Code |
Shift + Up/Down |
Select multiple cells |
Shift + M |
Merge selected cells |
C |
Copy cell |
X |
Cut cell |
V |
Paste cell below |
L |
Toggle line numbers |
O |
Toggle output |
H |
Show all shortcuts |
0, 0 |
Restart kernel |
Edit Mode (Enter)
| Shortcut | Action |
|---|---|
Shift + Enter |
Run cell, select next |
Ctrl + Enter |
Run cell, stay |
Alt + Enter |
Run cell, insert below |
Tab |
Autocomplete |
Shift + Tab |
Show docstring/signature |
Ctrl + / |
Toggle comment |
Ctrl + Z |
Undo |
Ctrl + Shift + - |
Split cell at cursor |
Viewing Documentation
Press Shift + Tab inside function parentheses to see the docstring:
# Place cursor inside the parentheses and press Shift+Tab
import pandas as pd
pd.read_csv( # Shift+Tab here shows the full signature and docs
Or use ? and ??:
pd.read_csv? # show docstring
pd.read_csv?? # show source code
Magic Commands
Magic commands are special IPython commands prefixed with % (line magic) or %% (cell magic).
Timing
# Time a single expression
%time sum(range(1_000_000))
# CPU times: user 23.4 ms, sys: 0 ns, total: 23.4 ms
# Average over multiple runs (more accurate)
%timeit sum(range(1_000_000))
# 23.4 ms ยฑ 312 ยตs per loop (mean ยฑ std. dev. of 7 runs, 10 loops each)
# Time an entire cell
%%timeit
total = 0
for i in range(1_000_000):
total += i
Shell Commands
# Run shell commands with !
!pip install pandas
!ls -la
!git status
!cat requirements.txt
# Capture output into a Python variable
files = !ls *.csv
print(files) # => ['data.csv', 'test.csv']
# Pass Python variables to shell
filename = "data.csv"
!wc -l {filename}
File Operations
# Write cell content to a file
%%writefile my_script.py
def hello(name):
return f"Hello, {name}!"
print(hello("World"))
# Load a file into a cell
%load my_script.py
# Run a Python file
%run my_script.py
%run my_script.py --arg1 value1 # with arguments
Plotting
# Render matplotlib plots inline (in the notebook)
%matplotlib inline
# Higher resolution plots
%config InlineBackend.figure_format = 'retina'
# Interactive plots (requires ipympl)
%matplotlib widget
Profiling
# Profile a function
%prun my_function(data)
# Line-by-line profiling (requires line_profiler)
%load_ext line_profiler
%lprun -f my_function my_function(data)
# Memory profiling (requires memory_profiler)
%load_ext memory_profiler
%memit my_function(data)
Other Useful Magics
# List all variables in scope
%who # brief list
%whos # detailed table with types and values
# Clear all variables
%reset
# Show command history
%history
# Change working directory
%cd /path/to/directory
%pwd # print working directory
# Auto-reload modules (great for development)
%load_ext autoreload
%autoreload 2 # reload all modules before executing code
# Display all output (not just last expression)
%config InteractiveShell.ast_node_interactivity = 'all'
Displaying Rich Output
from IPython.display import display, HTML, Image, Markdown, JSON
# Render HTML
display(HTML("<h2 style='color:blue'>Hello from HTML</h2>"))
# Render Markdown
display(Markdown("## Section\n\n- Item 1\n- Item 2"))
# Display an image
display(Image(url="https://example.com/image.png"))
display(Image(filename="local_image.png", width=400))
# Display JSON interactively
display(JSON({"key": "value", "nested": {"a": 1}}))
# Multiple outputs from one cell
x = 42
y = "hello"
display(x, y) # shows both
Working with DataFrames
import pandas as pd
# Show more rows/columns
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', 50)
pd.set_option('display.float_format', '{:.2f}'.format)
df = pd.read_csv('data.csv')
# Styled display
df.head(10).style.highlight_max(color='lightgreen') \
.highlight_min(color='lightcoral')
# Progress bar for long operations (requires tqdm)
from tqdm.notebook import tqdm
for row in tqdm(df.itertuples(), total=len(df)):
pass
Notebook Extensions and JupyterLab
# Install useful extensions
pip install jupyterlab-git # Git integration
pip install jupyterlab-lsp # Language server (autocomplete, linting)
pip install jupyterlab-code-formatter # Code formatting
pip install ipywidgets # Interactive widgets
# Enable widgets
jupyter nbextension enable --py widgetsnbextension
Interactive Widgets
import ipywidgets as widgets
from IPython.display import display
# Slider
slider = widgets.IntSlider(value=5, min=0, max=20, description='Value:')
display(slider)
# Interactive function
@widgets.interact(x=(0, 10), y=(0, 10))
def multiply(x=5, y=5):
print(f"{x} ร {y} = {x * y}")
Best Practices
Restart and Run All Before Sharing
Hidden state is the biggest Jupyter pitfall โ cells run out of order leave the notebook in an inconsistent state:
# Always verify your notebook runs top-to-bottom cleanly
Kernel โ Restart & Run All
Keep Cells Focused
# Bad: one giant cell
import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
df = df.dropna()
df['new_col'] = df['a'] * df['b']
result = df.groupby('category').mean()
result.plot()
# Good: separate logical steps into cells
# Cell 1: imports
# Cell 2: load data
# Cell 3: clean data
# Cell 4: feature engineering
# Cell 5: analysis
# Cell 6: visualization
Use Markdown for Documentation
## Data Loading
Load the raw dataset from the CSV file. The dataset contains 10,000 rows
with the following columns: `id`, `date`, `value`, `category`.
**Note:** Missing values in `value` are dropped in the next step.
Export Notebooks
# Export to various formats
jupyter nbconvert notebook.ipynb --to html
jupyter nbconvert notebook.ipynb --to pdf
jupyter nbconvert notebook.ipynb --to script # converts to .py
jupyter nbconvert notebook.ipynb --to markdown
Resources
- Jupyter Documentation
- JupyterLab Documentation
- IPython Magic Commands
- 28 Jupyter Notebook Tips, Tricks, and Shortcuts
Comments