Quick intro to Jupyter Notebook#

Note

We highly recommend using Jupyter notebooks for your coding assignments. This will allow your so seamlessly integrate code, plots, text and LaTeX into a single document.

This is a quick intro(duction) to how to use Python in a Jupyter Notebook. Jupyter Notebook is an open source web application you can use to create documents that contain code, plots, LaTeX equations and text in a single document. Jupyter notebooks support a many different scripts and programming languages, such as Python, JavaScript, HTML5, CSS, Julia and R. This makes it a powerful tool for creating interactive multi-media documents. Different programming languages use different kernels. This course book will use a Python 3 kernel.

Note

If you need a refresher on Python, we recommend reading the tutorial section of the official Python documentation.

Starting Jupyter#

If you’re using VSCode, the Jupyter extension will automatically detect any Jupyter notebook with the file extension .ipynb. If you prefer the browser application you can start the Jupyter notebook application by running the following command in your terminal or command prompt:

jupyter notebook

This will open your browser where you can create a new notebook or open existing notebooks in your current directory (folder). Jupyter notebooks are organized with cells of different flavours. You can write Python code in the code cell, which is followed by a result cell with the program output after the cell has been run. The text cell supports static content like text written in Markdown. Markdown supports different text formatting such as LaTeX notation, rendering $frac{1}{a^2}$ into \(\frac{1}{a^2}\). You can change the type of cell using the drop-down menu on the toolbar above your notebook.

Shortcuts#

Here’s a short list of useful keyboard shortcuts in Jupyter notebooks

Command Mode (Blue border):

  • A: Insert cell above

  • B: Insert cell below

  • X: Cut selected cell(s)

  • D D: Delete selected cell(s)

  • Y: Change cell to code

  • M: Change cell to markdown

  • Shift + Up/Down: Select multiple cells

  • Shift + M: Merge selected cells

Edit Mode (Green border):

  • Shift + Enter: Run cell and select below

  • Ctrl + Enter: Run cell

  • Ctrl + Shift + -: Split cell at cursor

Both Modes:

  • Esc: Enter command mode

  • Enter: Enter edit mode

  • H: Show all shortcuts

Restarting your kernel#

Sometimes your code might get stuck in an endless loop. You might also like to clear all your workspace variables. We can solve this by clicking the Kernel menu item Interrupt and Restart.

Magic Commands#

There are special commands that only work in a Jupyter notebook environment. Magic commands are preceded by % or %%. The magic command lsmagic lists all available commands

%lsmagic
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %code_wrap  %colors  %conda  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %mamba  %man  %matplotlib  %micromamba  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%code_wrap  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

You can list a cheat sheet with quickref

%quickref

who can list all the variables in your current notebook. whos is more detailed.

name = "linus"

%whos
Variable   Type    Data/Info
----------------------------
name       str     linus

We can also list the variables of a specific data type

%who str
name	 

Another useful command is timeit, which can time the execution of a Python statement or expression

from functools import lru_cache

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

%timeit fib(1000)
59.9 ns ± 1.94 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Details about any Python object can be found by adding ? to the end of a function or an object

round?

Exporting a Jupyter Notebook#

Exporting a Jupyter notebook to other formats requires nbconvert to be installed. nbconvert can be installed via the official nbconvert installation instructions. The simplest way to export your notebook is to save your notebook to a HTML file and print the webpage as a PDF. This often yields suboptimal documents. We recommend that you download the notebook as a PDF via LaTeX via the graphical interface. Optionally, you can convert the notebook to a PDF or a WebPDF using the following commands. If you’re struggling to install a LaTeX compiler, WebPDF is often the easiest and most reliable method.

Using LaTeX:

jupyter nbconvert your_notebook.ipynb --to pdf

Using WebPDF:

jupyter nbconvert your_notebook.ipynb --to webpdf

Learn more about Jupyter#

There are many resources online for learning more about Jupyter. This tutorial by RealPython is an example. YouTube hosts a great number of videos on the topic as well. The one listed below is a good started to learn the basics.