Getting started / Introduction to Jupyter notebooks
Introduction
If you’re new to data science and machine learning, you’ve probably heard of Jupyter notebooks. But what are they exactly, and why are they so popular in the field?
Jupyter notebooks are interactive web-based documents that allow you to combine code, text, and visualizations in a single place. They are commonly used in data science, machine learning, and scientific computing for prototyping, exploration, and communication.
One of the main benefits of Jupyter notebooks is that they allow you to write and run code in a flexible and interactive way. You can mix code blocks with text, equations, and visualizations, and you can run the code blocks one at a time or all at once. This makes it easy to test and debug code, as well as to document and share your work.
Creating and Running a Jupyter Notebook
Now that you have Jupyter notebooks installed, it’s time to create your first notebook! Here’s how:
- Open the Anaconda Navigator and click on the “Launch” button next to Jupyter notebooks. This will open a new window in your web browser with the Jupyter notebook interface. In the Jupyter notebook interface, click on the “New” button and select “Python 3” from the dropdown menu. This will create a new notebook with a default name, such as “Untitled.ipynb”.
- To rename the notebook, click on the notebook name and type a new name. The notebook name should end with “.ipynb” (short for “IPython notebook”).
- To write and run code in a Jupyter notebook, you’ll need to create a code cell. To do this, click on the “Insert” menu and select “Insert Cell Below” (or use the keyboard shortcut Shift+Enter).
- A code cell will appear below the current cell. You can type Python code into the cell and run it by clicking on the “Run” button or using the keyboard shortcut Shift+Enter.
- Jupyter notebooks allow you to mix code, text, and visualizations in the same document. To add text to a Jupyter notebook, you’ll need to create a markdown cell.
Creating Markdown cells
- To create a markdown cell, click on the “Insert” menu and select “Insert Cell Below” (or use the keyboard shortcut Shift+Enter).
- A markdown cell will appear below the current cell. You can type markdown syntax into the cell, such as text formatting, links, or images.
- To render the markdown, click on the “Run” button or use the keyboard shortcut
Shift+Enter
to run current cell and create a new one below. Or justControl+Enter
to run the current cell. The markdown will be converted to HTML and displayed as formatted text. Here’s an example of some basic markdown syntax:
Headings
# Heading 1
## Heading 2
### Heading 3
- Bulleted list
- Bulleted list
1. Numbered list
2. Numbered list
**Bold text**
_Italic text_
Creating a link
[Link text](https://www.example.com)
Displaying an Image
![Image alt text](image.png)
It’s important to make use of Markdown cells to comment and explain code, along with keeping it organised.
Edit Mode and Command Mode
In Jupyter notebooks, there are two modes that you can be in: edit mode and command mode.
Edit Mode
Edit mode is the mode you are in when you are typing code or text into a cell. When you are in edit mode, the cell is highlighted with a green border and you can edit the contents of the cell. You can enter edit mode by double-clicking on a cell or by pressing Enter when the cell is selected.
Command Mode
Command mode is the mode you are in when you are not editing the contents of a cell. When you are in command mode, the cell is highlighted with a blue border and you can use keyboard shortcuts to perform actions on the cell or the notebook. You can enter command mode by pressing Esc or by clicking outside the cell.
It might be slightly confusing at first, but once you get used to it, it’s really intuitive to work with.
Command Mode Shortcuts
Command | Description |
---|---|
dd | Delete the current cell |
Shift+M | Merge the current cell with the cell below |
Shift+Up | Move the current cell up |
Shift+Down | Move the current cell down |
Shift+J | Select the cell below |
Shift+K | Select the cell above |
Shift+Ctrl+A | Insert a new cell above the current cell |
Shift+Ctrl+B | Insert a new cell below the current cell |
Common Shortcuts
Shortcut | Description |
---|---|
Shift+Enter | Run the current cell and move to the next cell |
Ctrl+Enter | Run the current cell and stay in the current cell |
Alt+Enter | Run the current cell and insert a new cell below |
Ctrl+S | Save the current notebook |
Ctrl+Z | Undo the last action |
Ctrl+Y | Redo the last undone action |
Ctrl+A | Select all cells |
Ctrl+Shift+ - | Split the current cell at the cursor position |
Ctrl+Shift+P | Open the command palette |
Esc | Enter command mode (you can navigate the notebook with the arrow keys and enter edit mode with Enter ) |
Enter | Enter edit mode (you can edit the contents of the cell) |
Tab | Autocomplete or indent |
Shift+Tab | Show the documentation for the current object |
Shift+Ctrl+P | Open the command palette (in command mode) |
Shift+Ctrl+I | Interrupt the kernel (in command mode) |
Shift+Ctrl+0 | Restart the kernel (in command mode) |
Conclusion
Although all of the shortcuts above may seem intimidating, you can navigate using the UI, insert buttons while you are still learning. The more time you spend writing code in Jupyter notebooks, the more you will adapt to using them