Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"...on parlait de **[programmation impérative](https://fr.wikipedia.org/wiki/Programmation_imp%C3%A9rative) [structurée](https://fr.wikipedia.org/wiki/Programmation_structur%C3%A9e)**..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Les instructions de base"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions d'assignation"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"a = 3\n",
"b = 2\n",
"print(a+b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pour connaître les variables qui sont définies dans le _scope_ actuel :"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['In',\n",
" 'Out',\n",
" '_',\n",
" '__',\n",
" '___',\n",
" '__builtin__',\n",
" '__builtins__',\n",
" '__doc__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__spec__',\n",
" '_dh',\n",
" '_i',\n",
" '_i1',\n",
" '_i2',\n",
" '_ih',\n",
" '_ii',\n",
" '_iii',\n",
" '_oh',\n",
" 'a',\n",
" 'b',\n",
" 'exit',\n",
" 'get_ipython',\n",
" 'quit']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pour effacer une variable :"
]
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"z = 2\n",
"del(z)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'z' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-3a710d2a84f8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"\u001b[0;31mNameError\u001b[0m: name 'z' is not defined"
]
}
],
"source": [
"z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions conditionnelles"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"if a > b:\n",
" print(a)\n",
"else:\n",
" print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Indentation : espaces ou tabulation ?\n",
"\"Use 4 spaces per indentation level.\", cf. https://www.python.org/dev/peps/pep-0008/"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"La valeur de la variable a est supérieure à 1.\n"
]
}
],
"source": [
"if a > 1:\n",
" print('La valeur de la variable a est supérieure à 1.')\n",
"elif a < 1:\n",
" print('La valeur de la variable a est inférieure à 1.')\n",
"else:\n",
" print('La valeur de la variable a est égale à 1.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions de bouclage"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"a = 0\n",
"while a < 5:\n",
" a += 1\n",
" print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"N.B. : Il n'y a pas de `do...while`, mais l'on peut l'emuler"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"a = 0\n",
"while True:\n",
" a += 1\n",
" print(a)\n",
" if a >= 5:\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for i in range(3):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for i in range(0, 3):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n"
]
}
],
"source": [
"for i in range(1, 3):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class range in module builtins:\n",
"\n",
"class range(object)\n",
" | range(stop) -> range object\n",
" | range(start, stop[, step]) -> range object\n",
" | \n",
" | Return an object that produces a sequence of integers from start (inclusive)\n",
" | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.\n",
" | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.\n",
" | These are exactly the valid indices for a list of 4 elements.\n",
" | When step is given, it specifies the increment (or decrement).\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __bool__(self, /)\n",
" | self != 0\n",
" | \n",
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
" | __contains__(self, key, /)\n",
" | Return key in self.\n",
" | \n",
" | __eq__(self, value, /)\n",
" | Return self==value.\n",
" | \n",
" | __ge__(self, value, /)\n",
" | Return self>=value.\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __getitem__(self, key, /)\n",
" | Return self[key].\n",
" | \n",
" | __gt__(self, value, /)\n",
" | Return self>value.\n",
" | \n",
" | __hash__(self, /)\n",
" | Return hash(self).\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __le__(self, value, /)\n",
" | Return self<=value.\n",
" | \n",
" | __len__(self, /)\n",
" | Return len(self).\n",
" | \n",
" | __lt__(self, value, /)\n",
" | Return self<value.\n",
" | \n",
" | __ne__(self, value, /)\n",
" | Return self!=value.\n",
" | \n",
" | __reduce__(...)\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | __reversed__(...)\n",
" | Return a reverse iterator.\n",
" | \n",
" | count(...)\n",
" | rangeobject.count(value) -> integer -- return number of occurrences of value\n",
" | \n",
" | index(...)\n",
" | rangeobject.index(value) -> integer -- return index of value.\n",
" | Raise ValueError if the value is not present.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Static methods defined here:\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
" | Data descriptors defined here:\n",
" | \n",
" | start\n",
" | \n",
" | step\n",
" | \n",
" | stop\n",
"\n"
]
}
],
"source": [
"help(range)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
}
},
"nbformat": 4,
"nbformat_minor": 2
}