Initial commit
This commit is contained in:
commit
12d0d9dc67
100
python.md
Normal file
100
python.md
Normal file
@ -0,0 +1,100 @@
|
||||
# Python
|
||||
Hésite pas à regarder des repos déjà finis sur GitHub. Ce n'est pas de la
|
||||
triche si tu t'inspires de leur algo tant que tu ne copies pas le code.
|
||||
|
||||
## man en python
|
||||
Tu peux lancer un shell dans un terminal en lançant `python` dans ton terminal
|
||||
De la, `help(ord)` pour avoir le man de `ord` et `exit()` pour quitter.
|
||||
```bash
|
||||
$ python
|
||||
>>> help(ord)
|
||||
>>> exit()
|
||||
```
|
||||
|
||||
Si tu veux le man de math.ceil, il faut d'abord importer math
|
||||
```bash
|
||||
$ python
|
||||
>>> import math
|
||||
>>> help(math.ceil)
|
||||
>>> exit()
|
||||
```
|
||||
|
||||
Tu as des mans pour tous les types aussi pour savoir ce que tu peux faire
|
||||
avec : `help(int)`, `help(list)`, `help(str)`.
|
||||
Toutes les fonctions __something__ sont des fonction spéciales. Par ex, __add__
|
||||
gère le comportement si tu fais un `+` ("abc" + "def" == "abcdef"), c'est pas
|
||||
hyper important si tu comprends pas cette partie pour l'instant. Par contre
|
||||
`"abc".endswith()` ou `["a", "b"].append()` pourraient t'être utiles.
|
||||
|
||||
## Condition
|
||||
```python
|
||||
if cond and cond2 or cond3:
|
||||
print(hello)
|
||||
```
|
||||
|
||||
## Printf mais mieux
|
||||
```python
|
||||
a = 2
|
||||
b = 2.45675325
|
||||
s = "hello"
|
||||
print(f"{a}, {b:.2f}, {s}")
|
||||
```
|
||||
|
||||
`f""` (on appelle ça une fstring) te permet de rajouter une variable dans ta
|
||||
string avec `{nom_variable}` si tu veux la modifier,
|
||||
`{nom_variable:modificateur}`. `modificateur` est assez proche de ce que tu as
|
||||
en C.
|
||||
|
||||
## Tableaux
|
||||
La syntaxe est un peu bizarre, mais elle permet d'initialiser le tableau avec
|
||||
ce que tu veux.
|
||||
```python
|
||||
tab = [[0 for x in range(2)] for y in range(3)]
|
||||
|
||||
tab1 = [
|
||||
[0, 0]
|
||||
[0, 0]
|
||||
[0, 0]
|
||||
]
|
||||
```
|
||||
tab == tab1
|
||||
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)]
|
||||
|
||||
tab2 = [
|
||||
[0, 0, 0]
|
||||
[0, 0, 0]
|
||||
]
|
||||
```
|
||||
|
||||
```python
|
||||
tab = [[x for x in "abc"] for _ in range(2)]
|
||||
|
||||
tab2 = [
|
||||
["a", "b", "c"]
|
||||
["a", "b", "c"]
|
||||
]
|
||||
```
|
||||
|
||||
Tu peux aussi modifier la taille de ton tableau après coup avec tab.append()
|
||||
et tab.pop().
|
||||
|
||||
## Conversions
|
||||
```python
|
||||
a = int("2") # a = 2
|
||||
b = str(a) # b = "2"
|
||||
```
|
||||
|
||||
## Gestion d'erreur
|
||||
```python
|
||||
try:
|
||||
a = int("z")
|
||||
except:
|
||||
a = 0
|
||||
```
|
||||
|
||||
Si "z" n'est pas un int valide, le code dans `except` sera exécuté.
|
Loading…
Reference in New Issue
Block a user