Mémo Python Par Damien Boucard pour le club Lolut - http://lolut.utbm.info/ Sous licence Creative Commons ShareAlike 1.0 - http://creativecommons.org/licenses/sa/1.0/ Instructions de base ● Affichage sortie standard : ● Sortie d'itération : print expr1, ... , exprN for i in (1, 2, 3, 4): ● Boucle d'énumérative : for element in sequence: ● Importation de module if i == 3: instructions import lib, ... from lib import obj, ... continue print i ● Boucle conditionnelle : while condition: ● Instruction conditionnelle : ● Déclaration de fonction : instructions if condition1: def maFonction(param): instructions ● Sortie de boucle : instructions i = 0 elif condition2: ● Sortie de fonction : while 1: instructions def identite(a): i = i + 1 elif ... if i == 3: else: return a ● Instruction d'inaction : break instructions def faitRien(): pass Types de données ● Nombre entier : ● Nombre complexe : ● Tuple (non modifiable) : ● Dictionnaire : 0 0j () {} -1024 5 + 3j (x ,) { 0x15 0777 10000000000L ● Nombre flottant : 0.0 -4e-10 3.14 ● Séquence : ● Chaine : “” “Hello” 'World' “““Hello World””” (“deux”, (2, 2.0)) “cle”: valeur, 0x13: “MOV r0 0x0a”, ● Liste (modifiable) : (x, y): (r, v, b) [] [a ,] [root, “\\”, (x, y), 077, []] } ● Valeur nulle : None Opérateurs ● Affectation : a = 1 ● Arithmétique : a + b – c * d / e % f ** g ● Logique : (not a) or (b and c) ● Comparaison : a == b and c != d and e < f and g <= h and i > j and k >= l (m in sequence) and (n not in sequence) ● Sur bits : (a << 4) | (b & c >> 7) Orienté objets ● Déclaration : ● Utilisation : ● class NomClasse: instance = NomClasse(param1, ...) attributClasse = valeurDefaut; instance.methode(param1, ...) # contructeur print instance def __init__(self, param1, ...): instance.attributObjet = valeur self.attributObjet = param1 NomClasse.attributClasse = valeur self.__attributPrive = valeur print instance def methode(self, param1, ...): self.attributObjet += param1 # methode de conversion en chaine def __str__(self): return “attributClasse=%s, attributObjet=%s, attributPrive=%s” %(self.attributClasse, self.attributObjet, self.__attributPrive) Pour aller plus loin ● La doc de référence de la bibliothèque standard Python - http://www.python.org/doc/current/lib/lib.html ● Grand répertoire de liens pour Python - http://www.python-eggs.org/links.html