Presentationpython Ipynb
Presentationpython Ipynb
Nombres et opérations
In [2]: f = 34.78e-12 # flottant
c = 4+0.5j # complexe avec le suffixe j
i = -89 # entier notation décimale
ix = 0x100FF # entier notation hexadécimale
io = 0o7734 # entier notation octale
ib = 0b0101 # entier notation binaire
grand = 563272876763298724546328656232098919898326525729851563297862672
97632567816572898675
print("Un flottant:", f, "et un complexe:", c)
print("Des entiers:", i, ix, io, ib, sep=' ')
print(grand + 1)
print(4 * (c + 3.4j) ** 2)
print(1/2, 1//2, 1.0//2, 15%3, sep=' ')
Booléens
In [3]: vrai = True
faux = False
print("Valeurs booléennes:", vrai, faux)
expr = i < 0 and ix >-60000
print("expr:", expr)
Texte (str)
In [4]: s = """Caractère par son nom Unicode: \N{GREEK SMALL LETTER PI}
Caractère par son code Unicode: \u03A9"""
print(s)
Fortement typé
In [6]: #print(34 + "12")
#print("34" + 12)
In [8]: lst[2]
Out[8]: 9
In [9]: lst[2:4]
In [10]: lst[::2]
In [11]: lst[::-1]
In [12]: 9 in lst
Out[12]: True
Out[14]: 3.14159
In [15]: 12 in dico
Out[15]: True
Contrôle de flux
In [16]: x = 2
while x < 12:
if x%3:
x += 1
continue
print(x, end=" ")
x += 1
3 6 9
Valeur: 1
Valeur: 6
Valeur: 9
Valeur: -12
Valeur: 78
Valeur: 6
1
6
9
-12
78
6
0 1
1 6
2 9
3 -12
4 78
5 6
Combien?8
0 1 2 3 4 5 6 7
deux ===> 2
un ===> 1
pi ===> 3.14159
12 ===> douze
Switch
In [22]: d = {"double": lambda x: x*2,
"carre": lambda x: x**2,
"cube": lambda x: x**3,
"triple": lambda x: x*3
}
print(d["cube"](4))
print(d["triple"](4))
64
12
Expressions pratiques
In [23]: carres = [ x ** 2 for x in range(2,100) if x%6==0] # Construction d'une
liste via boucle de calcul
print(carres)
[36, 144, 324, 576, 900, 1296, 1764, 2304, 2916, 3600, 4356, 5184, 6084
, 7056, 8100, 9216]
Out[24]: 14285214290714285714119047619050000
Fonctions
In [27]: def moysous(sequence, seuil=100):
som, nbval = 0, 0
for val in sequence:
if val<seuil:
som += val
nbval += 1
res = som / nbval
return res
print(moysous([1,2,14,3,4,5], 10))
print(moysous([1,2,140,3,4,5]))
#print(moysous([], 10)) # bug division par zéro
3.0
3.0
4 sur 9 03/02/2015 16:56
Notebook [Link]
Définition de classe
In [30]: class AccumuleLongueur:
def __init__(self, val=0):
self._v = val
def __add__(self, s):
return AccumuleLongueur(self._v + len(s))
def __str__(self):
return "Longueur {}".format(self._v)
al = AccumuleLongueur()
print(al)
al = al + "Un début"
print(al)
al += "on ajoute une chaine."
print(al)
bl = al + "Et encore une autre mais que dans bl"
print(al, bl)
Longueur 0
Longueur 8
Longueur 29
Longueur 29 Longueur 65
5 sur 9 03/02/2015 16:56
Notebook [Link]
Duck Typing
In [31]: import sys
class FicUpper:
def write(self, s):
[Link]([Link]())
fic = FicUpper()
print("On affiche une chaîne", file=fic)
print("Et une autre.", file=fic)
Taille: 8
Pack => b'\x02\t\x00\x00\x00\x00:\x01' Octets: [2, 9, 0, 0, 0, 0, 58,
1]
Unpack => (2, 9, 14849)
Appel fonction C
// malib.c #include double mafct(double a, int b); double mafct(double a, int b) { return a * b; } // $ cc -shared
-Wl,-soname,malib -o [Link] -fPIC malib.c
<CDLL '/home/laurent/ownCloud/python-reseau-electroniciens/[Link]', h
andle 15e87b0 at 7fb868654be0>
<_FuncPtr object at 0x7fb86868e4f8>
Résultat: 15.2
3 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (2)
6 BINARY_MULTIPLY
7 STORE_FAST 1 (res)
4 10 LOAD_FAST 1 (res)
13 RETURN_VALUE
In [45]: !ls
In [47]: