# -*- coding: utf-8 -*- ## Conditions: if/then/else # if x = 43 if x == 42: # ne pas oublier le ':' print "x vaut 42" print "et pas autre chose" # Fin de l'indentation = fin du if print "Suite du programme" # if/else if x == 42: print "x vaut 42" else: print "x vaut autre chose" print "fin du if" # if/elif if x == 42: print "x vaut 42" elif x == 43: print "x vaut quarante trois" else: print "x vaut autre chose" ## Algorithme d'euclide : cf euclide-*.py ## Chaines de caractères a = "Ensimag" len(a) a.upper() a[0] a[2:4] ## Liste a = ['spam', 'eggs', 100, 1234] print a print a + ['python', 'eggs'] # tranches a[1] a[1:2] a[1:-1] 3 * a[:3] + ['Boo!'] ## Fonctions disponibles sur les listes a = [66.25, 333, 333, 1, 1234.5] print a.count(333), a.count(66.25), a.count('x') a.insert(2, -1) a.append(333) print a a.index(333) a.remove(333) print a a.reverse() print a a.sort() print a ## Boucles # while while x <= 42: x = x + 1 # for words = ['cat', 'window', 'defenestrate'] for w in words: print w, len(w) ## Cas particulier avec les boucles for range(10) for i in range(10): print i for i in range(7, 10): print i ## Disposition du code x = 42 # OK # x = # Interdit! # 42 x = (1 + 2 + 3 + 4 + 5 + # OK 6 + 7 + 8 + 9 + 10) x = 42; print x ## Exercice : somme des N premiers entiers # cf. sum.py