---
title: Slides for Python workshop
description: View the slide with "Slide Mode".
---
# Python Workshop 2021
<!-- Put the link to this slide here so people can follow -->
###### Let's use a collaborative document throughout the workshops to keep things in one place. This is the link to this page: [https://hackmd.io/@yinhsieh/B1YuK3POO](https://hackmd.io/@yinhsieh/B1YuK3POO)
---
## Let's briefly talk about some things :cat:
##### May 11 Mini Intro
- [ ] Building blocks of Python
- [ ] Python-mindset vs. R-mindset
- [ ] Tools - setting up a Python workspace
- [ ] Using documentation to learn how to use packages
---
## Building Blocks
Boring but important. Managing each data type and knowing how to extract values from them is essential!
Learn these types as you learn the syntax, then you are set for life.
### TYPES OF INBUILT THINGS
#### 1. Scalars: everything that comes in one, *or none*
```typescript=py
i = 42 # integer
i = 2**77 # Integers have arbitrary precision
g = 3.14 # floating point number
c = 2 - 3j # Complex number
b = True # boolean
s = "Hello!" # String (Unicode)
q = b'Hello' # bytes (8-bit values)
```
#### 2. Collections: everything none, one, or multiple
```typescript=py
l = [1, 2, 3] # list
l[1] = True # list elements can be any type
d = {"Janne": 123, "Richard": 456} # dictionary
s = set(("apple", "cherry", "banana", "apple")) # Set of unique values
```
---
### COMMON ACTIONS WITH YOUR THINGS
#### 1. Control structures: conditionals and loops
The classic `if`, `elif`, `else` statement.
```typescript=py
x = 2
if x == 3:
print('x is 3')
elif x == 2:
print('x is 2')
else:
print('x is something else')
```
The brain-twisting `while` statement.
```typescript=py
x = 0
while x < 42:
print('x is ', x)
x += 0.2
```
The favourite `for` loops, in 3 flavours.
```typescript=py
xs = [1, 2, 3, 4]
for x in xs:
print(x)
```
```typescript=py
for x in range(9):
print(x)
```
A stray hello world snuck its way into this, sorry Isabell! :smiling_face_with_smiling_eyes_and_hand_covering_mouth:
```typescript=py
xs = [1, 'hello', 'world']
for ii, x in enumerate(xs):
print(ii, x)
```
---
#### 2. Functions and classes: what it's really all about
Functions in python are defined with the `def` keyword. They can take arguments, and return arguments.
```typescript=py
def hello(name):
"""Say hello to the person given by the argument"""
print('Hello', name)
return 'Hello ' + name
hello("Anne")
```
Classes allow us to make our own objects and attach default functions to them. Very useful!
```typescript=py
class Hello:
def __init__(self, name):
self._name = name
def say(self):
print('Hello', self._name)
h = Hello("Richard")
h.say()
```
---
### A Comment about TYPE
```
type(object)
```
Python is a dynamically typed language. This means that the type of an object is defined at runtime, and a variable can be used to point at another type.
```
x = 42
x = "hello"
```
Types follow their own laws, but since dynamic typing is actually a less strict way of interacting with objects, **you** have to to keep good track of what types your data are, and document this.
---
## Python vs. R Mindset
Not a war, it's just a HUGE asset if you know both.

How does this influence how we structure our code?
---
## Setting up Python workspace
IDE
Jupyter Notebook
ipython - interactive session
---
## Reading documentation
GDAL
Matplotlib
Rasterio
---
# :bulb:
### more ideas?
---
# :100: :muscle: :tada:
---