Jupyter Notebook basics#
Jupyter Notebook is an aplication that enables you to create documents containing both code and explanations of this code in understandable language. Such documents are called ‘notebooks’. The code that you include in these notebooks can also be run (or executed) directly.
Creating notebooks enables you to share your code and the documentation of this code in a very effective way.
Cells#
A notebook is essentially a collection of cells. We shall be working with two types of cells.
A Code cell contains code to be executed using the so-called Python kernel. When you run a cell, the output will be displayed directly below the cell.
A Markdown cell contains rich text. The text can be formatted using a language named Markdown.
To view the markdown codes, you need to double click on the cell that contains this text. You can try this with this current cell.
The cell below contains code. Double clicking on this cell and try to edit the code.
# This cell contains code
print('Hello world!')
Hello world!
Cells can be run by placing the cursor inside the cell and by pressing Ctrl + Enter (or Command + Enter on MacOS). Alternatively, you can click ‘Run
’ in the menu at the top of the screen. Try to experiment with this. Can you change the output of the print()
function, for instance?
Code cells always have the characters In [ ]:
to their left. While the kernel is still active (i.e. when it is busy running the code), you will see an asterisk (’*
’) in between the two square brackets. When it has finished running, Jupyter Notebook will display a number. The program keeps track of the order in which you run all the cells.
You can also add new cells by choosing Insert
> Insert Cell Above
or Insert Cell Below
from the menu. New cells will be code cells by default. If you want to change the newly created cell into a Markdown cell, you can use the drowdown menu at the top of your screen, containing values such as Code
and Markdown
.
To delete a cell, you can choose Edit
> Delete cells
.
# Click on this cell to insert a new cell below it. Next, try to to delete this cell
Finally, try to run the cell below.
import this
Saving notebooks#
Normally, when you work with Jupyter Notebook, you use notebooks to write code and to produce some results. Of course you want to save that work. Although you may see that Jupyter Notebook automatically saves changes in “checkpoints” (temporary backups), you need to save your changes actively to make sure that you do not lose any work.
Save your file by clicking the “floppy disk” button on the toolbar, or via “File” > “Save”, or using a keyboard shortcut Ctrl + S (or Cmnd + S on MacOS).
Closing a notebook#
To close a notebook, save it and use the menu option “File” > “Close and Halt”. If you close the browser tab with your notebook directly, the file is actually still running. Close and Halt stops the background process and then closes the browser window.
Exiting Jupyter Notebook#
Since Jupyter Notebook is running in the background while you work in the browser frontend, closing the browser tabs or windows is not enough to stop the application. To exit Jupyter Notebook, click on the “Quit” button in the top right of the page showing the dashboard. When the background process succesfully terminated, Jupyter Notebook will tell you that you can close the browser tab or window.