Introduction to Jupyter

Jupyter is a web version of the python shell. It's useful to write text like this, or can also write python code and execute them block by block. It can also be used for other languages like Julia, R, Java, Javascript, and so on (pretty much anything!). This notebook introduces the basic blocks that help us use the notebook.

It provides an interactive interface to be able to run blocks of code and also to write documentation to understand the code.

1. Cells

Jupyter's basic building block is a cell. There are majorly two different types of cells in jupyter:

Focusing on cells: Cells can be selected by clicking on them. The currently focused cell is shown with a blue border. It is also possible to change the focused cell by using the arrow keys.

2. Editing cells

There are two modes that are available on Jupyter notebooks (Similar to vim or emacs):

Creating a new cell

To create a new cell, you can either:

Deleting a cell

To delete a cell, you can either:

3. Running Code

Code cells allow you to enter and run code in them, unline markdown cells.

Run a code cell by:

  1. Focus on the cell by clicking on it.
  2. Then, press Shift-Enter or press the button in the toolbar above to run the cell.

There are two other keyboard shortcuts for running code:

You can check if a cell is running by looking on it's left. If it shows In [*]:, it means that the cell is still being run. Once the cell has completed running, it is assigned a number. For example: In [4]:. The number denotes the order in which the cells are run. Hence, if 4 is shown, the cell was the fourth to be run in the notebook.

Code cells show the output of the code below the cell. If there is no output (Like in a = 10) there is no output shown. If there is an error, it shows the error with helpful debugging information instead:

4. The Kernel

All code is run using a kernel. The kernel is a separate process which links the notebook with the compiler/interpreter of the appropriate type (based on the language). The Kernel can be interrupted or restarted.

When you stop the execution of a kernel, python throws a KeyboardInterrupt exception.

Try running the following cell. It pauses and sleeps for 30 seconds. While it is running, hit the (stop) button in the toolbar above. It should give the KeyboardInterrupt exception:

Restarting the kernels

The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the in the toolbar above. On doing this, all your stored variables will be deleted from the session and will need to be redefined.

5. Output formatting

Handling sys.stdout and sys.stderr

The stdout and stderr streams are displayed as text in the output area.

Output is asynchronous

All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end.

Large outputs

Beyond a certain point, output will scroll automatically: