Notebooks basics#
In this first chapter, we just give a few hints regarding the usage of notebooks with Python.
What is a notebook#
This document is a Jupyter notebook. It is a pure text file that contains metadata recognized by VSCode or Jupyter Lab to be rendered corretyl. It is split in many parts called cells, which can contain simple text (like this current cell) or lines of code, like the following:
a = 5
Manipulate cells#
You can split your code in as many cells as you want. Those cells can also be manipulated in different ways. Fo this you typically select a cell by clicking in the left margin. You can:
use the options on the top right of the cell e.g. to execute, suppress etc. the cell
use the
...menu to have more advanced options e.g. to switch a cell from code to text mode (and vice-versa)move a cell
merge multiple cells
There are a few useful shortcuts to know that will make your life easier. Those shortcuts opperate at the cell level and are typically a single letter. If you are currently editing a cell (i.e. writing in it) you first need to “get out of it” by typing Esc. Then you can:
type B to add a cell below the current one
type A to add a cell above the current one
type DD (twice D) to remove the current cell
type M to change the cell to Markdown (text cell)
type Y to change the cell to Code
Note that you can also add new Code or Text cells by hovering over the bottom of the current cell and clicking on +Cell or +Markdown.
Executing code#
A code cell needs to be executed for Python to be “aware” of the code it contains. Executing means typically that variables like a are now stored in the computer memory so that they can be reused later. All variables defined in a notebook are shared within that notebook (but not in other notebooks).
To exectute a cell you can hit Shift + Enter or use the play button on the left of the cell.
a = 5
In addition to executing a single cell, you have the option of running all cells above or below the current cell using the options on the top right of the cell. You can also use the Run All button at the top of the notebook.
Warning on execution#
Cleaning#
While your notebook is running, all commands you have run are “remembered”. Even if you delete a cell, what was executed in there stays in memory. If you want to clean the memory you have to restart the notebook by using the Restart button.
Order of execution#
The order in which cells are executed is arbitrary, i.e. if you have two code cells, you can in prinicple run first the second one and then the first one. For example:
c = a + b
a = 1
b = 2
del a
del b
del c
This happens sometimes when writing new code and testing various options. If you restart your notebook or if you send your notebook to someone it is however highly likely that cells will be executed in the normal order i.e.:
c = a + b
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[9], line 1
----> 1 c = a + b
NameError: name 'a' is not defined
In which case, you will see the error above: indeed a and b have not been stored in memory yet! It is therefore strongly recommended to have notebooks run TOP to DOWN in their final form. This will avoid many issues and misunderstandings.
Markdown cells#
Add mentioned above, there are two cell types, Code and Text. Text cells get formatted using Markdown, a simple markup language using simple signs like * or # to specify formatting. You can for example:
Format titles and subtitles using one or more
#signs.Write in italic or bold by surrounding text with
*or**, respectively.Create lists using
-Add a link by using
[this is my text](http link)
You can also find more information in guides online such as this one.
Exercise#
Open a notebook and make sure you can run simple code such as a = 5 , add and remove cells, re-start the kernel, convert a cell to Markdown etc.