initiation_a_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| initiation_a_python [2021/04/16 10:40] – serge | initiation_a_python [2021/08/21 13:41] (Version actuelle) – [PEP8] serge | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ======Python: | ======Python: | ||
| + | |||
| + | <WRAP center round box 80% centeralign> | ||
| + | **[[toutes_les_pages_sur_python|Toutes les pages sur Python]] sans les pages sur {{tagpage> | ||
| + | </ | ||
| + | |||
| <WRAP center round box 80% centeralign> | <WRAP center round box 80% centeralign> | ||
| Ligne 8: | Ligne 13: | ||
| Et surtout je ne commence pas par **Chapitre 1 ** POO Programmation Orienté Objet. | Et surtout je ne commence pas par **Chapitre 1 ** POO Programmation Orienté Objet. | ||
| - | Ce tuto est très inspiré de [[https:// | + | Ce tuto est très inspiré de Apprenez à programmer en Python de Vincent Le Goff [[http:// |
| - | + | ||
| - | Cette page est la suite de [[Atelier Python Niveau 1]]. | + | |
| * **[[https:// | * **[[https:// | ||
| * **[[https:// | * **[[https:// | ||
| + | =====Les sources complètes de ces TD===== | ||
| + | * https:// | ||
| + | * **[[https:// | ||
| + | |||
| + | =====Installation===== | ||
| + | * **[[python_installation]]** | ||
| =====Quelques commandes===== | =====Quelques commandes===== | ||
| - | ====Tester | + | ====Tester |
| - | ===bpython=== | + | * **[[https:// |
| - | Avec la coloration syntaxique et l' | + | {{ : |
| - | * [[https:// | + | ===Un terminal quelconque=== |
| - | + | Ouvrir un terminator: | |
| - | sudo apt install bpython3 | + | {{ : |
| | | ||
| - | ===Un terminal quelconque=== | + | =====Premiers pas===== |
| - | Ouvrir un terminal: | + | Ouvrir un terminator |
| - | | + | bpython3 |
| - | + | ||
| - | Python 3.5.3 (default, Jan 19 2017, 14:11:04) | + | |
| - | [GCC 6.3.0 20170118] on linux | + | |
| - | Type " | + | |
| - | >>> | + | |
| - | Vous êtes dans Iddle ! | + | |
| - | + | ||
| - | ===bpython3=== | + | |
| <code python> | <code python> | ||
| bpython version 0.16 on top of Python 3.5.3 / | bpython version 0.16 on top of Python 3.5.3 / | ||
| Ligne 52: | Ligne 53: | ||
| >>> | >>> | ||
| </ | </ | ||
| + | |||
| ===int float string opération=== | ===int float string opération=== | ||
| <code python> | <code python> | ||
| Ligne 73: | Ligne 75: | ||
| * https:// | * https:// | ||
| <code python> | <code python> | ||
| - | and as class def elif else except | + | False |
| - | lambda | + | None break except |
| + | True class finally | ||
| + | and continue | ||
| + | as | ||
| + | assert | ||
| + | async elif | ||
| </ | </ | ||
| - | ===Boolean et rien=== | ||
| - | <code python> | ||
| - | True False None | ||
| - | </ | ||
| =====Type de données===== | =====Type de données===== | ||
| <code python> | <code python> | ||
| 1 # int | 1 # int | ||
| 3.14 # float | 3.14 # float | ||
| - | "Labbomedia" | + | "Labomedia" |
| [1, 2, 3] # list | [1, 2, 3] # list | ||
| {" | {" | ||
| </ | </ | ||
| - | =====Fonction===== | ||
| + | =====Fonction===== | ||
| <code python> | <code python> | ||
| >>> | >>> | ||
| Ligne 97: | Ligne 100: | ||
| Hello, World! | Hello, World! | ||
| </ | </ | ||
| - | |||
| - | |||
| =====Ouvrir Geany===== | =====Ouvrir Geany===== | ||
| ====Geany: un éditeur sympa multiplateforme==== | ====Geany: un éditeur sympa multiplateforme==== | ||
| - | * [[https:// | + | |
| - | + | ||
| - | Dans le dossier / | + | |
| - | + | ||
| - | puis coller le dossier **{{: | + | |
| ====Premier pas avec geany==== | ====Premier pas avec geany==== | ||
| Créer un nouveau fichier et l' | Créer un nouveau fichier et l' | ||
| Ligne 134: | Ligne 130: | ||
| print(a+e) | print(a+e) | ||
| + | |||
| + | print(f" | ||
| </ | </ | ||
| + | |||
| + | |||
| + | =====Les différents types de variable===== | ||
| + | ====Liste==== | ||
| + | <code python> | ||
| + | l = [1, 2, 3, 4, 5] | ||
| + | # On compte à partir de zéro | ||
| + | l[1] = 2 | ||
| + | </ | ||
| + | * [[http:// | ||
| + | * [[https:// | ||
| + | ====Dictionnaire==== | ||
| + | <code python> | ||
| + | d = {1: " | ||
| + | # Les clés doivent être immuables | ||
| + | d[3] = " | ||
| + | print(d) | ||
| + | </ | ||
| + | |||
| + | [[http:// | ||
| + | |||
| + | ====Tuples==== | ||
| + | <code python> | ||
| + | t = (" | ||
| + | print(t) | ||
| + | t = " | ||
| + | print(t) | ||
| + | </ | ||
| + | |||
| + | [[http:// | ||
| + | |||
| + | ====Set==== | ||
| + | Liste toutes les valeurs existantes, il n'y a pas de doublons. | ||
| + | <code python> | ||
| + | my_set = {1, 2, 3, 4, 2, 1} | ||
| + | print(my_set) | ||
| + | </ | ||
| + | |||
| =====Condition===== | =====Condition===== | ||
| Ligne 164: | Ligne 200: | ||
| == != < > <= >= | == != < > <= >= | ||
| </ | </ | ||
| - | |||
| - | |||
| - | |||
| - | =====Exercice Bissextile===== | ||
| - | Trouver si une année est bissextile: | ||
| - | <code text> | ||
| - | Si une année n'est pas multiple de 4, elle n'est pas bissextile | ||
| - | Si elle est multiple de 4, on regarde si multiple de 100 | ||
| - | Si oui, on regarde si multiple de 400 | ||
| - | Si oui, elle est bissextile | ||
| - | Si non, elle n'est pas bissextile | ||
| - | Si non, elle est bissextile | ||
| - | </ | ||
| - | |||
| - | <file python bissextile.py> | ||
| - | # Programme testant si une année est bissextile | ||
| - | |||
| - | # Demande de l' | ||
| - | annee = input(" | ||
| - | etc ...... | ||
| - | </ | ||
| - | |||
| - | Deux réponses: **[[https:// | ||
| - | |||
| =====Boucle===== | =====Boucle===== | ||
| - | Table du 4: | + | **Table du 4** |
| <code python> | <code python> | ||
| print(" | print(" | ||
| Ligne 199: | Ligne 211: | ||
| print(" | print(" | ||
| </ | </ | ||
| - | Les répétitions sont très sales ! | + | **Les répétitions sont très sales !** |
| <code python> | <code python> | ||
| i = 0 | i = 0 | ||
| Ligne 206: | Ligne 218: | ||
| i += 1 | i += 1 | ||
| </ | </ | ||
| - | ou encore | + | **ou encore** |
| <code python> | <code python> | ||
| for i in range(10): | for i in range(10): | ||
| Ligne 215: | Ligne 227: | ||
| print(str(i) + " x 4 =", i * 4) | print(str(i) + " x 4 =", i * 4) | ||
| </ | </ | ||
| - | =====Itération===== | + | **Itération** |
| <code python> | <code python> | ||
| chaine = " | chaine = " | ||
| Ligne 221: | Ligne 233: | ||
| print(lettre) | print(lettre) | ||
| </ | </ | ||
| + | |||
| =====Fonctions===== | =====Fonctions===== | ||
| ====Pourquoi faire des fonctions ou des classes ?==== | ====Pourquoi faire des fonctions ou des classes ?==== | ||
| Ligne 230: | Ligne 243: | ||
| * Permettre un travail en équipe en découpant un projet en bout de projet indépendant. | * Permettre un travail en équipe en découpant un projet en bout de projet indépendant. | ||
| - | ====Une fonction fait une chose. Une class fait une chose==== | + | **Une fonction fait une chose. Une class fait une chose** |
| * [[http:// | * [[http:// | ||
| Ligne 298: | Ligne 311: | ||
| ====Fonctions prédéfinies==== | ====Fonctions prédéfinies==== | ||
| <code python> | <code python> | ||
| - | print( | + | print( abs(-20), |
| - | abs(-20), | + | |
| | | ||
| | | ||
| - | | + | |
| - | | + | dir(1)) |
| </ | </ | ||
| - | =====Module Package===== | + | =====Module Package |
| ===def=== | ===def=== | ||
| Une ou des fonctions dans un fichier est un module. Plusieurs modules font un package. | Une ou des fonctions dans un fichier est un module. Plusieurs modules font un package. | ||
| Ligne 326: | Ligne 338: | ||
| python3 I_call.py | python3 I_call.py | ||
| - | ===time=== | + | ===Bibliothèque standard=== |
| + | * **[[https:// | ||
| + | ===time=== | ||
| <code python> | <code python> | ||
| import time | import time | ||
| Ligne 341: | Ligne 355: | ||
| print(" | print(" | ||
| </ | </ | ||
| - | |||
| - | ===Bibliothèque standard=== | ||
| - | * **[[https:// | ||
| =====Exception===== | =====Exception===== | ||
| - | |||
| <code python> | <code python> | ||
| try: | try: | ||
| Ligne 360: | Ligne 370: | ||
| **[[https:// | **[[https:// | ||
| - | =====Pass===== | + | ===Pass=== |
| - | ====Du sale==== | + | **Du sale** |
| <code python> | <code python> | ||
| try: | try: | ||
| Ligne 369: | Ligne 379: | ||
| pass | pass | ||
| </ | </ | ||
| - | + | **Du propre** | |
| - | ====Du propre==== | + | Ne jamais passer les erreurs sous silence, ou les faire taire explicitement. |
| - | Ne jamais passer les erreurs sous silence,\\ | + | |
| - | ou les faire taire explicitement. | + | |
| - | | + | |
| <code python> | <code python> | ||
| def exemple_propre(a): | def exemple_propre(a): | ||
| Ligne 385: | Ligne 392: | ||
| </ | </ | ||
| - | ====Pass pour du code en construction | + | **Pass pour du code en construction** |
| <code python> | <code python> | ||
| def je_multiplie(): | def je_multiplie(): | ||
| Ligne 392: | Ligne 399: | ||
| def j_imprime(): | def j_imprime(): | ||
| pass | pass | ||
| - | </ | ||
| - | =====Les différents types de variable===== | ||
| - | ====Liste==== | ||
| - | <code python> | ||
| - | l = [1, 2, 3, 4, 5] | ||
| - | # On compte à partir de zéro | ||
| - | l[1] = 2 | ||
| - | </ | ||
| - | * [[http:// | ||
| - | * [[https:// | ||
| - | ====Dictionnaire==== | ||
| - | <code python> | ||
| - | d = {1: " | ||
| - | # Les clés doivent être immuables | ||
| - | d[3] = " | ||
| - | print(d) | ||
| - | </ | ||
| - | |||
| - | [[http:// | ||
| - | |||
| - | ====Tuples==== | ||
| - | <code python> | ||
| - | t = (" | ||
| - | print(t) | ||
| - | t = " | ||
| - | print(t) | ||
| - | </ | ||
| - | |||
| - | [[http:// | ||
| - | |||
| - | ====Set==== | ||
| - | Je n' | ||
| - | <code python> | ||
| - | my_set = {1, 2, 3, 4, 2, 1} | ||
| - | print(my_set) | ||
| </ | </ | ||
| Ligne 469: | Ligne 441: | ||
| =====POO===== | =====POO===== | ||
| - | ====Dans le Blender Game Engine==== | + | |
| - | Seuls les programmeurs experts créent des class dans de gros projets. | + | |
| - | La suite ne concerne que les curieux. | + | |
| ====Programmation Orientée Objet==== | ====Programmation Orientée Objet==== | ||
| **[[https:// | **[[https:// | ||
| Ligne 508: | Ligne 478: | ||
| print(b.kind) | print(b.kind) | ||
| - | class Contact: | ||
| - | |||
| - | def __init__(self, | ||
| - | self.name = name | ||
| - | | ||
| - | def set_phone(phone): | ||
| - | self.phone = phone | ||
| - | | ||
| - | def get_phone(self): | ||
| - | return self.phone | ||
| - | |||
| - | c = Contact(" | ||
| - | print(c.get_phone()) | ||
| </ | </ | ||
| - | [[https:// | ||
| - | |||
| - | http: | ||
| - | [[https:// | ||
| =====La philosophie Python===== | =====La philosophie Python===== | ||
| ====Zen of python==== | ====Zen of python==== | ||
| Ligne 557: | Ligne 510: | ||
| ====PEP8===== | ====PEP8===== | ||
| - | * [[http://sametmax.com/le-pep8-en-resume/|un tuto parmi des tas sur @]] chez sametmax.com | + | |
| **Avec python, nous sommes toujours entre adultes responsables.** | **Avec python, nous sommes toujours entre adultes responsables.** | ||
| Ligne 570: | Ligne 524: | ||
| Une grande majorité des réponses sera sur **[[https:// | Une grande majorité des réponses sera sur **[[https:// | ||
| - | |||
| - | =====Scripts python dans le Blender Game Engine===== | ||
| - | Voir la page [[Scripts python dans le Blender Game Engine]] | ||
| - | |||
| - | =====Le script python parfait===== | ||
| - | * [[le_script_python_parfait|]] | ||
| {{tag> atelier_python python sb }} | {{tag> atelier_python python sb }} | ||
initiation_a_python.1618569607.txt.gz · Dernière modification : de serge
