Table of Contents

Jupyter Notebook - Quick Introduction

A very attractive way to use python and work interactively is provided by jupyter notebooks

Great way to develop and document an interactive analysis and to share with others:

Starting:
In shell-window:

jupyter notebook

Sometimes problems on cip nodes, you might need:

jupyter notebook --browser=firefox

More info at https://jupyter.readthedocs.io/en/latest/index.html

Python code cells

Code cells

Notebook managing

Notebooks

They can also be executed and stored on remote system and accessed via web-browser.

Edit Modes and Cell Types

There are two modes in which jupyter notebooks are displayed:

To enter edit mode, press ENTER. To leave it, press ESC or CTRL+ENTER (to execute code or render markdown) or SHIFT+ENTER (to execute / render and move to the next cell).

There are mainly two different types of cells, code and markdown. To switch the type of a cell in navigation mode, press

You can also delete cells in navigation mode by pressing d two times.

(Hint: You can find all keyboard shortcuts under "Help" of the notebook menu.)

Help and documentation

Jupyter/IPython uses the ? character as a shorthand for accessing documentation and other relevant information on any object, function or module:

In [2]: len?
Type:        builtin_function_or_method
String form: <built-in function len>
Namespace:   Python builtin
Docstring:
len(object) -> integer

Return the number of items of a sequence or mapping.

This notation works for just about anything, including object methods:

In [3]: L = [1, 2, 3]
In [4]: L.insert?
Type:        builtin_function_or_method
String form: <built-in method insert of list object at 0x1024b8ea8>
Docstring:   L.insert(index, object) -- insert object before index

or even objects themselves, with the documentation from their type:

In [5]: L?
Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

Tab-completion of object contents

Every Python object has various attributes and methods associated with it. This can be accessed in Python with help or dir. But much more convenient is the tab-completion interface. To see a list of all available attributes of an object, you can type the name of the object followed by a period (".") character and the Tab key:

In [10]: L.<TAB>
L.append   L.copy     L.extend   L.insert   L.remove   L.sort     
L.clear    L.count    L.index    L.pop      L.reverse

IPython Magic Commands

IPython magic commands are prefixed by the % character.

We just discuss two quick examples, but will see more in later chapters.

Running External Code: %run

Rather than writing or loading code in a cell it can be convenient to just run code in an external python file. This can be done with the %run magic.

In [6]: %run myscript.py

The output of the code will be shown and also all declarations (vars, functions, imports, ...) are available, it is equivalent to load and execute the code in a cell.

Timing code %timeit

This will determine the execution time of the single-line Python statement that follows it. For example, we may want to check the performance of a list comprehension:

In [8]: %timeit L = [n ** 2 for n in range(1000)]
1000 loops, best of 3: 325 µs per loop

The benefit of %timeit is that for short commands it will automatically perform multiple runs in order to attain more robust results. For multi line statements, adding a second % sign will turn this into a cell magic that can handle multiple lines of input. For example, here's the equivalent construction with a for-loop:

In [9]: %%timeit
   ...: L = []
   ...: for n in range(1000):
   ...:     L.append(n ** 2)
   ...: 
1000 loops, best of 3: 373 µs per loop

Shell commands

Simple executon of shell commands, just precede with !

Python versions

We are using for the course the recent Python 3.7 version together with corresponding Numpy, Scipy, Pandas, etc, packages.

The installation is based on the Anaconda and conda tools, which provide scientific and machine learning python packages for many architectures (Linux, WIndows, Mac).

Pandas provides a function to get detailed info on installed package versions:

Python3 vs Python2

For most parts differences between Python3 and Python2 are small:

More info e.g. in http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

We will use (only) Python 3. Python 2 was discontinued in 2020.