What are jupyter-notebooks?

  • Documents that contain rich text and executable code
  • Browser based app that allows editing and execution of notebooks
  • Kernels as "computational engines" that execute the code
  • (Dashboard as filebrowser for notebooks)

Commanding (running) and editing cells

Notebook content is structured in different cells of arbitrary size

Enter command mode with ESC key
-> navigate between cells, copy & paste content, run cells...

Enter editing mode with RETURN key
-> normal text editing

Cell content: markdown

Make cell a markdown cell with M key (in command mode)
-> write headings, bullet points, emphasis, LaTeX...

this is a new heading

this is italic text

  • this is a list of
  • some
  • bullet points

fancy latex
$\Rightarrow\mu\nabla^2 \underbrace{\left[\frac{\partial^2w}{\partial X^2} + \frac{\partial^2w}{\partial Y^2} + \frac{\partial^2w}{\partial Z^2}\right.} _{=\nabla^2w} - \frac{\partial}{\partial Z} \underbrace{\left.\left(\frac{\partial u}{\partial X} + \frac{\partial v}{\partial Y} + \frac{\partial w}{\partial Z}\right)\right]} _{\overset{(4)}{=} 0} = g\underbrace{\left(\frac{\partial^2\theta}{\partial Y^2} + \frac{\partial\theta^2}{\partial X^2}\right)} _{=\nabla^2\theta - \frac{\partial^2\theta}{\partial Z^2}}$

Cell content: code

Make cell a code cell with Y key
-> write normal python code, cells share single namespace over the whole document


In [2]:
#comments and numbers work normally
1 + 1


Out[2]:
2

In [23]:
#strings too
s = 'hello hacky people'
s


Out[23]:
'hello hacky people'

In [4]:
#variables can be assigned and will be known to all cells below this one
a,b = (10,10)

#output without print-statement only works if at the end of a cell
print(a+b)

#functions and classes can be defined like normal
def MyFunction(a,b):
    return [a+b,a-b,a*b,a/b]
MyFunction(a,b)


20
Out[4]:
[20, 0, 100, 1.0]

Libraries

Jupyter-notebooks have acess to all python libraries in your python distribution!


In [17]:
import numpy as np

c = np.arange(0,16).reshape((4,4))
d = np.ones((4,4))

#numpy arrays work!
print('marix c:\n',c)
print('matrix d:\n',d)

#a is still know from before... see?
print('scalar times matrix:\n',a*c)
print('matrix times matrix:\n',np.dot(c,d))


marix c:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
matrix d:
 [[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
scalar times matrix:
 [[  0  10  20  30]
 [ 40  50  60  70]
 [ 80  90 100 110]
 [120 130 140 150]]
matrix times matrix:
 [[  6.   6.   6.   6.]
 [ 22.  22.  22.  22.]
 [ 38.  38.  38.  38.]
 [ 54.  54.  54.  54.]]

In [21]:
#by the way: error messages and tracebacks work as well... :/
import seaborn as sns
#get the package via "conda install seaborn" in your terminal


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-21-0af279bcd19e> in <module>()
      1 #by the way: error messages and tracebacks work as well... :/
----> 2 import seaborn as sns

ImportError: No module named 'seaborn'

In [25]:
import matplotlib.pyplot as plt
#allows plots to be shown embedded in the notebook
%matplotlib inline

#create a beautiful poser-plot
x = np.linspace(0, 2 * np.pi, 500)
y1 = np.sin(x)
y2 = np.sin(3 * x)
fig, ax = plt.subplots()
nice_plot = ax.fill(x, y1, 'b', x, y2, 'r', alpha=0.3)


other nice things


In [12]:
#magic commands are available
%timeit(a*b)


The slowest run took 40.22 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 48.8 ns per loop

In [13]:
#run other notebooks or .py files
%run polar-chart.ipynb



In [15]:
#direct access to docstrings
?np.reshape()

Have fun!


In [ ]: