Sub topic 5
Work with cells, modes, and shortcuts
This chapter assumes you have JupyterLab running from a virtual environment, basic Python syntax, and a terminal you are comfortable with. Here you learn the notebook's working model: cells, the two editing modes, and the shortcuts that make notebooks fast to use.
Cells and the kernel
A notebook is an ordered list of cells. Each cell has a type:
- A code cell holds Python. When you run it, the kernel executes the code and the notebook shows the output below the cell.
- A Markdown cell holds formatted text. Running it renders the text. Use it for headings, notes, and explanations.
- A raw cell holds text that is never executed or rendered. You will rarely need it.
The kernel is the process that runs your code. It keeps variables in memory between cells. This is why a variable defined in one cell is available in the next, and also why order matters, which a later chapter covers.
Each code cell shows a label such as [3] on its left. The number is the order in which cells ran in this kernel session. It is not the cell's position on the page. A label of [*] means the cell is still running or queued.
Command mode and edit mode
JupyterLab has two modes, and most shortcut confusion comes from not knowing which one you are in.
| Mode | How to enter | What it looks like | What keys do |
|---|---|---|---|
| Edit mode | Press `Enter`, or click inside a cell | Cursor in the cell, colored left bar | Keys type text |
| Command mode | Press `Esc`, or click outside the text area | No cursor, blue left bar | Keys act on whole cells |
If you press D twice in edit mode, you type "dd" into your code. In command mode, the same keys delete the cell. When a shortcut does something unexpected, check the mode first.
Running cells
Three run shortcuts work in both modes:
Shift+Enterruns the cell and selects the next one. If there is no next cell, it creates one.Ctrl+Enter(Cmd+Returnon macOS in some setups) runs the cell and keeps it selected.Alt+Enterruns the cell and inserts a new cell below.
Try this in a new notebook. Type the code into a cell and press Shift+Enter:
total = 0
for n in range(1, 6):
total += n
totalThe output is 15. Jupyter prints the value of the last expression in a cell automatically, so you do not need print. Only the last expression is shown. Earlier bare expressions are evaluated and discarded.
Essential command-mode shortcuts
Press Esc first, then use these keys:
| Key | Action |
|---|---|
| `A` / `B` | Insert a cell above / below |
| `D`, `D` | Delete the selected cell |
| `M` / `Y` | Change cell to Markdown / code |
| `C`, `X`, `V` | Copy, cut, paste cells |
| `Z` | Undo the last cell operation |
| `Shift+M` | Merge selected cells |
| `0`, `0` | Restart the kernel (confirmation dialog appears) |
You can select several cells with Shift+Up or Shift+Down and then apply an action to all of them.
In edit mode, Ctrl+Shift+- splits the cell at the cursor, and Tab offers code completion. Shift+Tab shows the documentation for the object under the cursor, which is useful for checking the arguments of a pandas function without leaving the notebook.
Common errors
- Typing into the wrong mode. Letters appear in a cell when you meant to run an action, or a cell vanishes after you typed
dd. PressEscbefore using command-mode keys. - Nothing happens after
Shift+Enter. The label shows[*]. The kernel is busy or stuck. Use the Kernel menu to interrupt it, or restart it if needed. - A Markdown cell shows raw text. The cell has not been run, or it is still a code cell. Press
Min command mode, thenShift+Enter. - Accidentally deleted a cell. Press
Zin command mode, or use Edit, then Undo Delete Cells.
Exercise
You press B and a new cell appears below, but you meant to type the letter "b" into a code cell. Which mode were you in, and how do you get to the mode where typing works?
Show answer
You were in command mode, where letter keys act on cells. Press Enter (or click inside the cell) to switch to edit mode, where keys type text.
Exercise
A code cell's label reads [7] but it is the first cell on the page. What does the number mean?
Show answer
It is the execution count. This cell was the seventh cell run in the current kernel session, regardless of where it sits in the notebook.