Working Method with Notebooks
With notebooks, you work on computational problems in pieces or cells that you can run independently. All variables declared and functions defined in an already run cell are kept in memory so that subsequent cells can use them. Taking the approach of working on problems in pieces or cells enables you to run sections of code separately without breakpoints, because each cell is run independently and shows its output. This way, you can edit and run each cell as many times as you want until you get the expected output.
To clear the output generated by a previous execution of a notebook cells, you must restart the associated kernel with the button in the toolbar.
Edit Mode and Command Mode
Notebooks have two different modes:
-
In edit mode, the analyst can edit or run the content of the selected cell identified by a purple border.
-
In command mode, the analyst can add cells, delete them, or change their order.
-
To enter command mode, press ESC or click anywhere outside the cell.
-
To enter edit mode, click the text box of the cell you want to edit, or use the arrow keys to navigate to the cell and press
Enter
. -
When you click an icon in the notebook toolbar, you enter command mode automatically.
Select a Cell in the Notebook
-
To select a cell in command mode: Use the arrow keys. You can also use
k
andj
. -
To select a cell in edit mode: Click the cell text box.
-
To select multiple consecutive cells in command mode: Use the
Shift + Arrow
keys, orShift + k
andShift + j
, or click the first cell, pressShift
and click the last cell.
Add a Cell to the Notebook
Select a cell. In the notebook toolbar, click the icon. A new code type cell is created below the selected cell.
To control where the cell is inserted:
-
To create a cell above the selected cell: In command mode, press
A
or click theicon.
-
To create a cell below the selected cell: In command mode, press
B
or click theicon.
Change the Cell Type of a Notebook Cell
Select one or multiple cells. In the notebook toolbar, click the drop-down menu.
To change the cell type using the keyboard, select the cell in command mode and press these keys:
-
To change the cell to code:
Y
. -
To change the cell to Markdown:
M
.
Delete a Notebook Cell
In command mode, use the arrow keys to select the cell you want to delete. Press the d
key twice. The cell is deleted, along with its output, if any.
You can also delete a cell by clicking the cell icon.
Run a Notebook Cell
Select a cell. In the notebook toolbar, click the icon. The cell runs and shows its output.
To run a cell using the keyboard:
-
To run a cell with the keyboard: Select the cell and press
Ctrl + Enter
. -
To run a cell with the keyboard and move to the next cell: Press
Shift + Enter
. This is useful if you want to run a series of consecutive cells individually without needing to select them independently. -
To run a cell with the keyboard and insert a new cell immediately below it: Press
Alt + Enter
. This is useful if you want to quickly add new code or notes to your notebook.
Run all Notebook Cells
In the notebook toolbar, click the icon. The notebook shows each cell output below the cell. The cell code is not hidden.
Run All Notebook Cells in Presentation Mode
In the notebook toolbar, click the icon. The notebook enters presentation mode, hiding code sections.
Sort the Notebook Cells
-
Select a cell. In the toolbar, click the
and
icons.
-
Click the cell
and
icons.
-
Click the sequence number for the cell you want to move. Drag it to a new position.
Save a Notebook
-
In the notebook toolbar, click the
icon.
-
In command mode, press
Ctrl + s
.
Stop and Restart Kernel Execution
To clear cell output, or if the kernel gets stuck in an infinite loop, we recommend that you restart the kernel.
-
To restart the kernel: In the notebook toolbar, click the
icon.
-
To interrupt the kernel in command mode: Press i and i.
-
To restart the kernel in command mode: Press 0 and 0.
Copy, Cut, and Paste Cells
-
To copy one or more cells using the toolbar: Select the cells. Click
.
-
To copy one or more cells using the keyboard: Select the cells in command mode. Press c.
-
To cut one or more cells using the toolbar: Select the cells. Click
.
-
To cut one or more cells using the keyboard: Select them in command mode. Press x.
-
To paste one or more cells below an existing cell using the toolbar: Select the cell. Click
.
-
To paste one or more cells above an existing cell using the keyboard: Select the cell in command mode. Press Shift + v.
-
To paste one or more cells below an existing cell using the keyboard: Select the cell in command mode. Press v.
-
To duplicate a cell: Click the cell
icon. A copy of the cell is created below it.
Merge Cells
-
To merge two consecutive cells of the same type in command mode, press Shift + m.
Enable Code Autocomplete and Get Help
-
To get completion suggestions for your code as you type: Press Tab.
-
To call help for a function: Press Shift + Tab.
Move Cursor Within a Cell
-
To move the cursor to the start of the cell: Press Ctrl + Home.
-
To move the cursor to the end of the cell: Press Ctrl + End.
-
To move the cursor one word right: Press Ctrl + Right.
-
To move the cursor one word left: Press Ctrl + Left.
Other Operations
-
To hide code using the keyboard in command mode: Press O.
-
To hide code using the toolbar: Click
.
-
To show or hide line numbers within cells: In command mode, press l.
-
To show a list of keyboard shortcuts: In command mode, press h.
-
To merge two consecutive cells of the same type: In command mode, press Shift + m.