During this tutorial, we are using IPython/Jupyter Notebooks. Jupyter notebooks are a web based Python development environment allowing you to combine documentation (markdown), code, and their results into a single document. This follows a similar idea to Mathematica.
Anaconda is a free Python distribution including the most common Python packages for data analysis out of the box. Prebuild packages for the different platforms make it simply to use it and getting started quickly.
TASK: Install Anaconda on your machine (Please select the Python 3 download and in the lab the 32-bit version)
We use different frameworks/libraries in this tutorial:
Everything except for Seaborn is already included in Anaconda by default. So, lets install it.
TASK: Open a shell (bash, cmd) and execute:
conda install seaborn
Deploying Jupyter notebooks is quite simple. mybinder.org provides you with a free service that turns a Github repository into an collection of interactive notebooks accessible online.
Hint: If you have an index.ipynb notebook inside of a directory, this will be the default one.
Launching Jupyter is simple, just go to a command line, navigate to your desired directory and execute:
ipython notebook
This will open your webbrowser with the IPython dev environment in the current working directory. We are using this interactive tutorial as starting point. Clone it, navigate to it and launch ipython notebooks.
TASK clone this tutorial repository and launch the ipython environment inside of it
git clone https://github.com/sgratzl/ipython-tutorial-VA2015.git
cd ipython-tutorial-VA2015
ipython notebook
Juypter notebooks consists of individual cells. There are two major cell types: Code and Markdown.
Useful keyboard shortcuts:
Getting Help:
function? query the python docstring for the given function
In [8]:
#include some package which we use later on
import numpy as np
In [9]:
#test np.ar -> tab
a = np.array([1,2,3,4])
#test np.array -> shift-tab or np.array?
Python is an untyped dynamic language. The last output of a cell line will be printed. Individual values can also be printed using the print(...) function. Variables are just declared and assigned. Function are first level objects and Python can be used to program in a functional style. Some simple examples:
In [10]:
1+2
Out[10]:
In [11]:
3+4
10/2
Out[11]:
In [12]:
print(5+2)
3+2
Out[12]:
In [13]:
a = 5+2
b = 9
a/b
Out[13]:
In [14]:
def sum(a,b): #indent is important in Python!
return a+b
sum(4,4)
Out[14]:
In [15]:
def sub(arg1,arg2):
return arg1-arg2
def calc(f, a, b):
return f(a,b)
#functions are first level objects, e.g., can be passed as argument to another function
print('sum ', calc(sum, a, b))
print('sub', calc(sub, a, b))
In [16]:
#array
arr = [1,2,3,4]
#maps aka dictionaries/dicts
dictionary = { 'a': 'Alpha', 'b': 'Beta'}
#array transformation
arr2 = [ a * 2 for a in arr]
dict2 = { k : v.upper() for k,v in dictionary.items()}
print(arr2)
print(dict2)
In [17]:
if a < 5:
print ('small')
else:
print ('large')
In [18]:
c = 'small' if a < 5 else 'large'
c
Out[18]:
In [19]:
#what else: generators, iterators, classes, tuples, ...