It provides an interactive interface to be able to run blocks of code and also to write documentation to understand the code.
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.
There are two modes that are available on Jupyter notebooks (Similar to vim or emacs):
To create a new cell, you can either:
To delete a cell, you can either:
Code cells allow you to enter and run code in them, unline markdown cells.
Run a code cell by:
Shift-Enteror press the button in the toolbar above to run the cell.
# This creates a variable a. Hence, there's no output. a = 10
print("The value of a is =", a)
The value of a is = 10
There are two other keyboard shortcuts for running code:
Alt-Enterruns the current cell and inserts a new cell below the one that was executed.
Ctrl-Enterrun 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 :. 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:
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
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
import time time.sleep(30)
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-12-8861cbcd4718> in <module>() 1 import time ----> 2 time.sleep(30) KeyboardInterrupt:
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.
The stdout and stderr streams are displayed as text in the output area.
from __future__ import print_function import sys print('hi, stderr', file=sys.stderr)
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.
import time, sys for i in range(5): print(i) time.sleep(0.5)
Beyond a certain point, output will scroll automatically:
for i in range(500): print(2**i)