Getting Started

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.

Installation

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)

alternative use use the deployed version directly:

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

Deployment

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.

Usage

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

First Steps

Juypter notebooks consists of individual cells. There are two major cell types: Code and Markdown.

Useful keyboard shortcuts:

  • Enter: enter edit mode of the selected cell
  • Shift-Enter: run cell, select below
  • Ctrl-Enter: run cell
  • Alt-Enter: run cell, insert a new cell below

Getting Help:

  • Tab code completion or indent
  • Shift-Tab for a function, e.g. argument list
  • 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?

Interactive Python basics

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]:
3

In [11]:
3+4
10/2


Out[11]:
5.0

In [12]:
print(5+2)
3+2


7
Out[12]:
5

In [13]:
a = 5+2
b = 9
a/b


Out[13]:
0.7777777777777778

In [14]:
def sum(a,b): #indent is important in Python!
    return a+b
sum(4,4)


Out[14]:
8

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))


sum  16
sub -2

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)


[2, 4, 6, 8]
{'a': 'ALPHA', 'b': 'BETA'}

In [17]:
if a < 5:
    print ('small')
else:
    print ('large')


large

In [18]:
c = 'small' if a < 5 else 'large'
c


Out[18]:
'large'

In [19]:
#what else: generators, iterators, classes, tuples, ...