-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.qmd
204 lines (110 loc) · 4.73 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: "A soft intro to Python (part I)"
format:
revealjs:
theme: dark
smaller: true
jupyter: python3
---
## Setup
Setting up a local python environment is a pain...
![Source: http://xkcd.com/1987](img/py_nightmare.png){fig-align="center" width="50%"}
## Setup
Easiest way : install Anaconda Navigator...
Editors:
- IDLE = default
- Anaconda \> Spyder, PyCharm, ...
and RStudio too! File \> New file \> Python script
![Python prompt: `>>>`](img/py_in_RStudio.png){height="50%"}
## Setup
Online options:
- **Jupyter notebooks:** open from Anaconda
- **google colab :** (+) has google installed libraries and GPU/TPU power (limited GO). (-) to make one you need a google account
## Basics
Same programming rules:
- named variables:
`>>> a=50` - use `+=` to increment/concatenate
`>>> a += 1`
- a list = multiple values in a variable. Each value is identified by its **key** (name).
`>>> mylist = ['tic', 'tac', 'toc']`
- in Python, index starts at 0, not 1 (so NOT like in R): in mylist, the key for value 'tic' is 0.
`>>> print(mylist[2])`
## Basics
::: {style="font-size: 75%;"}
A common object to manipulate in Python are **dictionaries**: they're a pair of KEY:Value just like in lists, but here keys in dictionaries aren't ordered. Use `{}` to initiate them (but still use `[]` to manipulate them).
`>>> mydico = {}` or
`>>> mydico = dict(Lastname = "Petit", Firstname = "Cathleen", Pronounciation = "Kétline")`
Same conditional rules as in R: `if`, `else`, `elseif`... (--\> NO !! it's **elif** instead of `elseif`... but who uses those anyways ?)
**Arithmetic operators :** the usual, but also: `%`, which means 'modulo' = is a multiple of. Returns 1 or 0 (True/False).
**Logical operators :** `==`, `!=`, `>`, `>=` ...
:::
## For loops
Syntax:
::: {style="font-size: 100%;"}
```{python}
#| echo: true
#| eval: false
string = 'Hello sunshine !'
for a in string :
print(a)
```
Try it !
:::
## A few commun built-in functions:
::: {style="font-size: 55%;"}
`random(list)` → choose random element of a list
`len(list)` → number of elements in list
`max / min(list)`
`str.split(separator)` → transform str into list according to separator
`list.append(value)` → add a value to list (like c() in R)
`del list[key]`, **NOT the same as** `list.remove(value)`
`list.count(value)` → counts number of occurrences of that value in the list
`list[-1]` → shows the last value of the list
`list[a:b]` → show values from a to b
`x.extend(y)` → concatenate lists x and y
`str.find(string)` → counts occurrences of str in string
`str.replace(where, what)`
`input(string)` → prints string and asks user for feedback (VERY INTERESTING)
`range(int)` → useful in for loops: for i in range(1, 10):
:::
## Libraries
Just like in R: built-in functions, but also packages of functions, called libraries.
Use `import library` to load them. Colab has many pre-installed libraries, but sometimes you need to activate them at the beginning of your session with `!pip install library` or `pip install library` or `pip3 install library` if you're using a local environment.
### Common libraries:
- math, random, numpy, ...
- scikit-learn, pytorch, SciPy, TensorFlow, Keras....: `import sklearn as skl` → data science.
- matplotlib : data viz. You can also choose to only import a function instead of the whole library: `import matplotlib.pyplot as plt`
- pandas : Python's tidyverse ;) Handles dataframes.
## Your turn !!
- play around with the different functions
### Exercise:
Write a script to ask the user their age, and give different answers depending on whether you think it's old or young.
## Answer:
```python
limit = 99
age = input('How old are you ?')
if float(age) > limit:
print("Wow, you're a living fossil !")
else:
print("Oh yeah, you're alright.")
```
## Take home :
- indents !
- ":" after ifs and elses, rather than brackets like in R
- code execution by individual cells (on Colab/Jupyter) or entire script (Spyder, etc).
- python errors are usually quite precise: **read the error messages carefully**. Otherwise, absolute rule is: the internet has looooaaads of python forums, help pages, tutorials, bug fixes. A very active community.
## Homework :)
Generate a list of integers, and then show:
- the list
- the list in column format, displaying matching keys and values
- the sum of all elements of the list
- create a new list made of 5 multiples of the original list
- give min and max values of the list
- count occurrences of pair and odd numbers and sum them
## Quick games
Write a program to...
- throw a dice
- throw 2 dice
- Rock paper cisors
- guess a number between 1 and 1000 by indicating when the value is higher or lower
- hangman (without the graphical aspect)