Sub topic 5
Work with cells, modes, and shortcuts
This chapter assumes you have JupyterLab running from a project virtual environment, as set up in the previous chapters. You should know basic Python syntax and be able to open a notebook from the JupyterLab file browser. The goal here is a working model of how a notebook is built, how you move through it, and which mistakes cost the most time.
What a notebook is made of
A notebook is a single .ipynb file. Internally it is a JSON document holding an ordered list of cells, plus metadata such as the kernel name. Each cell has a type, its source text, and for code cells, a stored copy of the last output and an execution count.
JupyterLab uses three cell types:
| Cell type | Contents | What running it does |
|---|---|---|
| Code | Python source | Sends the source to the kernel and shows the output below the cell |
| Markdown | Formatted text | Renders the text. Nothing is sent to the kernel |
| Raw | Unformatted text | Nothing. Used by tools such as `nbconvert` that pass the text through |
Most of your work uses code and Markdown cells. Use Markdown cells to state the question, the data source, and what each result means. A notebook with only code cells is hard for anyone else to read, including you in a month.
Running a cell sends text to the kernel
The kernel is the separate Python process that executes your code. The browser tab only displays results. When you run a code cell, JupyterLab sends the cell's text to the kernel. The kernel runs it in a long-lived namespace, which is the set of variables and imports it currently holds. It then sends back the output.
Two consequences follow from this model:
- Variables persist between cells. If one cell sets
df = pd.read_csv(...), any later cell can usedf. - The order you run cells in matters, not the order they appear on the page. This is the root of the hidden-state problem covered in a later chapter.
The number in the [ ] brackets left of a code cell is the execution count. It tells you the order in which the kernel ran things. [*] means the cell is currently running or queued.
Two modes: edit and command
Keyboard shortcuts depend on the mode, and confusing the two is the most common beginner error.
- Edit mode means the cursor is inside a cell and you are typing. The cell border is blue in the default theme and the editor shows a text cursor.
- Command mode means the cell itself is selected, but you are not typing in it. Keys act on cells, not text.
Switch modes with these keys:
- Press
Escin edit mode to enter command mode. - Press
Enterin command mode to enter edit mode on the selected cell.
If you press d twice while you think you are in command mode but are actually in edit mode, you type "dd" into your code. If you press Esc, then d, d while in command mode, you delete the cell. Check the mode before you press letter keys.
Running cells
Three run shortcuts work in both modes and differ only in where the selection goes afterward:
| Shortcut | Action |
|---|---|
| `Shift+Enter` | Run the cell and select the next one. If there is no next cell, create one |
| `Ctrl+Enter` | Run the cell and keep it selected |
| `Alt+Enter` | Run the cell and insert a new cell below |
On macOS, Ctrl in these shortcuts stays Ctrl, and Alt is the Option key. Use Shift+Enter for normal top-to-bottom work, and Ctrl+Enter when you are tweaking one cell repeatedly, for example adjusting a plot.
The Run menu also offers "Run All Cells" and "Restart Kernel and Run All Cells". You will use the second one often in later chapters to check that a notebook runs cleanly from the top.
Command mode shortcuts worth learning
You do not need all the shortcuts. These cover most daily editing, and all of them are pressed in command mode:
| Key | Action |
|---|---|
| `a` | Insert a cell above |
| `b` | Insert a cell below |
| `dd` | Delete the selected cell |
| `m` | Change the cell to Markdown |
| `y` | Change the cell to code |
| `c`, `x`, `v` | Copy, cut, and paste cells |
| `z` | Undo the last cell operation |
| `Shift+M` | Merge the selected cells |
| `Up`, `Down` or `k`, `j` | Move the selection |
| `00` | Restart the kernel after a confirmation prompt (press `0` twice) |
| `Shift+Up`, `Shift+Down` | Extend the selection to several cells |
Select several cells with Shift+Up or Shift+Down, then apply an action such as dd or m to all of them at once.
To see the full list, open the Command Palette with Ctrl+Shift+C (or Cmd+Shift+C on macOS) and type a few letters of what you want. The palette also lists the current shortcut next to each command, which is more reliable than memorizing tables, because shortcuts can differ between versions and can be changed in Settings.
Useful edit mode shortcuts
While typing code, a few shortcuts save the most time:
Tabtriggers code completion, for example after typingdf.to see the DataFrame methods.Shift+Tabshows the docstring (the built-in documentation) for the function under the cursor. Inside the parentheses of a call such aspd.read_csv(, it shows the accepted arguments.Ctrl+/(orCmd+/) toggles comments on the selected lines.Ctrl+Shift+-splits the cell at the cursor into two cells.
Editing Markdown cells
Double-click a Markdown cell, or select it and press Enter, to edit it. Run it with Shift+Enter to render it. A rendered cell shows headings, lists, and links. Unrendered Markdown cells are a common source of confusion: if you see raw # symbols, the cell has not been run.
A minimal structure for an analysis notebook looks like this:
# Monthly sales analysis
## Question
Which regions grew fastest between January and June?
## Data
`data/sales.csv`, exported from the billing system on 2026-11-30.Placing a title, the question, and the data source at the top gives the reader the context before any code runs.
Output behaviour and edge cases
Several details about outputs surprise people the first time.
Only the last expression is displayed. If a cell ends with an expression, the kernel shows its value. Earlier expressions in the same cell are evaluated but not displayed:
import pandas as pd
df = pd.DataFrame({"region": ["N", "S"], "revenue": [120, 95]})
df.head() # evaluated, but not displayed
df.describe() # displayed, because it is the last lineTo show several results from one cell, use print() or display():
from IPython.display import display
display(df.head())
display(df.describe())A trailing semicolon hides the output. In a cell ending with plt.plot(x, y); the semicolon suppresses the printed return value of the call, such as a list of line objects.
Long outputs scroll or collapse. Right-click an output and choose "Enable Scrolling for Outputs" to keep a long table from filling the page. You can also click the blue bar left of an output to collapse it.
Interrupting versus restarting. If a cell runs too long, use the stop button in the toolbar (or press i twice in command mode) to interrupt it. This raises a KeyboardInterrupt in the kernel and keeps your variables. If the kernel does not respond, restart it. A restart clears every variable, so you must rerun the cells that create them.
Moving and organizing cells
Drag a cell by its left margin to reorder it, or select it and use the cell-moving commands in the Edit menu. In the sidebar, the Table of Contents panel builds an outline from your Markdown headings, which makes long notebooks easier to navigate. This is another reason to use real headings rather than bold text.
Keep each code cell small and focused: load data in one, clean it in another, plot in a third. Small cells make errors easier to locate. They also make it easier to rerun only the part you changed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Letter keys type text instead of acting on cells | You are in edit mode | Press `Esc`, then the key |
| `NameError` for a variable you defined | The defining cell has not run, or the kernel restarted | Run the defining cell, or use Run All Cells |
| A cell shows `[*]` forever | Long computation, or the kernel is stuck | Interrupt, then restart the kernel if needed |
| Shortcut does nothing | Focus is in another panel, or it is remapped | Click inside the notebook, then check the Command Palette |
| Markdown shows raw symbols | The cell was never rendered | Press `Shift+Enter` |
Exercise
You are in command mode with three cells selected using Shift+Down. You want all three turned into Markdown cells and then the notebook run from the top in a fresh kernel. Which keys and menu command do you use?
Show answer
Press m to convert all selected cells to Markdown. Then choose "Restart Kernel and Run All Cells" from the Kernel menu (or the Command Palette). Running from a fresh kernel confirms that the notebook does not depend on variables left over from earlier runs.
Exercise
A cell contains df.head() on line 1 and df.describe() on line 2, but only the summary statistics appear. Why, and how do you show both?
Show answer
Jupyter displays only the value of the last expression in a cell. Wrap both in display() (imported from IPython.display) or call print() on each so that both results appear.