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 [2018/07/05 11:48] – [Module get_object] serge | initiation_a_python [2021/08/21 13:41] (Version actuelle) – [PEP8] serge | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ======Initiation à python====== | + | ======Python: |
| - | Ce tuto est très inspiré de: [[https:// | + | |
| - | =====Python===== | + | |
| - | ====Installation==== | + | |
| - | Linux: c'est déjà installé. Win$ et Mac: débrouillez-vous ? | + | |
| - | ====Interpréteur==== | + | <WRAP center round box 80% centeralign> |
| - | Je décide de [[atelier_python_niveau_1# | + | **[[toutes_les_pages_sur_python|Toutes les pages sur Python]] sans les pages sur {{tagpage> |
| + | </ | ||
| + | |||
| + | <WRAP center round box 80% centeralign> | ||
| + | **{{tagpage> | ||
| + | </ | ||
| + | |||
| + | Cet atelier est animé comme un TD. Aucun cours théorique, que de la pratique avec des exemples fun 8-)\\ | ||
| + | Et surtout je ne commence pas par **Chapitre 1 ** POO Programmation Orienté Objet. | ||
| + | |||
| + | Ce tuto est très inspiré de Apprenez à programmer en Python de Vincent Le Goff [[http:// | ||
| + | |||
| + | * **[[https:// | ||
| + | * **[[https:// | ||
| + | |||
| + | =====Les sources complètes de ces TD===== | ||
| + | * https:// | ||
| + | * **[[https:// | ||
| + | |||
| + | =====Installation===== | ||
| + | * **[[python_installation]]** | ||
| =====Quelques commandes===== | =====Quelques commandes===== | ||
| + | ====Tester dans un navigateur==== | ||
| + | * **[[https:// | ||
| + | {{ : | ||
| + | |||
| + | ===Un terminal quelconque=== | ||
| + | Ouvrir un terminator: | ||
| + | {{ : | ||
| + | | ||
| + | =====Premiers pas===== | ||
| + | Ouvrir un terminator | ||
| + | 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 26: | Ligne 53: | ||
| >>> | >>> | ||
| </ | </ | ||
| - | |||
| - | <code python></ | ||
| ===int float string opération=== | ===int float string opération=== | ||
| Ligne 48: | Ligne 73: | ||
| ===Mots clé=== | ===Mots clé=== | ||
| + | * 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 73: | Ligne 100: | ||
| Hello, World! | Hello, World! | ||
| </ | </ | ||
| - | |||
| - | |||
| =====Ouvrir Geany===== | =====Ouvrir Geany===== | ||
| + | ====Geany: un éditeur sympa multiplateforme==== | ||
| + | * **[[python_installation# | ||
| + | ====Premier pas avec geany==== | ||
| Créer un nouveau fichier et l' | Créer un nouveau fichier et l' | ||
| Ligne 99: | Ligne 127: | ||
| print(d + e) | print(d + e) | ||
| - | print(str(a) + e) | + | print(a + e) |
| 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===== | ||
| + | ====si oui si non==== | ||
| <code python> | <code python> | ||
| a = -6 | a = -6 | ||
| Ligne 125: | Ligne 194: | ||
| </ | </ | ||
| + | **Indentation: | ||
| ====Opérateur de comparaison==== | ====Opérateur de comparaison==== | ||
| + | * https:// | ||
| <code python> | <code python> | ||
| == != < > <= >= | == != < > <= >= | ||
| Ligne 132: | Ligne 202: | ||
| + | =====Boucle===== | ||
| + | **Table du 4** | ||
| + | <code python> | ||
| + | print(" | ||
| + | print(" | ||
| + | ... | ||
| + | ... | ||
| + | print(" | ||
| + | </ | ||
| + | **Les répétitions sont très sales !** | ||
| + | <code python> | ||
| + | i = 0 | ||
| + | while i >= 10: | ||
| + | print(str(i) + " x 4 =", i * 4) | ||
| + | i += 1 | ||
| + | </ | ||
| + | **ou encore** | ||
| + | <code python> | ||
| + | for i in range(10): | ||
| + | print(str(i) + " x 4 =", i * 4) | ||
| + | </ | ||
| + | <code python> | ||
| + | for i in [0, 1, 2, 3]: # seulement jusque 3 | ||
| + | print(str(i) + " x 4 =", i * 4) | ||
| + | </ | ||
| + | **Itération** | ||
| + | <code python> | ||
| + | chaine = " | ||
| + | for lettre in chaine: | ||
| + | print(lettre) | ||
| + | </ | ||
| - | =====Exercice Bissextile===== | + | =====Fonctions===== |
| - | Trouver | + | ====Pourquoi faire des fonctions ou des classes ?==== |
| - | < | + | * Pour éviter les répétitions, |
| - | Si une année n'est pas multiple de 4, elle n'est pas bissextile | + | * Pour structurer le projet. |
| - | Si elle est multiple de 4, on regarde si multiple de 100 | + | * Pour tester des bouts de code individuellement. |
| - | | + | * Pour éviter les problèmes avec les variables globales. |
| - | | + | * Pour pouvoir réutiliser des bouts de codes. |
| - | | + | * Permettre un travail en équipe en découpant un projet en bout de projet indépendant. |
| - | Si non, elle est bissextile | + | |
| + | **Une fonction fait une chose. Une class fait une chose** | ||
| + | * [[http:// | ||
| + | |||
| + | ====Simple==== | ||
| + | < | ||
| + | def ma_fonction(): | ||
| + | | ||
| + | |||
| + | def ta_fonction(a): | ||
| + | | ||
| + | |||
| + | def sa_fonction(a, n): | ||
| + | while i < n: | ||
| + | | ||
| + | |||
| + | ma_fonction() | ||
| + | ta_fonction(" | ||
| + | ta_fonction(" | ||
| + | sa_fonction(5, 3) | ||
| </ | </ | ||
| - | Rajouter le lien de la solution | + | ====Arguments par défaut==== |
| - | <file python | + | <code python> |
| - | # Programme testant si une année | + | def fonct(a=1, b=2, c=3): |
| + | print(a, b, c) | ||
| + | |||
| + | fonct() | ||
| + | fonct(10) | ||
| + | fonct(b=10) | ||
| + | </ | ||
| + | |||
| + | ====Return==== | ||
| + | Une fonction sans return retourne None | ||
| + | <code python> | ||
| + | def test1(): | ||
| + | """ | ||
| + | print(" | ||
| + | |||
| + | def test2(): | ||
| + | """ | ||
| + | print(" | ||
| + | return " | ||
| + | |||
| + | a = test1() | ||
| + | print(a) | ||
| + | |||
| + | b = test2() | ||
| + | print(b) | ||
| + | </ | ||
| + | |||
| + | La fonction s' | ||
| + | |||
| + | <code python> | ||
| + | def test(a): | ||
| + | if a < 0: | ||
| + | print(" | ||
| + | return 1 | ||
| + | else: | ||
| + | print(" | ||
| + | return 0 | ||
| + | print(" | ||
| + | |||
| + | test(1) | ||
| + | test(-1) | ||
| + | </ | ||
| + | |||
| + | ====Fonctions prédéfinies==== | ||
| + | <code python> | ||
| + | print( abs(-20), | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | =====Module Package Import===== | ||
| + | ===def=== | ||
| + | Une ou des fonctions dans un fichier | ||
| + | |||
| + | ===Module simple=== | ||
| - | # Demande de l' | + | <file python called.py> |
| - | annee = input("Saisissez votre année ..." | + | def je_suis_appeler(phrase): |
| - | etc ...... | + | print("phrase =", phrase) |
| </ | </ | ||
| - | =====boucle===== | ||
| - | =====Itération===== | + | <file python I_call.py> |
| + | from called import je_suis_appeler | ||
| - | =====Fonctions===== | + | p = "Une phrase exemple à la noix" |
| + | je_suis_appeler(p) | ||
| + | </ | ||
| - | ===Return=== | + | Exécuter: |
| + | python3 I_call.py | ||
| - | =====Module===== | + | ===Bibliothèque standard=== |
| + | * **[[https:// | ||
| - | =====Module get object===== | + | ===time=== |
| + | <code python> | ||
| + | import time | ||
| - | [[https:// | + | print(dir(time)) |
| + | print(" | ||
| + | </code> | ||
| - | à utiliser dans blender | + | <code python> |
| - | =====Package===== | + | import datetime |
| + | |||
| + | print(dir(datetime)) | ||
| + | print(" | ||
| + | </ | ||
| =====Exception===== | =====Exception===== | ||
| + | <code python> | ||
| + | try: | ||
| + | resp = 1 / 0 | ||
| + | except: | ||
| + | print(" | ||
| + | |||
| + | try: | ||
| + | resp = 1 / 0 | ||
| + | except ZeroDivisionError as e: | ||
| + | print(e) | ||
| + | </ | ||
| + | |||
| + | **[[https:// | ||
| + | ===Pass=== | ||
| + | **Du sale** | ||
| + | <code python> | ||
| + | try: | ||
| + | resp = 1 / 0 | ||
| + | return resp | ||
| + | except: | ||
| + | pass | ||
| + | </ | ||
| + | **Du propre** | ||
| + | Ne jamais passer les erreurs sous silence, ou les faire taire explicitement. | ||
| + | <code python> | ||
| + | def exemple_propre(a): | ||
| + | try: | ||
| + | return 1 / a | ||
| + | except ZeroDivisionError as e: | ||
| + | print(e) | ||
| + | return None | ||
| + | except: | ||
| + | return None | ||
| + | </ | ||
| + | |||
| + | **Pass pour du code en construction** | ||
| + | <code python> | ||
| + | def je_multiplie(): | ||
| + | pass | ||
| + | | ||
| + | def j_imprime(): | ||
| + | pass | ||
| + | </ | ||
| + | |||
| + | =====Type===== | ||
| + | Python est fortement typé: il utilise le **[[https:// | ||
| + | |||
| + | <code python> | ||
| + | t = type({1, 2, 3, 4, 2, 1}) | ||
| + | print(t) | ||
| + | </ | ||
| + | =====Variables globales et locales, espaces de noms===== | ||
| + | Le fichier pi.py n'a rien à voir avec pi ou PI ou pi() | ||
| + | |||
| + | <code python pi.py> | ||
| + | def pi(): | ||
| + | return " | ||
| + | </ | ||
| + | |||
| + | <code python jeu.py> | ||
| + | from pi import pi as pipi | ||
| + | |||
| + | # Toutes les variables globales en majuscule | ||
| + | PI = 3.14 | ||
| + | |||
| + | # Ce pi est une variable globale | ||
| + | pi = pipi() | ||
| + | |||
| + | def print_pi(pi): | ||
| + | """ | ||
| + | print(pi) | ||
| + | | ||
| + | def print_a(a): | ||
| + | print(a) | ||
| + | | ||
| + | print_pi(3) | ||
| + | print_a(3) | ||
| + | print_pi(PI) | ||
| + | print_pi(pi) | ||
| + | print_pi() | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====POO===== | ||
| + | |||
| + | ====Programmation Orientée Objet==== | ||
| + | **[[https:// | ||
| + | |||
| + | ====Pourquoi des class ?==== | ||
| + | **[[initiation_a_python# | ||
| + | |||
| + | ====Les class permettent de créer des objets==== | ||
| + | <code python> | ||
| + | class Subliminale: | ||
| + | pass | ||
| + | |||
| + | class Minimale: | ||
| + | print(" | ||
| + | |||
| + | class MyTest: | ||
| + | def my_test(self): | ||
| + | print(" | ||
| + | |||
| + | class Bicycle: | ||
| + | """ | ||
| + | |||
| + | def __init__(self, | ||
| + | """ | ||
| + | | ||
| + | self.color = color | ||
| + | self.kind = kind | ||
| + | |||
| + | s = Subliminale() | ||
| + | print(type(s)) | ||
| + | |||
| + | m = Minimale() | ||
| + | |||
| + | b = Bicycle(" | ||
| + | print(b.color) | ||
| + | print(b.kind) | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | =====La philosophie Python===== | ||
| + | ====Zen of python==== | ||
| + | Dans l' | ||
| + | <code python> | ||
| + | import this | ||
| + | </ | ||
| + | <code text> | ||
| + | Préfère : | ||
| + | la beauté à la laideur, | ||
| + | l' | ||
| + | le simple au complexe | ||
| + | et le complexe au compliqué, | ||
| + | le déroulé à l' | ||
| + | l' | ||
| + | Prends en compte la lisibilité. | ||
| + | Les cas particuliers ne le sont jamais assez pour violer les règles. | ||
| + | Mais, à la pureté, privilégie l' | ||
| + | Ne passe pas les erreurs sous silence, | ||
| + | ... ou bâillonne-les explicitement. | ||
| + | Face à l' | ||
| + | Sache qu'il ne devrait y avoir qu'une et une seule façon de procéder, | ||
| + | même si, de prime abord, elle n'est pas évidente, à moins d' | ||
| + | Mieux vaut maintenant que jamais. | ||
| + | Cependant jamais est souvent mieux qu' | ||
| + | Si l' | ||
| + | Si l' | ||
| + | Les espaces de nommage ! Sacrée bonne idée ! Faisons plus de trucs comme ça. </ | ||
| + | ====PEP8===== | ||
| + | |||
| + | * **[[https:// | ||
| + | |||
| + | |||
| + | **Avec python, nous sommes toujours entre adultes responsables.** | ||
| + | * C'est toujours possible d’enfreindre une règle. | ||
| + | * Par contre, certaines règles sont obligatoires: | ||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | =====Recherche web===== | ||
| + | Dans un moteur de recherche, toujours en anglais, par exemple: | ||
| + | python3 download web page | ||
| + | |||
| + | Une grande majorité des réponses sera sur **[[https:// | ||
| - | =====Pass===== | ||
| - | {{tag> | + | {{tag> |
| - | {{tag> | + | |
initiation_a_python.1530791287.txt.gz · Dernière modification : de serge
