Why shortcuts and magics matter
In data analytics, you often:
- Rerun code many times
- Explore different plots and transformations
- Measure performance
- Run shell commands
Jupyter shortcuts and magic commands help you iterate much faster.
Keyboard shortcuts (classic Jupyter Notebook)
Jupyter has two main modes:
- Command mode (press
Esc): navigate and manage cells - Edit mode (press
Enter): edit inside a cell
Most useful shortcuts (Command mode)
A→ insert cell aboveB→ insert cell belowD, D→ delete selected cellX→ cut cellC→ copy cellV→ paste cellZ→ undo deleteM→ convert cell to MarkdownY→ convert cell to CodeShift + Up/Down→ select multiple cells
Most useful shortcuts (Edit mode)
Ctrl + Enter→ run cell (stay)Shift + Enter→ run cell (move down)Alt + Enter→ run cell (insert new cell below)Tab→ autocomplete (when available)Ctrl + /→ toggle comment in selection
What are magic commands?
Magic commands are special Jupyter commands that start with:
%for line magics%%for cell magics
They are not standard Python syntax; they’re handled by IPython/Jupyter.
Common line magics
%time and %timeit
Use these to measure execution time.
%time sum(range(10_000_00))%timeit runs the expression multiple times and gives an average:
%timeit sum(range(1_000_00))%who and %whos
Show variables currently defined.
%whos%pwd and %cd
%pwdshows current working directory%cdchanges it
%pwd%cd data%ls (list files)
%lsPlotting magics
%matplotlib inline
Classic way to render plots inside the notebook.
%matplotlib inline%matplotlib notebook (interactive)
This can enable interactive plots in the classic notebook.
%matplotlib notebookUseful cell magics
%%writefile (save code to a file)
You can write a cell’s content into a Python script.
%%writefile my_script.py
print("Hello from a file")%%bash (run bash commands)
If your Jupyter supports it, you can run bash scripts:
%%bash
ls -laIf %%bash isn’t available, you can use ! shell escapes instead.
Shell escapes with !
Prefix a command with ! to run it in the OS shell.
!python --version!pip list | headGetting help quickly
Add ? to functions
import pandas as pd
pd.DataFrame?Use help()
help(pd.DataFrame)Visualize it
The % prefix tells IPython how much of the input to treat as a magic command versus plain Python.
flowchart TD
A["Line typed in a cell"] --> B{"Starts with?"}
B -->|"%%"| C["Cell magic: applies to the WHOLE cell"]
B -->|"%"| D["Line magic: applies to ONE line"]
B -->|"!"| E["Shell escape: runs in the OS shell"]
B -->|"nothing special"| F["Normal Python code"]
Common mistakes
Mistake 1: Using magics in .py files
Magics only work in Jupyter/IPython, not in normal Python scripts.
Mistake 2: Forgetting to rerun earlier cells
Notebook state is incremental. If you restart the kernel, rerun setup cells.
Check your understanding
In command mode, which keys delete the selected cell?
Pressing D twice deletes the cell. X cuts a cell instead, and Z undoes a delete.
Show answer
B — D, D — Pressing D twice deletes the cell. X cuts a cell instead, and Z undoes a delete.
What is the difference between % and %% magics?
Line magics start with % and affect one line. Cell magics start with %% and apply to the entire cell, like %%writefile.
Show answer
B — % applies to one line and %% applies to the whole cell — Line magics start with % and affect one line. Cell magics start with %% and apply to the entire cell, like %%writefile.
Which command would you use to see how long an expression takes, averaged over several runs?
%timeit runs the expression many times and reports an average. %time runs it once.
Show answer
C — %timeit — %timeit runs the expression many times and reports an average. %time runs it once.
How do you run an OS shell command such as python --version inside a notebook cell?
The ! prefix is the shell escape. Adding ? after a name shows its documentation instead.
Show answer
A — Prefix it with ! — The ! prefix is the shell escape. Adding ? after a name shows its documentation instead.
Next
Continue to: Google Colab Walkthrough to learn cloud notebooks and GPU/TPU options.
🧪 Try It Yourself
Exercise 1 – Classify a Magic Command
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Time a Function With timeit
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Strip the ! From a Shell Escape
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.