Python initiation : relecture.
This commit is contained in:
parent
ae9bdf13fc
commit
f8818bff93
|
@ -1,8 +1,22 @@
|
||||||
# Python
|
# Python
|
||||||
|
|
||||||
|
Par
|
||||||
|
|
||||||
|
<!-- .slide: data-background="static/background.jpg" -->
|
||||||
|
|
||||||
|
Julien Palard <julien@palard.fr>
|
||||||
|
|
||||||
|
https://mdk.fr
|
||||||
|
|
||||||
notes:
|
notes:
|
||||||
|
|
||||||
Juste pour le doctest :
|
|
||||||
|
Introduce yourself!
|
||||||
|
|
||||||
|
Ça couvre les types de bases survol quelques strucutres de contrôle,
|
||||||
|
et quelques fonctions natives.
|
||||||
|
|
||||||
|
Et juste pour le doctest :
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import random
|
import random
|
||||||
|
@ -23,22 +37,6 @@ recettes = [
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Python
|
|
||||||
|
|
||||||
par
|
|
||||||
|
|
||||||
Julien Palard <julien@palard.fr>
|
|
||||||
|
|
||||||
https://mdk.fr
|
|
||||||
|
|
||||||
notes:
|
|
||||||
|
|
||||||
Introduce yourself!
|
|
||||||
|
|
||||||
Ça couvre les types de bases survol quelques strucutres de contrôle,
|
|
||||||
et quelques fonctions natives.
|
|
||||||
|
|
||||||
|
|
||||||
## Python : Introduction
|
## Python : Introduction
|
||||||
|
|
||||||
Python est un langage de programmation permettant de s'exprimer de
|
Python est un langage de programmation permettant de s'exprimer de
|
||||||
|
@ -49,16 +47,27 @@ manière **concise** et **lisible**.
|
||||||
|
|
||||||
YouTube, Dropbox, Reddit, Instagram, Spotify, NASA…
|
YouTube, Dropbox, Reddit, Instagram, Spotify, NASA…
|
||||||
|
|
||||||
![](static/Black-hole-M87.jpg)
|
<img src="static/Black-hole-M87.jpg" style="width:60%">
|
||||||
|
|
||||||
|
|
||||||
## Combien utilisent Python ?
|
## Combien ?
|
||||||
|
|
||||||
![](static/Evolution_of_Python.png)
|
<a href="https://git.afpy.org/mdk/python-versions">
|
||||||
|
<img src="https://git.afpy.org/mdk/python-versions/media/branch/main/pypi-download-counts.png" style="width:90%">
|
||||||
|
</a>
|
||||||
|
|
||||||
::: notes
|
::: notes
|
||||||
|
|
||||||
https://insights.stackoverflow.com/trends
|
Oui ça fait ~175TB/jour sur PyPI.org.
|
||||||
|
|
||||||
|
https://insights.stackoverflow.com/trends baisse en 2023, pourquoi ?
|
||||||
|
|
||||||
|
|
||||||
|
## Quelle version ?
|
||||||
|
|
||||||
|
<a href="https://git.afpy.org/mdk/python-versions">
|
||||||
|
<img src="https://git.afpy.org/mdk/python-versions/media/branch/main/python-versions-lines.png" style="width:90%">
|
||||||
|
</a>
|
||||||
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
@ -424,7 +433,7 @@ True
|
||||||
## Travailler avec les ensembles
|
## Travailler avec les ensembles
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> {"farine", "sel"} | {"œuf", "sel"} == {"sel", "farine", "œuf"}
|
>>> {"farine"} | {"œuf", "sucre"} == {"sucre","farine","œuf"}
|
||||||
True
|
True
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -455,16 +464,6 @@ TypeError: can't multiply sequence by non-int of type 'str'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Mais en cas d'ambiguité…
|
|
||||||
|
|
||||||
```pycon
|
|
||||||
>>> {"farine", "levure"} + {"sel", "œuf"}
|
|
||||||
Traceback (most recent call last):
|
|
||||||
File "<stdin>", line 1, in <module>
|
|
||||||
TypeError: unsupported operand type(s) for +: 'set' and 'set'
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
# Les variables
|
# Les variables
|
||||||
|
|
||||||
## Affectation
|
## Affectation
|
||||||
|
@ -619,7 +618,7 @@ chaînes.
|
||||||
['farine', 'sel', 'sucre']
|
['farine', 'sel', 'sucre']
|
||||||
```
|
```
|
||||||
|
|
||||||
## Les importer des modules
|
## importer des modules
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> from random import choice
|
>>> from random import choice
|
||||||
|
@ -708,10 +707,8 @@ sel
|
||||||
>>> ingredient = ingredients[1]
|
>>> ingredient = ingredients[1]
|
||||||
>>> print(ingredient)
|
>>> print(ingredient)
|
||||||
farine
|
farine
|
||||||
>>> ingredient = ingredients[2]
|
|
||||||
>>> print(ingredient)
|
|
||||||
sucre
|
|
||||||
```
|
```
|
||||||
|
…
|
||||||
|
|
||||||
|
|
||||||
## for
|
## for
|
||||||
|
@ -1254,10 +1251,10 @@ préciser peut être : "dont on connaît à toutes les étapes du programme".
|
||||||
## Exemple
|
## Exemple
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Point:
|
class Recette:
|
||||||
def __init__(self, x, y):
|
def __init__(self, nom, ingrédients):
|
||||||
self.x = x
|
self.nom = nom
|
||||||
self.y = y
|
self.ingrédients = ingrédients
|
||||||
```
|
```
|
||||||
|
|
||||||
Notes: On connaît les attributs.
|
Notes: On connaît les attributs.
|
||||||
|
@ -1266,10 +1263,11 @@ Notes: On connaît les attributs.
|
||||||
## Exemple
|
## Exemple
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> ISIN = {
|
>>> ingrédients = {
|
||||||
... "GLE": "FR0000130809",
|
... 'farine': 1000,
|
||||||
... "PARRO": "FR0004038263",
|
... 'eau': 615,
|
||||||
... "AM": "FR0000121725",
|
... 'levure': 10,
|
||||||
|
... 'sel': 20
|
||||||
... }
|
... }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1399,12 +1397,13 @@ Notes: C'est l'occasion de parler de assert.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from hypothesis import given
|
from hypothesis import given
|
||||||
from hypothesis.strategies import integers
|
from hypothesis.strategies import floats
|
||||||
|
|
||||||
|
@given(floats())
|
||||||
|
def test_temperature_conversions(temp):
|
||||||
|
assert fahrenheit_to_celsius(
|
||||||
|
celsius_to_fahrenheit(temp)) == temp
|
||||||
|
|
||||||
@given(integers(min_value=2,
|
|
||||||
max_value=1000))
|
|
||||||
def test_fib(i):
|
|
||||||
assert fib(i) == fib(i-1) + fib(i-2)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -1425,15 +1424,6 @@ Notes: Prérequis: pip et venv.
|
||||||
Pas plus de 7.
|
Pas plus de 7.
|
||||||
|
|
||||||
|
|
||||||
## Garder son code lisible
|
|
||||||
|
|
||||||
Attention au code vieillissant, au « je ne rajoute qu'une ligne ou
|
|
||||||
deux à cette fonction et c'est réglé ».
|
|
||||||
|
|
||||||
Notes: Deux ans après la fonction fait 800 lignes, et personne ne l'a
|
|
||||||
vu venir. flake8 peut aider.
|
|
||||||
|
|
||||||
|
|
||||||
## Les « linters »
|
## Les « linters »
|
||||||
|
|
||||||
Il existe plusieurs outils pour « relire » votre code :
|
Il existe plusieurs outils pour « relire » votre code :
|
||||||
|
@ -1518,8 +1508,6 @@ breakpoint()
|
||||||
|
|
||||||
## PYTHONDEVMODE=y
|
## PYTHONDEVMODE=y
|
||||||
|
|
||||||
Et `./configure --with-pydebug`.
|
|
||||||
|
|
||||||
Notes: Voir mon bashrc :] Surtout "viable" depuis la 3.8.
|
Notes: Voir mon bashrc :] Surtout "viable" depuis la 3.8.
|
||||||
|
|
||||||
|
|
||||||
|
@ -1625,10 +1613,10 @@ y → 12
|
||||||
C'est transformer une chaîne en octets, pour son transport ou stockage :
|
C'est transformer une chaîne en octets, pour son transport ou stockage :
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> "L'été".encode()
|
>>> "Pâte à crêpe".encode()
|
||||||
b"L'\xc3\xa9t\xc3\xa9"
|
b'P\xc3\xa2te \xc3\xa0 cr\xc3\xaape'
|
||||||
>>> list("L'été".encode())
|
>>> list("Pâte à crêpe".encode())
|
||||||
[76, 39, 195, 169, 116, 195, 169]
|
[80, 195, 162, 116, 101, 32, 195, 160, 32, 99, 114, 195, 170, 112, 101]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -1637,8 +1625,8 @@ b"L'\xc3\xa9t\xc3\xa9"
|
||||||
C'est le contraire ...
|
C'est le contraire ...
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> bytes([76, 39, 195, 169, 116, 195, 169]).decode()
|
>>> b'P\xc3\xa2te \xc3\xa0 cr\xc3\xaape'.decode()
|
||||||
"L'été"
|
'Pâte à crêpe'
|
||||||
```
|
```
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
|
@ -1666,4 +1654,7 @@ variable.
|
||||||
|
|
||||||
## Les PyCons
|
## Les PyCons
|
||||||
|
|
||||||
La PyConFr (tous les ans, sauf cette année) ! Les meetups locaux hors période de pandémie !
|
La PyConFR (tous les ans) ! Les meetups locaux !
|
||||||
|
|
||||||
|
- https://pycon.fr
|
||||||
|
- https://discuss.afpy.org/c/meetups/12
|
||||||
|
|
Loading…
Reference in New Issue
Block a user