Documents

A web browser opens and show the "Files" tab by default. Select a "Python 3" notebook from the "New" menu.

This create a new notebook document:

Notebook documents contain the inputs and outputs of an interactive session as well as narrative text that accompanies the code but is not meant for execution. Rich output generated by running code, including HTML, images, video, and plots, is embeddeed in the notebook, which makes it a complete and self-contained record of a computation.

Change the notebook title in the header at the top. The document is store with its title as filename suffixed with .ipynb inside your working directory.

Cf. What is a Jupyter Notebook

Cells

Notebooks consist of a linear sequence of cells containing text or executable code. The web interface represents a be-modal editor:

  • Enter to go into edit mode for a cell
  • Esc to leave insert mode back to command mode

Command mode keys:

  • h to show command keys
  • a/b create new cell above/bellow
  • dd delete current cell
  • s save notebook

Code

The default cell type is code. The code is executed on the associated kernel.

  • Shift-Enter run current cell
  • Alt-Enter run current cell and inserts a new one below
  • Ctrl-Enter run the current cell and enters command mode

Stdout and stderr streams are displayed as text in the output area.

Clear the output of a cell by changing its type to raw an then back to code using the command keys r, y


In [3]:
import sys
print('Some text')
print('Errors...', file=sys.stderr)


Some text
Errors...

Markdown

Use the command key m to change the cell type to markdown.

It uses the typical Markdown notation including embeded code for illustration (inluding GitHub style syntax high-lighting):

if (i=0; i<n; i++) {
  printf("hello %d\n", i);
  x += 4;
}

Cf. Markdown Cells

MathJax

LaTex style math equations inline expressions enclosed $ (dollar):

$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$

Center a single line expressions by surrounding it with $$ (double dollar).

Built-in Commands

Also called "magic commands".

  • Single magic commands are prefixed with % (percent)
  • Multi-line epxressions start with %% (double percent)
  • Prefix the magic command with ? (question mark) to see the help text

List a all magic functions with:


In [ ]:
%lsmagic

Interpreters

Execute a shell command by prefixing it with !


In [4]:
!date +%s


1501677502

Execute Ruby code:


In [8]:
%%ruby
puts 4 * 5


20