Python Central HubSign in

Data Analytics with Python / Phase 1: Environment and Setup

Jupyter Shortcuts & Magic Commands

Master Jupyter keyboard shortcuts and magic commands like %timeit, %matplotlib inline, and %%writefile for faster analysis.

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 above
  • B → insert cell below
  • D, D → delete selected cell
  • X → cut cell
  • C → copy cell
  • V → paste cell
  • Z → undo delete
  • M → convert cell to Markdown
  • Y → convert cell to Code
  • Shift + 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.

Python
%time sum(range(10_000_00))

%timeit runs the expression multiple times and gives an average:

Python
%timeit sum(range(1_000_00))

%who and %whos

Show variables currently defined.

Python
%whos

%pwd and %cd

  • %pwd shows current working directory
  • %cd changes it
Python
%pwd
Python
%cd data

%ls (list files)

Python
%ls

Plotting magics

%matplotlib inline

Classic way to render plots inside the notebook.

Python
%matplotlib inline

%matplotlib notebook (interactive)

This can enable interactive plots in the classic notebook.

Python
%matplotlib notebook

Useful cell magics

%%writefile (save code to a file)

You can write a cell’s content into a Python script.

Python
%%writefile my_script.py
print("Hello from a file")

%%bash (run bash commands)

If your Jupyter supports it, you can run bash scripts:

Shell
%%bash
ls -la

If %%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
!python --version
Python
!pip list | head

Getting help quickly

Add ? to functions

Python
import pandas as pd
pd.DataFrame?

Use help()

Python
help(pd.DataFrame)

Visualize it

The % prefix tells IPython how much of the input to treat as a magic command versus plain Python.

diagram Magic command dispatch mermaid
How IPython decides between a line magic, a cell magic, and a shell escape

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

quizCheck yourself
  1. In command mode, which keys delete the selected cell?

    Show answer

    B — D, D — Pressing D twice deletes the cell. X cuts a cell instead, and Z undoes a delete.

  2. What is the difference between % and %% magics?

    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.

  3. Which command would you use to see how long an expression takes, averaged over several runs?

    Show answer

    C — %timeit — %timeit runs the expression many times and reports an average. %time runs it once.

  4. How do you run an OS shell command such as python --version inside a notebook cell?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Time a Function With timeit

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Strip the ! From a Shell Escape

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did