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édenteDernière révisionLes deux révisions suivantes | ||
initiation_a_python [2018/07/09 13:54] – [Du propre] serge | initiation_a_python [2021/08/21 13:28] – [Excercices] serge | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ======Initiation à python====== | + | ======Python: |
- | Ce tuto est très inspiré de: [[https:// | + | |
- | Cette page est la suite de [[Atelier | + | <WRAP center round box 80% centeralign> |
+ | **[[toutes_les_pages_sur_python|Toutes les pages sur Python]] | ||
+ | </ | ||
+ | |||
+ | <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 21: | Ligne 52: | ||
' | ' | ||
>>> | >>> | ||
+ | </ | ||
===int float string opération=== | ===int float string opération=== | ||
Ligne 41: | 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 66: | 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 95: | 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 119: | Ligne 194: | ||
</ | </ | ||
+ | **Indentation: | ||
====Opérateur de comparaison==== | ====Opérateur de comparaison==== | ||
+ | * https:// | ||
<code python> | <code python> | ||
== != < > <= >= | == != < > <= >= | ||
</ | </ | ||
- | |||
- | |||
- | |||
- | =====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 159: | 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 166: | 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 175: | 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 181: | Ligne 233: | ||
print(lettre) | print(lettre) | ||
</ | </ | ||
+ | |||
=====Fonctions===== | =====Fonctions===== | ||
====Pourquoi faire des fonctions ou des classes ?==== | ====Pourquoi faire des fonctions ou des classes ?==== | ||
Ligne 190: | 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** | ||
+ | * [[http:// | ||
====Simple==== | ====Simple==== | ||
Ligne 256: | 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 284: | 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 299: | Ligne 355: | ||
print(" | print(" | ||
</ | </ | ||
- | ===Module get object=== | ||
- | |||
- | [[https:// | ||
=====Exception===== | =====Exception===== | ||
- | |||
<code python> | <code python> | ||
try: | try: | ||
Ligne 317: | Ligne 369: | ||
</ | </ | ||
- | **[[https:// | + | **[[https:// |
- | =====Pass===== | + | ===Pass=== |
- | ====Du sale==== | + | **Du sale** |
<code python> | <code python> | ||
try: | try: | ||
resp = 1 / 0 | resp = 1 / 0 | ||
+ | return resp | ||
except: | except: | ||
pass | pass | ||
</ | </ | ||
- | + | **Du propre** | |
- | ====Du propre==== | + | Ne jamais passer les erreurs sous silence, ou les faire taire explicitement. |
<code python> | <code python> | ||
def exemple_propre(a): | def exemple_propre(a): | ||
try: | try: | ||
return 1 / a | return 1 / a | ||
- | except as e: | + | except |
print(e) | print(e) | ||
+ | return None | ||
+ | except: | ||
return None | return None | ||
</ | </ | ||
- | =====Les différents type de variable===== | + | **Pass pour du code en construction** |
- | ====Liste==== | + | |
<code python> | <code python> | ||
+ | def je_multiplie(): | ||
+ | pass | ||
+ | |||
+ | def j_imprime(): | ||
+ | pass | ||
</ | </ | ||
- | ====Dictionnaire==== | + | =====Type===== |
+ | Python est fortement typé: il utilise le **[[https:// | ||
<code python> | <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 " | ||
</ | </ | ||
- | ====Tuples==== | + | <code python |
- | <code python> | + | from pi import pi as pipi |
- | </ | + | # Toutes les variables globales en majuscule |
+ | PI = 3.14 | ||
- | ====Chaîne de caractères==== | + | # Ce pi est une variable globale |
- | <code python> | + | 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() | ||
</ | </ | ||
- | ====Set==== | + | |
- | Je n' | + | =====POO===== |
+ | |||
+ | ====Programmation Orientée Objet==== | ||
+ | **[[https:// | ||
+ | |||
+ | ====Pourquoi des class ?==== | ||
+ | **[[initiation_a_python# | ||
+ | |||
+ | ====Les class permettent de créer des objets==== | ||
<code python> | <code python> | ||
- | my_set = {1, 2, 3, 4, 2, 1} | + | class Subliminale: |
- | print(my_set) | + | pass |
+ | |||
+ | class Minimale: | ||
+ | print(" | ||
+ | |||
+ | class MyTest: | ||
+ | def my_test(self): | ||
+ | print(" | ||
+ | |||
+ | class Bicycle: | ||
+ | """ | ||
+ | |||
+ | def __init__(self, color, kind): | ||
+ | """ | ||
+ | |||
+ | self.color = color | ||
+ | self.kind = kind | ||
+ | |||
+ | s = Subliminale() | ||
+ | print(type(s)) | ||
+ | |||
+ | m = Minimale() | ||
+ | |||
+ | b = Bicycle(" | ||
+ | print(b.color) | ||
+ | print(b.kind) | ||
</ | </ | ||
- | + | ||
=====La philosophie Python===== | =====La philosophie Python===== | ||
Ligne 373: | Ligne 488: | ||
</ | </ | ||
<code text> | <code text> | ||
- | Préfèrer le beau au laid, | + | Préfère : |
- | l’explicite à l’implicite, | + | la beauté à la laideur, |
- | le simple au complexe, | + | l'explicite à l'implicite, |
- | le complexe au compliqué, | + | le simple au complexe |
- | le déroulé à l’imbriqué, | + | |
- | l’aéré au compact. | + | le déroulé à l'imbriqué, |
- | La lisibilité | + | l'aéré au compact. |
- | Les cas particuliers ne le sont jamais assez pour violer les règles, | + | Prends en compte |
- | même s’il faut privilégier | + | Les cas particuliers ne le sont jamais assez pour violer les règles. |
- | Ne jamais passer | + | Mais, à la pureté, privilégie |
- | ou les faire taire explicitement. | + | Ne passe pas les erreurs sous silence, |
- | Face à l’ambiguïté, | + | ... ou bâillonne-les explicitement. |
- | Il doit y avoir une – et si possible | + | Face à l'ambiguïté, |
- | même si cette façon | + | Sache qu'il ne devrait |
- | Mieux vaut maintenant que jamais, | + | même si, de prime abord, elle n'est pas évidente, à moins d'être Néerlandais. |
- | même si jamais est souvent mieux qu’immédiatement. | + | Mieux vaut maintenant que jamais. |
- | Si l’implémentation s’explique difficilement, | + | Cependant |
- | Si l’implémentation s’explique | + | Si l'implémentation s'explique difficilement, |
- | Les espaces de nommage | + | Si l'implémentation s'explique |
- | </ | + | Les espaces de nommage |
====PEP8===== | ====PEP8===== | ||
* [[http:// | * [[http:// | ||
- | **Avec python, sommes toujours entre adultes responsables.** | + | **Avec python, |
* C'est toujours possible d’enfreindre une règle. | * C'est toujours possible d’enfreindre une règle. | ||
* Par contre, certaines règles sont obligatoires: | * Par contre, certaines règles sont obligatoires: | ||
- | * Les choses cachées ne le sont jamais absolument. | + | * [[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:// | ||
- | {{tag> | + | {{tag> |
- | {{tag> | + |
initiation_a_python.txt · Dernière modification : 2021/08/21 13:41 de serge