diff --git a/python.md b/python.md index 0ea16f0..9b623c5 100644 --- a/python.md +++ b/python.md @@ -37,12 +37,24 @@ if cond and cond2 or cond3: a = 2 b = 2.45675325 s = "hello" -print(f"{a}, {b:.2f}, {s}") +print(f"a = {a}, b = {b:.2f}, s = {s}") # affiche "a = 2, b = 2.45, s = hello" ``` -`f""` (on appelle ça une fstring) te permet de rajouter une variable dans ta -string avec `{var}` si tu veux la modifier, `{var:mod}`. `mod` est assez proche -de ce que tu as en C. +`f""` (on appelle ça une fstring) te permet d'insérer une variable `{var}` dans ta +string. Comme en C, tu peux appliquer des modificateurs sur cette variable +`{var:mod}`. La syntaxe de mod est très proche de celle du C : +```c +// c +#include +float b = 2.45675325; +printf("%.2f", b); +``` + +```python +# python +float b = 2.45675325 +print(f"{b:.2f}") +``` ## For 1. for (int i = 0; i < 3; i++) @@ -60,14 +72,17 @@ for c in s: 2. un mixe des deux ```python -l = [8, 4, 7] -for i, nb in enumerate(l): - print(i, nb) # affiche "0 8\n1 4\n2 7" +l = ["abc", "def", "ghi"] +for (i, nb) in enumerate(l): + print(i, nb) # affiche "0 abc\n1 def\n2 ghi" ``` +Pour chaque tour de boucle, `enumerate` te donne deux variables : l'index et +la valeur (`i` et `str[i]`) ## Tableaux La syntaxe est un peu bizarre, mais elle permet d'initialiser le tableau avec -ce que tu veux. Dans tous les exemples, `tab == tab1`. +ce que tu veux.
+Dans tous les exemples, `tab == tab1`. ```python tab = [[0 for x in range(2)] for y in range(3)] @@ -82,18 +97,18 @@ Pas de gestion de la ram en python, donc 2 et 3 peuvent être des variables. ... ou des strings ```python s = "abc" -tab = [[0 for x in s] for _ in range(2)] +tab = [[0 for x in s] for y in range(2)] -tab2 = [ +tab1 = [ [0, 0, 0] [0, 0, 0] ] ``` ```python -tab = [[x for x in "abc"] for _ in range(2)] +tab = [[x for x in "abc"] for y in range(2)] -tab2 = [ +tab1 = [ ["a", "b", "c"] ["a", "b", "c"] ]