Python the essentials: A minimal introduction
Introduction to GIS scripting
May, 2017© 2017, Stijn Van Hoey (mailto:stijnvanhoey@gmail.com). Licensed under CC BY 4.0 Creative Commons
the obligatory...
In [131]:
print("Hello INBO_course!") # python 3(!)
Python is a calculator
In [27]:
4*5
Out[27]:
In [28]:
3**2
Out[28]:
In [29]:
(3 + 4)/2, 3 + 4/2,
Out[29]:
In [30]:
21//5, 21%5 # floor division, modulo
Out[30]:
also logical operators:
In [2]:
3 > 4, 3 != 4, 3 == 4
Out[2]:
Variable assignment
In [6]:
my_variable_name = 'DS_course'
my_variable_name
Out[6]:
In [7]:
name, age = 'John', 30
print('The age of {} is {:d}'.format(name, age))
More information on print format: https://pyformat.info/
In [31]:
import os
You would load a library (`library("ggplot2")`) instead of importing a package
In [32]:
os.listdir()
Out[32]:
Loading with defined short name (community agreement)
In [33]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Loading functions from any file/module/package:
In [34]:
%%file rehears1.py
#this writes a file in your directory, check it(!)
"A demo module."
def print_it():
"""Dummy function to print the string it"""
print('it')
In [35]:
import rehears1
In [36]:
rehears1.print_it()
In [37]:
%%file rehears2.py
#this writes a file in your directory, check it(!)
"A demo module."
def print_it():
"""Dummy function to print the string it"""
print('it')
def print_custom(my_input):
"""Dummy function to print the string that"""
print(my_input)
In [38]:
from rehears2 import print_it, print_custom
In [39]:
print_custom('DS_course')
Different options are available:
floats
In [40]:
a_float = 5.
In [41]:
type(a_float)
Out[41]:
integers
In [42]:
an_integer = 4
In [43]:
type(an_integer)
Out[43]:
booleans
In [44]:
a_boolean = True
a_boolean
Out[44]:
In [45]:
type(a_boolean)
Out[45]:
In [46]:
3 > 4 # results in boolean
Out[46]:
Booleans are written as False or True, NOT as FALSE/TRUE
In [132]:
print(False) # test yourself with FALSE
In [28]:
a_string = "abcde"
a_string
Out[28]:
A string is a collection of characters...
In [29]:
a_string.capitalize(), a_string.upper(), a_string.endswith('f') # Check the other available methods for a_string yourself!
Out[29]:
In [30]:
a_string.upper().replace('B', 'A')
Out[30]:
In [31]:
a_string + a_string
Out[31]:
In [32]:
a_string * 5
Out[32]:
A list can contain mixed data types (character, float, int, other lists,...)
In [51]:
a_list = [1, 'a', 3, 4]
a_list
Out[51]:
In [53]:
another_list = [1, 'a', 8.2, 4, ['z', 'y']]
another_list
Out[53]:
In [34]:
a_list.append(8.2)
a_list
Out[34]:
In [35]:
a_list.reverse()
a_list
Out[35]:
In [146]:
a_list + ['b', 5]
Out[146]:
ADVANCED users area: list comprehensions
In [79]:
[el*2 for el in a_list] # list comprehensions...a short for-loop
Out[79]:
list comprehensions are basically a short-handed version of a for-loop inside a list. Hence, the previous action is similar to:
In [80]:
new_list = []
for element in a_list:
new_list.append(element*2)
print(new_list)
Another example checks the methods available for the list data type:
In [81]:
[el for el in dir(list) if not el[0] == '_']
Out[81]:
In [75]:
[el for el in dir(list) if not el.startswith('_')]
Out[75]:
In [72]:
sentence = "the quick brown fox jumps over the lazy dog"
In [82]:
#split in words and get word lengths
[len(word) for word in sentence.split()]
Out[82]:
R also has lists as data type, e.g. `list(c(2, 5, 3), 21.3, "a")`
A dictionary is basically an efficient table that maps keys to values. It is an unordered container
It can be used to conveniently store and retrieve values associated with a name
In [133]:
a_dict = {'a': 1, 'b': 2}
In [134]:
a_dict['c'] = 3
a_dict['a'] = 5
In [135]:
a_dict
Out[135]:
In [98]:
a_dict.keys(), a_dict.values(), a_dict.items()
Out[98]:
In [99]:
an_empty_dic = dict() # or just {}
an_empty_dic
Out[99]:
In [100]:
example_dict = {"timeseries": [2, 5, 3],
"parameter": 21.3,
"scenario": "a"}
example_dict
Out[100]:
R also has a dictionary like data type, e.g.
> example_dict <- list(c(2,5,3),21.3,"a")
> names(example_dict) <- c("timeseries", "parameter", "scenario")
> example_dict
$timeseries
[1] 2 5 3
$parameter
[1] 21.3
$scenario
[1] "a"
In [101]:
a_tuple = (1, 2, 4)
In [136]:
collect = a_list, a_dict
In [137]:
type(collect)
Out[137]:
In [138]:
serie_of_numbers = 3, 4, 5
In [139]:
# Using tuples on the left-hand side of assignment allows you to extract fields
a, b, c = serie_of_numbers
In [140]:
print(c, b, a)
In [19]:
grades = [88, 72, 93, 94]
In [20]:
from IPython.display import SVG, display
display(SVG("../img/slicing-indexing.svg"))
In [21]:
grades[2]
Out[21]:
In [22]:
from IPython.display import SVG, display
display(SVG("../img/slicing-slicing.svg"))
In [23]:
grades[1:3]
Out[23]:
In [36]:
a_list = [1, 'a', 8.2, 4]
In [25]:
a_list[0], a_list[2]
Out[25]:
Select from...till
In [37]:
a_string = "abcde"
a_string
Out[37]:
In [38]:
a_string[2:4]
Out[38]:
Select, counting backward:
In [39]:
a_list[-2]
Out[39]:
The `-` symbol in R has a completely different meaning: `NOT`
> test <- c(1, 2, 3, 4, 5, 6)
> test[-2]
[1] 1 3 4 5 6
In [40]:
a_list = [0, 1, 2, 3]
From the first element until a given index:
In [41]:
a_list[:3]
Out[41]:
In [42]:
a_list[::2]
Out[42]:
Dictionaries
In [45]:
a_dict = {'a': 1, 'b': 2}
a_dict['a']
Out[45]:
Tuples
In [46]:
a_tuple = (1, 2, 4)
a_tuple[1]
Out[46]:
start <= i < stopL[start:stop] has (stop-start) elements.L[start:stop:stride]Assigning new values to items -> mutable vs immutable
In [47]:
a_list
Out[47]:
In [48]:
a_list[2] = 10 # element 2 changed -- mutable
a_list
Out[48]:
In [49]:
a_tuple[1] = 10 # cfr. a_string -- immutable
a_string[3] = 'q'
In [169]:
for i in [1, 2, 3, 4]:
print(i)
In [ ]:
In [170]:
for i in a_list: # anything that is a collection/container can be looped
print(i)
In [172]:
for char in 'Hello DS':
print(char)
In [173]:
for i in a_dict: # items, keys, values
print(i)
In [174]:
for j, key in enumerate(a_dict.keys()):
print(j, key)
In [175]:
b = 7
while b < 10:
b+=1
print(b)
In [176]:
if 'a' in a_dict:
print('a is in!')
In [177]:
if 3 > 4:
print('This is valid')
In [178]:
testvalue = False # 0, 1, None, False, 4 > 3
if testvalue:
print('valid')
else:
raise Exception("Not valid!")
In [179]:
myvalue = 3
if isinstance(myvalue, str):
print('this is a string')
elif isinstance(myvalue, float):
print('this is a float')
elif isinstance(myvalue, list):
print('this is a list')
else:
print('no idea actually')
We've been using functions the whole time...
In [54]:
len(a_list)
Out[54]:
In [190]:
a_list.reverse()
a_list
Out[190]:
Defining a function:
In [193]:
def custom_sum(a, b, verbose=False):
"""custom summation function
Parameters
----------
a : number
first number to sum
b : number
second number to sum
verbose: boolean
require additional information (True) or not (False)
Returns
-------
my_sum : number
sum of the provided two input elements
"""
if verbose:
print('print a lot of information to the user')
my_sum = a + b
return my_sum
Setup of a function:
def
In [194]:
custom_sum(2, 3, verbose=False) # [3], '4'
Out[194]:
ADVANCED users area:
Functions are objects as well... (!)
In [184]:
def f1():
print('this is function 1 speaking...')
def f2():
print('this is function 2 speaking...')
In [185]:
def function_of_functions(inputfunction):
return inputfunction()
In [186]:
function_of_functions(f1)
Anonymous functions (lambda)
In [187]:
add_two = (lambda x: x + 2)
In [188]:
add_two(10)
Out[188]:
Ready for some real Python power: Numpy/Pandas