-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_tuto.qmd
126 lines (64 loc) · 2.55 KB
/
python_tuto.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
---
title: "Python tutorial"
format:
revealjs:
#logo: img/cesab-logo.png
theme: moon
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="centre" height="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 += 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 toto is 0.
`>>> print(mylist[2])`
## Basics
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, elsif --> NO !! it's **elif** (but who uses those..?!)
Otherwise same logical operators : `==, !=, >, >=, ... `
Another charming specificity is that **indentation matters** and will throw errors if ignored.
## A few commun built-in functions:
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)
list[key] = calls value associated to key
del list[key]
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
lists of lists....
## Your turn !!
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 = "How old are you ?"
if int(age) > limit:
print("Wow, you're a living fossil !")
else
print("Oh yeah, you're alright.")
```