Theoretical physics in transportation

Jupyter tutorial

Jupyter is a very convenient environment for solving some particular problem with Python language. It is similar to the console in the sense that the environment reacts on your commands immediately. On the other hands, it is similar to environments like Mathematica or Maple in that the code you type does not go away after you execute it. Each piece of code remains in its own cell which you can execute at any time. Moreover, Jupyter notebooks support formatting and $\LaTeX$ so you can accompany your code with fancy comments. Or you can create a lecture on theoretical physics with inline Python code!

Warning. This tutorial assumes that you have successfully followed the installation tutorial where you also find how to run Jupyter and create a new file.

Cell manipulation

When you create a new notebook in Jupyter, you should see something like this:


Figure 1. New notebook.

In Jupyter, there are input cells where you type the Python commands. They are indicated by text In [ ] on the left hand side. Click on the cell and type some Python command, e.g.

  sum( x**2 for x in range(101) )

In order to execute the command, press Shift + ENTER. Jupyter evaluates the command and displays a new cell with the result. Such output cells are indicated by Out [ ], see the following example.

Now, let's do an experiment. Arrange the windows on your desktop so that you can simultaneously see the following instructions and operate the Jupyter notebook.

  • Click on arbitrary cell with mouse. After clicking, you should see a blinking cursor. If you now press any character, it will be simply typed on the position of the cursor.
  • Press ESC button. The blinking cursor disappeared. That means that keys you press will be interpreted as commands.
  • Press a. A new input cell was created above the active cell.
  • Press ENTER. Blinking cursor appears and you can type a text into a new cell. Type few random characters.
  • Now press ESC again so that you enter the command mode again.
  • Press dd (i.e., press character d twice). That will delete the active cell and the old cell becomes active.
  • Press b which creates a new input cell below the active cell. The rest should be clear.
  • When you are in command mode, you can navigate to any cell using the arrow keys (up, down). To activate selected cell, press ENTER. To deactivate cell being edited, press ESC.

Full list of available shortcuts can be displayed (in the command mode) by pressing h

You perhaps noticed that in order to execute the code, you need to press Shift + ENTER, rather than just ENTER. The reason is that the latter will act as in usual text editor, i.e. it will create a new line in the cell, like in the following example.


Advanced formatting

Let us create, step-by-step, a simple Jupyter notebook which will demonstrate its basic capabilities. First, delete all cells using dd in command mode, until your notebook consists of the single cell like in the Figure 1. Select this single remaining cell and enter the command mode (for example, click on the cell, then pres ESC, so that the blinking cursor disappears). In the command mode, press m. This will change the cell from Code to Markdown, as you can see in the panel on the top of the notebook. Markdown means that what you enter will be not interpreted as the Python code, but as the formatting commands. In particular, the sequence of hashes is interpreted as the heading of corresponding level. After you pressed m, press ENTER in order to edit the cell. In order to create first-level heading, type just one # and follow it by the heading itself. In our example, the cell should look like this:

# My first Jupyter notebook

After you type the desired heading, press Shift + ENTER in order to confirm the changes and the heading will be formatted accordingly:

Of course, you can modify the heading at any time by navigating to the cell (by clicking or by arrows in command mode) and entering the editing mode (by pressing ENTER) if necessary. After you do your changes, confirm them by pressing Shift+ENTER again.

As a small exercise, let us create a 2nd-level heading.

  • Navigate to the cell My first Jupyter notebook
  • Create a new cell bellow the title by pressing b
  • Change the cell from Code to Markdown by pressing m
  • Press ENTER to start editing the cell and type ## Introduction
  • Press Shift+ENTER to confirm the changes.
Your notebook should look like this:

Let us add some text. Create another cell, change it to Markdown and type the text inside it. You can write either plain text or use HTML tags in a usual way. In our case, we chose the following text:

  This introductory notebook illustrates basic capabilities of Jupyter. It was created for the purposes
  of the lecture <i>Theoretical physics in transportation</i>. Details on the lectures can be found on
  the <a href="http://utf.mff.cuni.cz/~scholtz/index.php?section=lectures&page=TEF/tef">web page</a>.

During the editing, your notebook should look like this:

After pressing Shift+ENTER, the cell is formatted as follows:

Beside HTML, you can also use $\LaTeX$ which is great if you want to create notebook with formulas. If you want to include a separate line with mathematical formula, use the $\LaTeX$ environment align, for example,

\begin{align}
f : x \mapsto x^2 + 1
\end{align}

Again, during the editing, your cell may look similar to this:

After pressing Shift+ENTER, the changes are confirmed and $\LaTeX$ code is compiled. The result:

Download

Here you can download the full notebook . Below you see how this notebook should look like in your web browser. The version here is, of course, not editable.

Last update of this page: June 05 2019 12:57:32.