Notebook basics

This notebook covers the minimal information one needs in order to execute code in an existing notebook.
If you already know notebooks even a tiny bit, no need to dwell here, you won't learn anything new :-)

What is a notebook?

A notebook integrates code and its output into a single document that combines visualizations,
narrative text, mathematical equations, and other rich media.
In other words: it's a single document where you can run code, display the output, and also add explanations,
formulas, charts, and make your work more transparent, understandable, repeatable, and shareable.

--- from How to Use Jupyter Notebook in 2020: A Beginner’s Tutorial

That's about it. A notebook is a mean to interleave executable code with some documentation, which can be text, image...
pretty much anything! Notebooks support many programming languages. In this tutorial, we write Python code.

The notebook content is divided into cells, which are independent block of either code or documentation.

  • The text you read now has been written in a markdown cell (documentation)
  • Right below, you can see a simple code cell containing only a print statement.
In [ ]:
print("Hi there, I'm a code cell!")

As you can see, the rendering of the code cell is different.
(Note: the exact rendering depends on the software used to visualize and interact with the notebook)

One important difference with the documentation cell is the [ ] showing up on the left-side of the cell.
While the brackets are empty, it indicates that the code in that cell has not been executed yet.

Executing code cells

Let go ahead and execute it!

  • Click on the code cell above (or navigate to it with the keyboard up and down arrows);
  • Execute the code by either
    • Clicking the Run button from the task bar, or
    • Using the Ctrl+Enter keyboard shortcut.

Tada! You can now see the output of your code cell displayed below the cell, if any.
In our case, the output is a simple string.

Notice that the brackets now indicate [1]. If you execute the cell again, it will show [2], and so on.
Thus, as you might have got it already, the number in the brackets indicate the sequence number of the cell execution;
every time a cell (any cell) is executed in the notebook, this number is incremented and displayed next to the cell.

Note. The same commands also render documentation cells.
E.g., if you edit a Markdown cell (i.e., you see the source code displayed), you can render it with Ctrl+Enter.

State sharing and persistence

One important observation is that the code cell share their state!
That is, variables or functions that are created in one executed cell can be used in other cells.

Let illustrate that real quick.

In [ ]:
# Let's instantiate a variable here...
myVar = 42

Once the cell above has been executed, myVar is globally defined and accessible anywhere in the notebook.

In [ ]:
# ... we can access it there

Finally, state is persistent, that is, even if you change the content of a cell and re-execute it,
the output of past executions are still kept in memory. For example, if you update the cell above
to change the variable name from myVar to my_var and re-execute, a new variable will be created,
but the old one remains too!

Try-it out, you'll see you can still run print(myVar).

If you want to clean up the state of a notebook, you can either

  • restart the kernel by clicking the circled arrow in the task bar, or
  • go to Kernel and select Restart or Restart & Clear Output.

Creating new cells

At some point, you may want to create new cells. There are many ways to do this,
here are some of the most convenient ones:

  • Click the + button in the task bar (Insert cell below);
  • Use the Alt+Enter shortcut, which both executes the current cell and inserts a new cell below.

Going further

This was a really minimalistic introduction into how to interact with a Jupyter notebook.
Naturally, it barely scratched the surface of what there is to know about notebooks!

If you want to dig a little deeper, there are plenty of good tutorials for beginners throughout the web.
Here is a short selection.

Generic tutorials

Useful pages from the official Jupyter documentation