import re import PyQt4 as pq from lxml.html import *
i = 1 s1 = "bye"
i,s1 = 1,"bye"
s2 = "Bye "+s1+" world" s2 = "Bye %s world"%s1 s2 = "Bye {0} world".format(s1)
+ - * / % **
x = 3 print x**4 # 81 print x%2 # 1
+ %
split rsplit join
str = "abc" i = 3 print "%s%s%s"%(i,str,i) # "3abc3" str = "une phrase avec plein de mots dedans" liste=str.split(' ') # ['une', 'phrase', 'avec', 'plein', 'de', 'mots', 'dedans'] print ','.join(liste) # une,phrase,avec,plein,de,mots,dedans
== != < > <= >=
or and not
min=x if x<y else y
=
1: l1 = [1, 2.1, "trois"] 2: print len(l1) # 3 3: 4: for i in l1: 5: print i, 6: 7: l1[1] = 4 # → 1 4 trois 8: print(l1[:1]) # → [1] 9: print(l1[1:]) # → [4,'trois'] 10: 11: l1.pop() # [1, 4] 12: l1.append(['trois',5]) # [1,4,['trois',5]] 13: l1.pop() # [1, 4] 14: l1.extend(['trois',5]) # [1,4,'trois',5] 15: del l1[0] # → [4,'trois',5] 16: l1.insert(0,"six") # → ['six',4,'trois',5] 17: 18: a,b,l3 = l1 19: ValueError: too many values to unpack 20: a,b,l3 = l1[0],l1[1],l1[2:] # a → 'six', b → 4, l3 → ['trois',5] 21: l2 = [ "a",l1,"b" ] # → [ 'a', ['six', 4, 'trois', 5], 'b']
1: T1 = ( 1, 4, "trois" ) 2: print(T1[:1]) # (1,) 3: print(T1[1:]) # (4,'trois') 4: 5: # un tuple n'est pas une liste ! 6: T1[1]=2.1 7: 'tuple' object does not support item assignment 8: T1.pop() 9: 'tuple' object has no attribute 'pop'
1: h = {'z':1, 'y':"deux", 'x':3.0 } 2: h[1] = 4 # 'y':"deux", 'z':1 'x':3.0 1:4 3: h['y'] = 2 # 'y':2 'z':1 'x':3.0 1:4 4: del h['z'] # 'y':2 'x':3.0 1:4 5: k = h.keys() # vue des clés : à ce moment de l'exécution → ['y', 'x', 1] 6: v = h.values() # vue des valeurs : à ce moment de l'exécution → [2, 3.0, 4] 7: for k in h.keys(): 8: print "(%s->%s)"%(k,h[k]), 9: # (y->2) (x->3.0) (1->4)
1: import re 2: s = " a = 5 " 3: if (re.search('^\s*\S+\s*=\s*\S+\s*$/',s): 4: m=re.search('\s*(\S+)\s*=\s*(\S+)\s*$',s) 5: x,y = m.group(1,2) # x → "a", y → "5" 6: s = re.sub('\s','',s) # s → "a=5" 7: h = re.findall('=+',s) # h → "="
Attention, il n'y a pas d'accolades pour séparer les blocs en python, c'est l'identation qui permet de les marquer. L'indentation fait partie de la syntaxe.
1: if ...: 2: ... 3: elif ...: 4: ... 5: else: 6: ...
1: while ...: 2: ... 3: 4: for i in liste: 5: ... 6: 7: for var in range(10): 8: ... 9: 10: while ...: 11: ... 12: if ...: 13: continue # itération suivante 14: elif ...: 15: break # fin des itérations
1: def f(a,b): 2: ... 3: f(1.2,"tagada")
1: def f(p1,p2=[]): 2: ... 3: 4: l1 = [1,2,3] 5: l2 = [4,5] 6: f([l1,l2]) 7: # p1=[[1,2,3],[4,5]], p2=[]
1: def f(p1,p2=[]): 2: ... 3: 4: l1 = [1,2,3] 5: l2 = [4,5] 6: f(l1,l2) 7: # p1=[1,2,3],p2=[4,5]]
1: def f(): 2: ... 3: return -1 4: ... # ne sera pas exécuté 5: 6: i=f() # $i → -1
1: import sys 2: 3: for line in sys.stdin: # lit ligne à ligne 4: print line,
1: import re 2: 3: fh = open("/etc/passwd") 4: for line in fh: 5: line=line.rstrip('\n') # on enlève la fin de ligne 6: line=re.sub('#.*$','',line) # on élimine les commentaires 7: if line=="": 8: break # on passe les lignes vides 9: ... 10: fh.close()
1: fh = open("/tmp/toto","w") 2: i = 1 3: for item in liste: 4: fh.write("%d: %s\n"%(i,item)) 5: i = i+1 6: fh.close()
s = """
bla bla
bla blabla bla
bla blabla blabla bla
"""
import os os.system("ls -l /")
import subprocess for i in subprocess.check_output(['/bin/ls','-l']).split('\n'): print i
try: des commandes... except: afficher un message sympathique pour indiquer le problème
1: # fichier Url.py 2: 3: import urllib 4: class Url: 5: 6: def __init__(self,uri): #constructeur 7: self.uri=uri #initialisation 8: 9: def get(self): #méthode 10: print urllib.urlopen(self.uri).read()
1: from Url import * 2: 3: u = Url("http://www.insmi.fr") 4: u.get()
$Id: memopythoncore.txt 638 2012-05-23 07:06:18Z aicardi $