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
Types de données
●Affichage sortie standard :
print expr1, ... ,
exprN
●Importation de module
import lib, ...
from lib import obj, ...
●Instruction conditionnelle :
if condition1:
instructions
elif condition2:
instructions
elif ...
else:
instructions
●Boucle d'énumérative :
for element in sequence:
instructions
●Boucle conditionnelle :
while condition:
instructions
●Sortie de boucle :
i = 0
while 1:
i = i + 1
if i == 3:
break
●Sortie d'itération :
for i in (1, 2, 3, 4):
if i == 3:
continue
print i
●Déclaration de fonction :
def maFonction(param):
instructions
●Sortie de fonction :
def identite(a):
return a
●Instruction d'inaction :
def faitRien():
pass
●Nombre entier :
0
-1024
0x15
0777
10000000000L
●Nombre flottant :
0.0
-4e-10
3.14
●Nombre complexe :
0j
5 + 3j
●Séquence :
●Chaine :
“”
“Hello”
'World'
“““Hello
World”””
●Tuple (non modifiable) :
()
(x ,)
(“deux”, (2,
2.0))
●Liste (modifiable) :
[]
[a ,]
[root, “\\”, (x,
y), 077, []]
●Dictionnaire :
{}
{
“cle”: valeur,
0x13: “MOV r0 0x0a”,
(x, y): (r, v, b)
}
●Valeur nulle :
None
Opérateurs
Orienté objets
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
●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)
●Déclaration :
●class NomClasse:
attributClasse = valeurDefaut;
# contructeur
def __init__(self, param1, ...):
self.attributObjet = param1
self.__attributPrive = valeur
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)
●Utilisation :
instance = NomClasse(param1, ...)
instance.methode(param1, ...)
print instance
instance.attributObjet = valeur
NomClasse.attributClasse = valeur
print instance
1 / 2 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!