Introduction to Jupyter (What is this thing?)

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:

  • Code cells - Code cells have code which is executed by the kernel. Hence, the also have an output section right underneath the cell.
  • Markdown cells - Markdown cells are parsed into HTML and using markdown stylization.

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):

  • Edit mode - If a cell is in an edit mode, it is possible to type content into it. When a cell is in the edit mode, it has a **green border**.
  • Command mode - If a cell is in the command mode, it can't be edited. But it's easier to move, copy, cut, run, etc. and perform operations on the cell itself. When a cell is in a command mode, it has a **blue border**.

Creating a new cell

To create a new cell, you can either:

  • Choose the "Insert" > "Insert cell below" or "Insert cell above" option in the menubar on the top.
  • Type b to add a cell below the focused cell. Type a to add a cell above the focused cell. This needs to be done in the command mode.

Deleting a cell

To delete a cell, you can either:

  • Choose the "Edit" > "Delete a cell" option in the menubar on the top.
  • Type dd to delete the cell in the command mode. (2 times d)

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.
In [3]:
# This creates a variable a. Hence, there's no output.
a = 10
In [4]:
print("The value of a is =", a)
The value of a is = 10

There are two other keyboard shortcuts for running code:

  • Alt-Enter runs the current cell and inserts a new cell below the one that was executed.
  • Ctrl-Enter run the current cell and enters the edit mode of the cell to edit it.

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:

In [7]:
a =
  File "<ipython-input-7-aa268cd80993>", line 1
    a =
SyntaxError: invalid syntax

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:

In [8]:
import time
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-8-8861cbcd4718> in <module>()
      1 import time
----> 2 time.sleep(30)


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.

In [1]:
print("hi, stdout")
hi, stdout
In [9]:
from __future__ import print_function
print('hi, stderr', file=sys.stderr)
NameError                                 Traceback (most recent call last)
<ipython-input-9-580ad4b3d8a2> in <module>()
      1 from __future__ import print_function
----> 2 print('hi, stderr', file=sys.stderr)

NameError: name 'sys' is not defined

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.

In [10]:
import time, sys
for i in range(5):

Large outputs

Beyond a certain point, output will scroll automatically:

In [ ]:
for i in range(500):