tech:notes_python_syntaxe
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| tech:notes_python_syntaxe [2025/05/24 12:25] – Jean-Baptiste | tech:notes_python_syntaxe [2026/05/30 21:05] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes Python syntaxe | ||
| + | |||
| + | |||
| + | ## Liste de tous les mots clefs Python | ||
| + | |||
| + | |||
| + | ~~~python | ||
| + | # | ||
| + | |||
| + | import keyword | ||
| + | keyword.kwlist | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | [' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ~~~ | ||
| + | |||
| + | ~~~python | ||
| + | keyword.softkwlist | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | [' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Fonctions | ||
| + | |||
| + | Annotations de fonctions | ||
| + | ~~~python | ||
| + | def func(a: int, b: int, c: int) -> int: | ||
| + | return a + b + c | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Liste des fonctions natives | ||
| + | |||
| + | https:// | ||
| + | |||
| + | |||
| + | ## Syntaxe | ||
| + | |||
| + | |||
| + | ~~~python | ||
| + | kw = dict( | ||
| + | changed=changed, | ||
| + | query=cursor.query, | ||
| + | statusmessage=statusmessage, | ||
| + | query_result=query_result, | ||
| + | rowcount=rowcount if rowcount >= 0 else 0, | ||
| + | ) | ||
| + | |||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | In [7]: rowcount=-1 | ||
| + | |||
| + | In [8]: print(rowcount) if rowcount >= 0 else 0 | ||
| + | Out[8]: 0 | ||
| + | |||
| + | ~~~ | ||
| + | |||
| + | ~~~python | ||
| + | i: int = 10 | ||
| + | s: str = ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ## Lambda | ||
| + | |||
| + | ~~~python | ||
| + | # Simple quote a string | ||
| + | q = lambda s: f"' | ||
| + | |||
| + | # Double quote a string | ||
| + | qq = lambda s: f'" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Cast | ||
| + | |||
| + | String to bool | ||
| + | ~~~python | ||
| + | tls_verify = environ.get(" | ||
| + | |||
| + | # String to bool | ||
| + | tls_verify = tls_verify.lower in (" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ou encore | ||
| + | ~~~python | ||
| + | def str2bool(v): | ||
| + | return v.lower() in (" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Décorateur | ||
| + | |||
| + | Exemple | ||
| + | ~~~python | ||
| + | def decorteur_print(_): | ||
| + | def print_tmp(*args, | ||
| + | print(" | ||
| + | print(*args, | ||
| + | return print_tmp | ||
| + | |||
| + | |||
| + | @decorteur_print | ||
| + | def print2(*args, | ||
| + | print(*args, | ||
| + | |||
| + | | ||
| + | print2(" | ||
| + | ~~~ | ||
| + | |||
| + | Surcharger une fonction | ||
| + | ~~~python | ||
| + | # On copie notre fonction | ||
| + | print_bak = print | ||
| + | |||
| + | def decorteur_print(_): | ||
| + | def print_tmp(*args, | ||
| + | print_bak(" | ||
| + | print_bak(*args, | ||
| + | return print_tmp | ||
| + | |||
| + | @decorteur_print | ||
| + | def print(*args, | ||
| + | print_bak(*args, | ||
| + | |||
| + | print(" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Générateurs | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | ### Yield vs yield from | ||
| + | |||
| + | #### The yield statement | ||
| + | |||
| + | The generator is a function that returns an iterator. Normally a generator function has a yield statement. To define a generator function we need to use a yield statement as shown below: | ||
| + | |||
| + | ~~~python | ||
| + | def generate_number(): | ||
| + | for x in range(10): | ||
| + | yield x | ||
| + | ~~~ | ||
| + | |||
| + | The generate_number has a yield statement, so it is a generator function. Let’s print the values from the generator : | ||
| + | |||
| + | ~~~python | ||
| + | gen = generate_number() | ||
| + | |||
| + | for number in gen: | ||
| + | print(number) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | 0 | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | 5 | ||
| + | 6 | ||
| + | 7 | ||
| + | 8 | ||
| + | 9 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | #### The “yield from” statement | ||
| + | |||
| + | The “yield from” allows us to yield from another generator function or iterables. Let’s understand with some examples. | ||
| + | |||
| + | Consider the following generator function : | ||
| + | ~~~python | ||
| + | def gen_number(): | ||
| + | yield 1 | ||
| + | yield 2 | ||
| + | yield 3 | ||
| + | ~~~ | ||
| + | |||
| + | We can print the generator values using a loop. | ||
| + | ~~~python | ||
| + | for num in gen_number(): | ||
| + | print(num) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | ~~~ | ||
| + | |||
| + | Let’s consider another function(gen_cool_numbers) using the gen_number generator function. | ||
| + | ~~~python | ||
| + | def gen_cool_numbers(): | ||
| + | yield 1000 | ||
| + | for num in gen_number(): | ||
| + | yield num | ||
| + | yield 2000 | ||
| + | |||
| + | for x in gen_cool_numbers(): | ||
| + | print(x) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | 1000 | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 2000 | ||
| + | ~~~ | ||
| + | |||
| + | We can update the gen_cool_numbers function by replacing the for loop with yield from as shown below: | ||
| + | ~~~python | ||
| + | def gen_cool_numbers(): | ||
| + | yield 1000 | ||
| + | yield from gen_number() | ||
| + | yield 2000 | ||
| + | |||
| + | for x in gen_cool_numbers(): | ||
| + | print(x) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | 1000 | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 2000 | ||
| + | ~~~ | ||
| + | |||
| + | Yield from can also be used with iterable(list, | ||
| + | ~~~python | ||
| + | def gen_cool_numbers(): | ||
| + | yield 1000 | ||
| + | yield from [50,60,80] | ||
| + | yield 2000 | ||
| + | |||
| + | for x in gen_cool_numbers(): | ||
| + | print(x) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | 1000 | ||
| + | 50 | ||
| + | 60 | ||
| + | 80 | ||
| + | 2000 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | |||
| + | |||
