fix: print

This commit is contained in:
AngeD 2023-12-14 14:49:05 +01:00
parent 622ea02370
commit 33efef4410

View File

@ -37,12 +37,24 @@ if cond and cond2 or cond3:
a = 2 a = 2
b = 2.45675325 b = 2.45675325
s = "hello" 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 `f""` (on appelle ça une fstring) te permet d'insérer une variable `{var}` dans ta
string avec `{var}` si tu veux la modifier, `{var:mod}`. `mod` est assez proche string. Comme en C, tu peux appliquer des modificateurs sur cette variable
de ce que tu as en C. `{var:mod}`. La syntaxe de mod est très proche de celle du C :
```c
// c
#include <stdio.h>
float b = 2.45675325;
printf("%.2f", b);
```
```python
# python
float b = 2.45675325
print(f"{b:.2f}")
```
## For ## For
1. for (int i = 0; i < 3; i++) 1. for (int i = 0; i < 3; i++)
@ -60,14 +72,17 @@ for c in s:
2. un mixe des deux 2. un mixe des deux
```python ```python
l = [8, 4, 7] l = ["abc", "def", "ghi"]
for i, nb in enumerate(l): for (i, nb) in enumerate(l):
print(i, nb) # affiche "0 8\n1 4\n2 7" 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 ## Tableaux
La syntaxe est un peu bizarre, mais elle permet d'initialiser le tableau avec 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.<br>
Dans tous les exemples, `tab == tab1`.
```python ```python
tab = [[0 for x in range(2)] for y in range(3)] 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 ... ou des strings
```python ```python
s = "abc" 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]
[0, 0, 0] [0, 0, 0]
] ]
``` ```
```python ```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"]
["a", "b", "c"] ["a", "b", "c"]
] ]