Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python Tutorial

Acknowledgements: Florian Lemmerich, for the original version of this Notebook.

Setting up your working environment

Use the rocket symbol in the top-right corner to move this notebook to Binder or Google Colab (allows to save your progress, but needs a Google account).

Alternatively, if you absolutely want to, you can set up a Python environment with Conda locally. Students usually encounter a couple of roadblocks here so it takes long. Help for this kind of setup is only given in the first exercise session.

  1. Install Anaconda (or miniconda or mamba if you know what you are doing)

  2. Open the directory where this file is in your terminal.

  3. Install dependencies (python, jupyterlab, python packages) into a new conda environment with conda env create -f environment.yml.

  4. Start Jupyter Lab from the terminal with jupyter lab.

Variables and data types

Execute the cell with Shift+Enter or Ctrl+Enter:

Click on the left of a cell to get into the “blue” mode then you can press:

  • a -> new cell above the current cell

  • b -> new cell below the current cell

  • x -> delete the current cell

Click within a cell to get into the “green” mode (edit mode). Now you can write code here.

# print statements give out information
print ("Hello")
print ("World")
Hello
World
x = "Hello!"
print(x)

y = "Hello"
x = " World"
print (y + x)

x = 5
y = 2
print (x + y)
Hello!
Hello World
7
x
5
print (4+5)
print (x == 10)
9
False
x = "Hello"
y = "World"
print (x + y)
HelloWorld
# Python is dynamically typed, types can change:
x = "hello"
print (x)
x = 5
print (x)
hello
5
# main data types:
x = 5
print(type(x))

x = 5.0
print(type(x))

x = "5"
print(type(x))

x = True
print(type(x))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
# Python is strongly typed
x = "5"
y = 2
x+y
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 4
      2 x = "5"
      3 y = 2
----> 4 x+y

TypeError: can only concatenate str (not "int") to str
type(x)
str
x = "5"
y = "4"
int(x)+int(y)
9

Data structures

Lists

# creating an empty list
l = []
# adding elements to the list
l.append (2)
l.append (5)
l.append (10)
l
[2, 5, 10]
l[1]
5
# accessing the last element
l[-1]
10
# accessing a slice (end index is excluded)
l[:-1]
[2, 5]
#setting an element 
l[2] = 7.3
l
[2, 5, 7.3]
#the length of a list:
len (l)
3
# We can also directly specify lists:
# a list with integer objects
list_2 = [3,5,7,9]

# a list of strings
list_3 = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

# a list of mixed types
list_4 = [2, 5, "Elephant", 6, 8.2, True]
# we can also do lists of lists!
lol = [[1,2,3], ["a","b","c"], [1.2,2.3,4.5,6.7,8.9]]
#the length of a list:
len(lol)
3

Tuples

Tuples are just the same as lists, but they are immutable.

t = (1,2,3)
t[1]
2
t[1] = 5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 t[1] = 5

TypeError: 'tuple' object does not support item assignment

Sets

Sets are similar collections, but have no order and can contain each element only once

s = set()
s.add(50)
s.add(20)
s.add(10)
s.add(20)
s
{10, 20, 50}
l = [2,3,4,5,6,2,3,4,5,2]
s = set()
for x in l:
    s.add(x)
s
{2, 3, 4, 5, 6}
list (set(l))
[2, 3, 4, 5, 6]

Dictionaries

Python’s built-in mapping type. They map keys, which can be any immutable (unchangable) type, to values, which can be any type.

