Python basics intro with variables, data types, operators, control flow and functions
1. Untitled0 http://127.0.0.1:8888/27ec8dbc-329f-4d46-87e9-5d085a...
In [1]: a = 'bonjour calvix'
In [2]: print a
bonjour calvix
In [3]: a='bonjour'
In [4]: a = "bonjour"
In [5]: a
Out[5]: 'bonjour'
In [6]: a= 42
In [7]: print a
42
In [8]: a = 10
b = 20
c = a + b
In [9]: print c
30
In [10]: 3*4
Out[10]: 12
In [11]: 3-2
Out[11]: 1
In [12]: 4/2
Out[12]: 2
In [13]: 5/2
Out[13]: 2
In [14]: 5.0/2
Out[14]: 2.5
In [16]: type(10)
Out[16]: int
In [17]: type(10.0)
Out[17]: float
In [18]: type('blabla')
Out[18]: str
In [19]: 10,5
Out[19]: (10, 5)
In [21]: # operateurs classique +-=/
In [23]: 5%2
Out[23]: 1
1 of 6 13/01/2012 22:09
2. Untitled0 http://127.0.0.1:8888/27ec8dbc-329f-4d46-87e9-5d085a...
In [26]: 'foo ' + 'b a r'
Out[26]: 'foo b a r'
In [28]: 'foo' + 10
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/nicolas/<ipython-input-28-89215d4a8e7f> in <module>()
----> 1 'foo' + 10
TypeError: cannot concatenate 'str' and 'int' objects
In [30]: 'foo' + '10'
Out[30]: 'foo10'
In [32]: 'foo' + str(10)
Out[32]: 'foo10'
In [35]: 'foo' * 5
Out[35]: 'foofoofoofoofoo'
In [37]: prenom = 'nicolas'
'bonjour, %s' % prenom
Out[37]: 'bonjour, nicolas'
In [39]: 'bonjour, {}'.format(prenom)
Out[39]: 'bonjour, nicolas'
In [40]: # Boolene
In [41]: True #False
Out[41]: True
In [42]: b = [29, 'foo', a]
In [44]: b
Out[44]: [29, 'foo', 10]
In [46]: b[1]
Out[46]: 'foo'
In [48]: b[1] = 42
In [49]: print b
[29, 42, 10]
In [50]: b[5]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/home/nicolas/<ipython-input-50-ca5d543a082a> in <module>()
----> 1 b[5]
IndexError: list index out of range
2 of 6 13/01/2012 22:09
3. Untitled0 http://127.0.0.1:8888/27ec8dbc-329f-4d46-87e9-5d085a...
In [51]: b[3] = 20
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/home/nicolas/<ipython-input-51-f517828affd8> in <module>()
----> 1 b[3] = 20
IndexError: list assignment index out of range
In [52]: b.append('calvix')
In [53]: print b
[29, 42, 10, 'calvix']
In [54]: len(b)
Out[54]: 4
In [55]: b.append(20)
In [56]: print b
[29, 42, 10, 'calvix', 20]
In [58]: b.append([20, 'linux'])
In [59]: print b
[29, 42, 10, 'calvix', 20, [20, 'linux'], [20, 'linux']]
In [60]: b.extend([30, 'torvalds'])
In [61]: print b
[29, 42, 10, 'calvix', 20, [20, 'linux'], [20, 'linux'], 30, 'torvalds']
In [64]: b.count([20, 'linux'])
Out[64]: 2
In [67]: print b.pop()
[20, 'linux']
In [68]: # Tuple
In [70]: a = (20, 'calvix', 30)
In [71]: print a
(20, 'calvix', 30)
In [72]: a[1] = 'linux'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/nicolas/<ipython-input-72-228477c61ecf> in <module>()
----> 1 a[1] = 'linux'
TypeError: 'tuple' object does not support item assignment
In [73]: 30, 'calvix'
Out[73]: (30, 'calvix')
In [74]: 'calvix'
Out[74]: 'calvix'
In [75]: a = 'ping'
3 of 6 13/01/2012 22:09
4. Untitled0 http://127.0.0.1:8888/27ec8dbc-329f-4d46-87e9-5d085a...
In [77]: print a[1]
i
In [78]: a[1] = 'o'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/nicolas/<ipython-input-78-a1f251c3625e> in <module>()
----> 1 a[1] = 'o'
TypeError: 'str' object does not support item assignment
In [81]: a = {1: 'un', 2: 'deux', 'linus': 'torvalds', 'GNU': 'RMS'}
In [82]: a[1]
Out[82]: 'un'
In [83]: a[3]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/nicolas/<ipython-input-83-94e7916e7615> in <module>()
----> 1 a[3]
KeyError: 3
In [85]: a['GNU'] = 42
In [87]: print a
{1: 'un', 2: 'deux', 'linus': 'torvalds', 'GNU': 42}
In [88]: print a['linus']
torvalds
In [89]: conf = {'taille': 30, 'largeur': 20, 'couleur': 'bleu'}
In [90]: conf['couleur']
Out[90]: 'bleu'
In [91]: # affectation de valeur à une variable
a = 10
In [93]: # Comparaison de valeurs
a == 12
Out[93]: False
In [94]: # > < <= >= == !=
In [97]: age = 70
if age >= 60:
print 'vieux'
elif age < 20:
print 'très jeune'
else:
print 'jeune'
vieux
In [98]: tableau = ['foo', 'bar', 42]
In [99]: 'foo' in tableau
Out[99]: True
4 of 6 13/01/2012 22:09
5. Untitled0 http://127.0.0.1:8888/27ec8dbc-329f-4d46-87e9-5d085a...
In [101]: if 'calvix' in tableau:
print 'c'est dans le tableau'
else:
print 'pas grave'
pas grave
In [102]: for element in tableau:
print element
foo
bar
42
In [105]: for i, item in enumerate(tableau):
print i, item
0 foo
1 bar
2 42
In [106]: for i in range(10):
print i
0
1
2
3
4
5
6
7
8
9
In [107]: i=0
i += 1
In [108]: print i
1
In [109]: #équivaut à; i = i + 1
In [110]: # n'existe pas avec python i++;i--;
In [114]: i = 0
while i < 5:
print 'i vaut %s' % i
i += 1
i vaut 0
i vaut 1
i vaut 2
i vaut 3
i vaut 4
In [132]: def bonjour (prenom, age):
if age < 0:
raise ValueError
print 'bonjour %s, j'ai %s ans' % (prenom, str(age))
In [134]: try:
bonjour('nicolas', -10)
except ValueError:
print 'exception levée'
exception levée
In [139]: def add (a, b):
return a+b
In [140]: add(40, 2)
Out[140]: 42
5 of 6 13/01/2012 22:09
6. Untitled0 http://127.0.0.1:8888/27ec8dbc-329f-4d46-87e9-5d085a...
In [141]: def valeur(a, b):
return a, b
In [142]: x, y = valeur(20, 30)
In [143]: print x
20
In [150]: class Point:
def __init__(self, x, y):
self._valeur= 30
self.x = x
self.y = y
In [148]: a = Point(20, 30)
In [149]: a.x
Out[149]: 20
In [151]: a = add
In [152]: print a
<function add at 0x96f7844>
In [153]: a(3, 5)
Out[153]: 8
In [153]:
In [158]: a = 'UnE PhRase ExeMple'
In [159]: a.capitalize()
Out[159]: 'Une phrase exemple'
In [160]: import string
In [162]: string.printable
Out[162]: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[]^_`{|}~ tnrx0bx0c'
In [ ]:
6 of 6 13/01/2012 22:09