Jupyter Notebooks
Jupyter notebooks run bits of code interactively and are great environments for learning to program with Python. They can run as a web-application inside a web-browser, inside an integrated development environment such as VS Code, or even in the cloud with tools like mybinder.org. To get the full functionality of jupyter notebooks you must have Python and Jupyter Notebooks installed on your computer. The easiest way to do this is to install Anaconda, which includes both Python and Jupyter. If you haven’t installed Anaconda yet, see the Anaconda installation instructions.
Editors
Jupyter Notebook Web Application
Once jupyter is installed, open up the Anaconda Prompt (or other terminal application) and type the command
jupyter notebook
The jupyter notebook interface should open up in your web-browser. You can use the New
button to create a new notebook, the Upload
button to open an existing notebook, and the file directory to navigate your file system. Check the Jupyter documentation for more details.
VS Code
With the Python extension for VS Code you can create, open, and edit jupyter notebook .ipynb
files. VS Code takes a bit longer to load files and is missing some features of the web application but also adds auto-complete, debugging, and other features that are not available in the web-based interface.
Document Structure
Jupyter notebooks combine text and code in two different kinds of cells, markdown (text) cells and code cells.
Markdown Cells
In markdown cells, you can write plain text, GitHub Flavored Markdown, or even HTML. You can add headings, lists, links, images, code snippets, and many other types of elements.
Code Cells
Below is a code cell. You can run the code by clicking on the cell and using the keyboard command ctrl + enter
.
[4]:
# print() is a Python function for outputing text
print("this is a code cell")
this is a code cell
Command Mode vs. Edit Mode
Cells can be in one of two states, command mode or edit mode.
To enter command mode, you can click on the side bar to the left side of the cell. In command mode, we can create, delete, copy, paste, or change the type of cells.
To enter edit mode you can click inside the cell. In edit mode, we edit and execute a cell’s content (either markdown or Python code).
You can also use the esc
and enter
keys to switch between command and edit modes.
Some Useful Keyboard Shortcuts
Note
All fo the following shortcuts are supported by the jupyter notebook web-application but may not be supported by other editors e.g. VS Code
Command Mode Shortcuts
|
switch from command mode to edit mode |
|
undo |
|
reverse last undo |
|
switch code cell to text |
|
switch text cell to code |
|
move to the previous cell |
|
move to the next cell |
|
create a new cell above the current cell |
|
create a new cell below the current cell |
|
toggle line numbers in a code cell |
|
delete the current cell |
|
select current cell and above cell |
|
select current cell and above cell |
|
merge selected cells |
Edit Mode Shortcuts
|
execute a cell |
|
execute a cell and move to next cell |
|
execute a cell and create a new cell below |
|
switch from edit mode to command mode |
|
undo |
|
reverse last undo |
|
split cell at cursor |