d = dict()
d = {}
presidents_first_inauguration = {}
presidents_first_inauguration['Biden'] = 2021
presidents_first_inauguration['Trump'] = 2017
presidents_first_inauguration['Obama'] = 2009
presidents_first_inauguration['Bush'] = 2001
print(presidents_first_inauguration)
{'Biden': 2021, 'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
# or, shorter:
presidents_first_inauguration = {'Biden': 2021,
                            'Trump': 2017, 
                            'Obama': 2009, 
                            'Bush': 2001}
presidents_first_inauguration ["Trump"]
2017
print (presidents_first_inauguration.keys())
print (presidents_first_inauguration.values())
dict_keys(['Biden', 'Trump', 'Obama', 'Bush'])
dict_values([2021, 2017, 2009, 2001])
len (presidents_first_inauguration)
4

Control statements

control flow in Python noticeable does not use ANY (,),[,],{,},... Instead indendation determines what belongs to block of commands

If-elif-else

x = 10
if x > 5:
    print ('This is a big number!')
This is a big number!
#Note the difference
x = 0
y = 0
if x > 5:
    x = x + 1
    y = y + 1
print (y)

x = 0
y = 0
if x > 5:
    x = x + 1
y = y + 1
print (y)
0
1
x = 15
if x > 20:
    print ('This is a very big number!')
elif x > 10:
    print ('This is a big number!')
else:
    print ('this is a small number!')
This is a big number!
x = 10
command = 'increment'
if command =='increment':
    x = x + 1
print (x)
11

Loops

first_names = ['John', 'Paul', 'George', 'Ringo']
for name in first_names:
    print("Hello " + name + "!")
Hello John!
Hello Paul!
Hello George!
Hello Ringo!

Contrary to many other programming languages there is no built-in for... counting loop. However, you can use the range function:

list(range(10,20,3))
[10, 13, 16, 19]
for i in range(10):
    print (i)
0
1
2
3
4
5
6
7
8
9
# enumerate is a useful convenience function.
# Note how pairs (index, name) are split up into separate variables.
for index, name in enumerate(first_names):
    print("Name "+ str(index) + ": " + name)
Name 0: John
Name 1: Paul
Name 2: George
Name 3: Ringo
# We can loop over any iterable, e.g., also on strings.
x = "example"
for letter in x:
    print (letter)
e
x
a
m
p
l
e
# As a simple example, lets create a dictionary, which maps each string in a list to its length:
name_lengths = {}
for name in first_names:
    name_lengths[name] = len(name)
name_lengths
{'John': 4, 'Paul': 4, 'George': 6, 'Ringo': 5}
# while loops function very similar to many popular languages:
x = 1
while True:
    x = x * 2
    if x >= 100:
        break
    print(x)
    
2
4
8
16
32
64

Functions

Defining your own functions is easy:

l = [1,2,3,4,5324,2,5,2,3,65,2]
len(l)
11
def print_all_names(names):
    for x in names:
        print (x)
print_all_names(l)
1
2
3
4
5324
2
5
2
3
65
2
def increment_function(x):
    x = x + 1
    return x
increment_function(5)
6
# You can call a function using its parameters names
def my_division(nominator,denominator):
    return nominator / denominator

print(my_division(12,4))
# you can call function parameters by name!
print(my_division(denominator=4, nominator=16))
3.0
4.0
# You can also specify default parameters for a function
def my_division(nominator,denominator = 2):
    return nominator / denominator

print (my_division(12,3))
4.0
# n
y = 5
x = 2
def increment_value(x):
    y = x + 1
    return y
increment_value(3)
y
5

Imports

Python has a lot of built-in packages you can use, or you can download and install more packages from the internet. Using such packages is easy:

import statistics

statistics.mean([3,5,7,9])
6
# you can also import just single functions from a package
from math import log
log(2.71 * 2.72)
1.9975805151995156

List Comprehension

my_list = [2,6,5,4,66,9,100,55,4,6,4,2]

l2 = [2*x for x in my_list]
l2
[4, 12, 10, 8, 132, 18, 200, 110, 8, 12, 8, 4]
l3 = []
for x in my_list:
    l3.append(2*x)
l3
[4, 12, 10, 8, 132, 18, 200, 110, 8, 12, 8, 4]
my_list = [2,6,5,4,66,9,100,55,4,6,4,2]

new_list = [len(str(x)) for x in my_list if x > 20]
new_list
[2, 3, 2]
nl = []
for x in my_list:
    if x > 20:
        nl.append(x*2)
nl
[132, 200, 110]

numpy

import numpy as np

x = np.array([1,2,3])
x * 4
array([ 4, 8, 12])
m = np.array([[1,2,3],[4,5,6],[7,8,9]])
m
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
m * 2
array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]])
m.dot(x)
array([14, 32, 50])
m[1,2]
np.int64(6)