Jupyter notebook INTRODUCTION

DS Data manipulation, analysis and visualisation in Python
December, 2017

© 2016, Joris Van den Bossche and Stijn Van Hoey (mailto:jorisvandenbossche@gmail.com, mailto:stijnvanhoey@gmail.com). Licensed under CC BY 4.0 Creative Commons



In [ ]:
from IPython.display import Image
Image(url='http://python.org/images/python-logo.gif')

To run a cell: push the start triangle in the menu or type SHIFT + ENTER/RETURN

Notebook cell types

We will work in Jupyter notebooks during this course. A notebook is a collection of cells, that can contain different content:

Code


In [ ]:
# Code cell, then we are using python
print('Hello DS')

In [ ]:
DS = 10
print(DS + 5) # Yes, we advise to use Python 3 (!)

Writing code is what you will do most during this course!

Markdown

Text cells, using Markdown syntax. With the syntax, you can make text bold or italic, amongst many other things...

  • list
  • with
  • items

Link to interesting resources or images:

Blockquotes if you like them This line is part of the same blockquote.

Mathematical formulas can also be incorporated (LaTeX it is...) $$\frac{dBZV}{dt}=BZV_{in} - k_1 .BZV$$ $$\frac{dOZ}{dt}=k_2 .(OZ_{sat}-OZ) - k_1 .BZV$$

Or tables:

course points
Math 8
Chemistry 4

or tables with Latex..

Symbool verklaring
$BZV_{(t=0)}$ initiële biochemische zuurstofvraag (7.33 mg.l-1)
$OZ_{(t=0)}$ initiële opgeloste zuurstof (8.5 mg.l-1)
$BZV_{in}$ input BZV(1 mg.l-1.min-1)
$OZ_{sat}$ saturatieconcentratie opgeloste zuurstof (11 mg.l-1)
$k_1$ bacteriële degradatiesnelheid (0.3 min-1)
$k_2$ reäeratieconstante (0.4 min-1)

Code can also be incorporated, but than just to illustrate:

BOT = 12
print(BOT)

HTML

You can also use HTML commands, just check this cell:

html-adapted titel with <h3>

Bold text <b> of or italic <i>

Headings of different sizes: section

subsection

subsubsection

Raw Text

Cfr. any text editor

Notebook handling ESSENTIALS

Completion: TAB

  • The TAB button is essential: It provides you all possible actions you can do after loading in a library AND it is used for automatic autocompletion:

In [ ]:
import os
os.mkdir

In [ ]:
my_very_long_variable_name = 3
my_ + TAB

Help: SHIFT + TAB

  • The SHIFT-TAB combination is ultra essential to get information/help about the current operation

In [ ]:
round(3.2)

In [ ]:
os.mkdir

In [ ]:
# An alternative is to put a question mark behind the command
os.mkdir?
EXERCISE: What happens if you put two question marks behind the command?

In [ ]:
import glob
glob.glob??

edit mode to command mode

  • edit mode means you're editing a cell, i.e. with your cursor inside a cell to type content --> green colored side
  • command mode means you're NOT editing(!), i.e. NOT with your cursor inside a cell to type content --> blue colored side

To start editing, click inside a cell or

To stop editing,

new cell A-bove

Create a new cell above with the key A... when in command mode

new cell B-elow

Create a new cell below with the key B... when in command mode

CTRL + SHIFT + P

Just do it!

Trouble...

NOTE: When you're stuck, or things do crash:
  • first try **Kernel** > **Interrupt** -> your cell should stop running
  • if no succes -> **Kernel** > **Restart** -> restart your notebook

Overload?!?



No stress, just go to

`Help` > `Keyboard shortcuts`

  • Google search is with you!

REMEMBER: To run a cell: push the start triangle in the menu or type SHIFT + ENTER

some MAGIC...

%psearch


In [ ]:
%psearch os.*dir

%%timeit


In [ ]:
%%timeit

mylist = range(1000)
for i in mylist:
    i = i**2

In [ ]:
import numpy as np

In [ ]:
%%timeit

np.arange(1000)**2

%lsmagic


In [ ]:
%lsmagic

%whos


In [ ]:
%whos

Let's get started!


In [ ]:
from IPython.display import FileLink, FileLinks

In [ ]:
FileLinks('.', recursive=False)

The follow-up notebooks provide additional background (largely adopted from the scientific python notes, which you can explore on your own to get more background on the Python syntax if specific elements would not be clear.

For now, we will work on the python_rehearsal.ipynb notebook, which is a short summary version of the other notebooks to quickly grasp the main elements