About Jupyter Notebook

Jupyter Notebook can be used for:

  • Interactive data exploration;
  • Data analytics and visualization;
  • Financial quantitatve analysis;
  • Learning Python, R, Julia and dozens of other supported languages

The name Jupyter mixes Julia, Python and R, the first three languages targeted after iPython Notebook was reenginered to support different language processors (called kernels). This notebook is using the Python 3 kernel, as indicated in the top right corner.

Jupyter is a browser-based application that to edit and share interactive documents called notebooks.

Interacting with a Jupyter Notebook

As a first step, please take the User Interface Tour available from the Help menu above.

A notebook is made of text and code cells. You are reading a text cell, formatted using the Markdown syntax. To learn more about editing Markdown in Jupyter, read Working With Markdown Cells (local copy).

Code cells

Code cells are written in the programming language supported by the running kernel. They produce output in the form of text, tables or graphics. This is a code cell:


In [ ]:
def fibonacci(n):
    a, b = 0, 1
    while n:
        a, b = b, a + b
        n -= 1
    return a

fibonacci(100)

The label In [n] on the left indicates that is a code cell. If there's no n, the cell has not been executed. You can execute a code cell by selecting it and pressing the <ctrl><enter> keyboard combination. Please do it now and you'll see the 100th number in the Fibonacci sequence displayed in a new output cell, labeld Out [n], where n matches the number of the code cell that produced that output.

You can edit a code or text cell by clicking on it. Please edit the code cell above, changing the 100 argument in the fibonacci(100) function call to another number.

The content of a cell can be as simple as math expression like the one below. Use <shift><enter> to run it and select the next cell.


In [ ]:
2**100

Magic commands

In addition to the syntax of the kernel language, code cells support magic commands, which a are written with a % prefix, like this:


In [2]:
%ls -la


total 56
drwxrwxr-x 3 luciano luciano  4096 Out 25 04:16 ./
drwxrwxr-x 7 luciano luciano  4096 Out 25 03:59 ../
-rw-rw-r-- 1 luciano luciano  6157 Out 25 04:16 About Jupyter.ipynb
-rw-rw-r-- 1 luciano luciano  3642 Out 24 23:19 Demo fibonacci.ipynb
-rw-rw-r-- 1 luciano luciano 10533 Out 24 23:20 fibonacci.ipynb
-rw-rw-r-- 1 luciano luciano  4171 Out 25 00:08 Gerador de Fibonacci.ipynb
-rw-rw-r-- 1 luciano luciano  1508 Out 24 23:08 Intro.ipynb
drwxr-xr-x 2 luciano luciano  4096 Out 25 03:54 .ipynb_checkpoints/
-rw-rw-r-- 1 luciano luciano  7768 Out 25 04:04 Working With Markdown Cells.ipynb

To learn about the available magic commands, run %magic:


In [4]:
%magic

Graphics

Several graphing libraries can be used with the Python kernel, but the most popular is matplotlib.

To draw graphs in a notebook, use the magic command %matplotlib inline and the matplotlib.pyplot object, conventionally imported as plt:


In [ ]:
%matplotlib inline
# use the "magic" command above once to configure inline graphs

import matplotlib.pyplot as plt
plt.bar(range(1, 7), [fibonacci(n) for n in range(1, 7)])
plt.xlabel('Fibonnacci numbers')
plt.show()

If you want to convey an idea instead of actual data, you may want to format a graph as in the style of a xkcd comic:


In [ ]:
with plt.xkcd():
    plt.bar(range(1, 7), [fibonacci(n) for n in range(1, 7)])
    plt.xlabel('vacation days')
    plt.ylabel('ice cream gallons')
    plt.show()

Attention: the Humor-Sans.ttf font needs to be installed for correct display of xkcd graphics. After installing the font, if you still get a missing font warning, run the cell below to clear the Jupyter font cache.


In [ ]:
import matplotlib as mpl
font_cache_path = mpl.get_cachedir() + '/fontList*.cache'
# uncomment the magic command below to actually clear the cache
# %rm $font_cache_path

Multimedia displays

The display module of the IPython API allows embedding several external media types into a notebook, including a Web page:


In [5]:
from IPython.display import IFrame
IFrame('https://en.wikipedia.org/wiki/Fibonacci_number', width='100%', height=400)


Out[5]:

In [ ]: