pres02_basic_statements.ipynb 50.3 KB
Newer Older
paugier's avatar
paugier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Python training UGA 2017\n",
    "\n",
    "**A training to acquire strong basis in Python to use it efficiently**\n",
    "\n",
15
    "Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)\n",
paugier's avatar
paugier committed
16
    "\n",
17
    "# Standard types and basic statements"
paugier's avatar
paugier committed
18
19
20
21
22
23
24
25
26
27
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    "## Function calls\n",
    "\n",
    "There are [built-in functions](https://docs.python.org/3/library/functions.html) and the developers can of course define other functions. To call a function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some functions return a result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(1.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's common to store the result in a variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_var = round(1.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which can then be used:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(my_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Few standard types\n",
paugier's avatar
paugier committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    "\n",
    "- Simple types (`int`, `float`, `bool`, `complex`)\n",
    "- Standard type `str`\n",
    "- Standard type `list`\n",
    "- Standard type `tuple`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### `int` (integers)"
   ]
  },
  {
   "cell_type": "code",
146
   "execution_count": 5,
paugier's avatar
paugier committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = 4\n",
    "c = -10\n",
    "\n",
    "# binary notation (base 2)\n",
    "b = 0b010\n",
    "\n",
    "# octal notation (base 8)\n",
    "o = 0o011\n",
    "\n",
    "# hexadecimal (base 16)\n",
    "h = 0x1cf0\n",
    "\n",
    "a = int('1')       # base 10\n",
    "a = int('111', 2)  # base 2\n",
    "a = int('70', 8)   # base 8\n",
    "a = int('16', 16)  # base 16"
   ]
  },
paugier's avatar
paugier committed
170
171
172
173
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
paugier's avatar
paugier committed
174
    "Remark: `int` in Python 3 are impressive! No limit! See https://docs.python.org/3.1/whatsnew/3.0.html#integers"
paugier's avatar
paugier committed
175
176
   ]
  },
paugier's avatar
paugier committed
177
178
179
180
181
182
183
184
185
186
187
188
189
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Arithmetic operations"
   ]
  },
  {
   "cell_type": "code",
190
   "execution_count": 6,
paugier's avatar
paugier committed
191
   "metadata": {},
paugier's avatar
paugier committed
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
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n",
      "7\n",
      "30\n",
      "3.3333333333333335\n",
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(10 + 3)\n",
    "print(10 - 3)\n",
    "print(10 * 3)\n",
    "print(10 / 3)  # float division\n",
    "print(10 // 3)  # integer division\n",
    "print(10 % 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### `bool` (booleans)"
   ]
  },
  {
   "cell_type": "code",
228
   "execution_count": 7,
paugier's avatar
paugier committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "b = bool('1')\n",
    "b = False\n",
    "b = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Comparison operations (bool)\n",
    "\n",
    "- `==` equal\n",
    "- `!=` différent\n",
    "- `<` inferior\n",
248
    "- `<=` inferior or equal\n",
paugier's avatar
paugier committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    "- `>` superior\n",
    "- `>=` superior or equal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "##### Keyword `is`: check identity"
   ]
  },
  {
   "cell_type": "code",
266
   "execution_count": 8,
paugier's avatar
paugier committed
267
   "metadata": {},
paugier's avatar
paugier committed
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
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = None\n",
    "print(a is None)\n",
    "print(a is not None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##### Keywords `and` and `or`"
   ]
  },
  {
   "cell_type": "code",
297
   "execution_count": 9,
paugier's avatar
paugier committed
298
299
300
301
302
303
304
305
306
307
308
309
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
310
     "execution_count": 9,
paugier's avatar
paugier committed
311
312
313
314
315
316
317
318
319
320
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True"
   ]
  },
  {
   "cell_type": "code",
321
   "execution_count": 10,
paugier's avatar
paugier committed
322
   "metadata": {},
paugier's avatar
paugier committed
323
324
325
326
327
328
329
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
330
     "execution_count": 10,
paugier's avatar
paugier committed
331
332
333
334
335
336
337
338
339
340
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
341
   "execution_count": 11,
paugier's avatar
paugier committed
342
   "metadata": {},
paugier's avatar
paugier committed
343
344
345
346
347
348
349
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
350
     "execution_count": 11,
paugier's avatar
paugier committed
351
352
353
354
355
356
357
358
359
360
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and False"
   ]
  },
  {
   "cell_type": "code",
361
   "execution_count": 12,
paugier's avatar
paugier committed
362
   "metadata": {},
paugier's avatar
paugier committed
363
364
365
366
367
368
369
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
370
     "execution_count": 12,
paugier's avatar
paugier committed
371
372
373
374
375
376
377
378
379
380
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or True"
   ]
  },
  {
   "cell_type": "code",
381
   "execution_count": 13,
paugier's avatar
paugier committed
382
   "metadata": {},
paugier's avatar
paugier committed
383
384
385
386
387
388
389
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
390
     "execution_count": 13,
paugier's avatar
paugier committed
391
392
393
394
395
396
397
398
399
400
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or False"
   ]
  },
  {
   "cell_type": "code",
401
   "execution_count": 14,
paugier's avatar
paugier committed
402
   "metadata": {},
paugier's avatar
paugier committed
403
404
405
406
407
408
409
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
410
     "execution_count": 14,
paugier's avatar
paugier committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False or False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### `float` (real, double precision) and `complex`"
   ]
  },
  {
   "cell_type": "code",
432
   "execution_count": 15,
paugier's avatar
paugier committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# float\n",
    "a = float('1')\n",
    "a = 1.234\n",
    "a = 1e2\n",
    "a = -1e-2\n",
    "a = .2"
   ]
  },
  {
   "cell_type": "code",
448
   "execution_count": 16,
paugier's avatar
paugier committed
449
   "metadata": {},
paugier's avatar
paugier committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1+2j) 1.0 2.0\n"
     ]
    }
   ],
   "source": [
    "# complex (2 floats)\n",
    "c = complex('1')\n",
    "c = 1 + 2j\n",
    "print(c, c.real, c.imag)"
   ]
  },
466
467
468
469
470
471
472
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remark: notation `var_name.attr_name` to access to an attribute of an object."
   ]
  },
paugier's avatar
paugier committed
473
474
475
476
477
478
479
480
481
482
483
484
485
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Warning about floating-point arithmetic and numerical errors!"
   ]
  },
  {
   "cell_type": "code",
486
   "execution_count": 17,
paugier's avatar
paugier committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n"
     ]
    }
   ],
   "source": [
    "b = 1e16\n",
    "c = 1.2 + b\n",
    "d = c - b\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
paugier's avatar
paugier committed
508
509
    "Very general issue (not Python): \n",
    "\n",
510
    "See https://en.wikipedia.org/wiki/Floating-point_arithmetic and https://docs.python.org/3/tutorial/floatingpoint.html"
paugier's avatar
paugier committed
511
512
   ]
  },
paugier's avatar
paugier committed
513
514
515
516
517
518
519
520
521
522
523
524
525
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Standard type `str`"
   ]
  },
  {
   "cell_type": "code",
526
   "execution_count": 18,
paugier's avatar
paugier committed
527
   "metadata": {},
paugier's avatar
paugier committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Strings on \n",
      "more thand \n",
      "one line.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "s = 'hello'\n",
    "s = \"hello\"\n",
    "\n",
    "s = ('How is it possible to write a very very '\n",
    "     'very long string with lines limited to 79 characters?')\n",
    "\n",
    "s = \"\"\"Strings on \n",
    "more thand \n",
    "one line.\n",
    "\"\"\"\n",
    "print(s)"
   ]
  },
paugier's avatar
paugier committed
554
555
556
557
558
559
560
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Warning: big difference between Python 2 and Python 3. In Python 3, `str` are unicode and there is another type `bytes`."
   ]
  },
paugier's avatar
paugier committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Methods of the type `str`\n",
    "\n",
    "Objects of built-in types have methods associated with their type (object oriented programming). The built-in function `dir` returns a list of name of the attributes. For a string, these attributes are python system attributes (with double-underscores) and several public methods:"
   ]
  },
  {
   "cell_type": "code",
576
   "execution_count": 19,
paugier's avatar
paugier committed
577
578
579
580
581
582
583
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
paugier's avatar
paugier committed
584
585
586
     "name": "stdout",
     "output_type": "stream",
     "text": [
587
      "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n"
paugier's avatar
paugier committed
588
     ]
paugier's avatar
paugier committed
589
590
591
592
    }
   ],
   "source": [
    "s = 'abcdef'\n",
paugier's avatar
paugier committed
593
    "print(dir(s))"
paugier's avatar
paugier committed
594
595
596
597
598
599
600
601
602
603
604
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To access an attribute of an object (here, the method `str.startswith`), we use the dot:"
   ]
  },
  {
   "cell_type": "code",
605
   "execution_count": 20,
paugier's avatar
paugier committed
606
   "metadata": {},
paugier's avatar
paugier committed
607
608
609
610
611
612
613
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
614
     "execution_count": 20,
paugier's avatar
paugier committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.startswith('a')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### standard type `str`\n",
    "\n",
    "#### function `str.format`\n",
    "\n",
    "```text\n",
    "Docstring:\n",
    "S.format(*args, **kwargs) -> str\n",
    "\n",
    "Return a formatted version of S, using substitutions from args and kwargs.\n",
    "The substitutions are identified by braces ('{' and '}').\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
646
   "execution_count": 21,
paugier's avatar
paugier committed
647
   "metadata": {},
paugier's avatar
paugier committed
648
649
650
651
652
653
654
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a = 1.23456789'"
      ]
     },
655
     "execution_count": 21,
paugier's avatar
paugier committed
656
657
658
659
660
661
662
663
664
665
666
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1.23456789\n",
    "'a = {}'.format(a)"
   ]
  },
  {
   "cell_type": "code",
667
   "execution_count": 22,
paugier's avatar
paugier committed
668
   "metadata": {},
paugier's avatar
paugier committed
669
670
671
672
673
674
675
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a = 1.2346'"
      ]
     },
676
     "execution_count": 22,
paugier's avatar
paugier committed
677
678
679
680
681
682
683
684
685
686
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'a = {:.4f}'.format(a)"
   ]
  },
  {
   "cell_type": "code",
687
   "execution_count": 23,
paugier's avatar
paugier committed
688
   "metadata": {},
paugier's avatar
paugier committed
689
690
691
692
693
694
695
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a =   1.2346'"
      ]
     },
696
     "execution_count": 23,
paugier's avatar
paugier committed
697
698
699
700
701
702
703
704
705
706
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'a = {:8.4f}'.format(a)"
   ]
  },
  {
   "cell_type": "code",
707
   "execution_count": 24,
paugier's avatar
paugier committed
708
   "metadata": {},
paugier's avatar
paugier committed
709
710
711
712
713
714
715
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a = 1.2346e+00 (scientific notation)'"
      ]
     },
716
     "execution_count": 24,
paugier's avatar
paugier committed
717
718
719
720
721
722
723
724
725
726
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'a = {:.4e} (scientific notation)'.format(a)"
   ]
  },
  {
   "cell_type": "code",
727
   "execution_count": 25,
paugier's avatar
paugier committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t2\t3\n"
     ]
    }
   ],
   "source": [
    "print('{}\\t{}\\t{}'.format(1, 2, 3))"
   ]
  },
  {
   "cell_type": "markdown",
paugier's avatar
paugier committed
744
   "metadata": {
paugier's avatar
paugier committed
745
746
747
    "slideshow": {
     "slide_type": "subslide"
    }
paugier's avatar
paugier committed
748
   },
paugier's avatar
paugier committed
749
750
751
752
753
754
755
756
   "source": [
    "### standard type `str`\n",
    "\n",
    "#### New in Python 3.6: format strings"
   ]
  },
  {
   "cell_type": "code",
757
   "execution_count": 26,
paugier's avatar
paugier committed
758
759
760
761
762
763
764
765
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a = 1.23456789'"
      ]
     },
766
     "execution_count": 26,
paugier's avatar
paugier committed
767
768
769
770
771
772
773
774
775
776
777
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1.23456789\n",
    "f'a = {a}'"
   ]
  },
  {
   "cell_type": "code",
778
   "execution_count": 27,
paugier's avatar
paugier committed
779
780
781
782
783
784
785
786
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a = 1.2346'"
      ]
     },
787
     "execution_count": 27,
paugier's avatar
paugier committed
788
789
790
791
792
793
794
795
796
797
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f'a = {a:.4f}'"
   ]
  },
  {
   "cell_type": "code",
798
   "execution_count": 28,
paugier's avatar
paugier committed
799
800
801
802
803
804
805
806
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a =   1.2346'"
      ]
     },
807
     "execution_count": 28,
paugier's avatar
paugier committed
808
809
810
811
812
813
814
815
816
817
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f'a = {a:8.4f}'"
   ]
  },
  {
   "cell_type": "code",
818
   "execution_count": 29,
paugier's avatar
paugier committed
819
820
821
822
823
824
825
826
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a = 1.2346e+00 (scientific notation)'"
      ]
     },
827
     "execution_count": 29,
paugier's avatar
paugier committed
828
829
830
831
832
833
834
835
836
837
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f'a = {a:.4e} (scientific notation)'"
   ]
  },
  {
   "cell_type": "code",
838
   "execution_count": 30,
paugier's avatar
paugier committed
839
   "metadata": {},
paugier's avatar
paugier committed
840
841
842
843
844
845
846
847
848
849
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t2\t3\n"
     ]
    }
   ],
   "source": [
850
    "print(f'{1}\\t{1+1}\\t{2+1}')"
paugier's avatar
paugier committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### standard type `str`\n",
    "\n",
    "Strings are immutable **[\"sequences\"](https://docs.python.org/3/library/stdtypes.html)**.\n",
    "\n",
865
    "- lookup"
paugier's avatar
paugier committed
866
867
868
869
   ]
  },
  {
   "cell_type": "code",
870
   "execution_count": 31,
paugier's avatar
paugier committed
871
   "metadata": {},
paugier's avatar
paugier committed
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s = 'abcdef'\n",
    "print('a' in s)\n",
    "print('hello' not in s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- We can get an element of a string (**index starts from 0**):"
   ]
  },
  {
   "cell_type": "code",
897
   "execution_count": 32,
paugier's avatar
paugier committed
898
   "metadata": {},
paugier's avatar
paugier committed
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print(s[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- since strings are immutable, they can not be modified inplace. If we try, we get an error:\n",
    "\n",
    "```python\n",
    "s[0] = 'b'\n",
    "```\n",
    "```text\n",
    "---------------------------------------------------------------------------\n",
    "TypeError                                 Traceback (most recent call last)\n",
    "<ipython-input-5-55620f378bce> in <module>()\n",
    "----> 1 s[0] = 'b'\n",
    "\n",
    "TypeError: 'str' object does not support item assignment\n",
    "```\n",
    "\n",
    "- since strings are sequences, they can be \"sliced\" (we will soon study in details this powerful notation):"
   ]
  },
  {
   "cell_type": "code",
935
   "execution_count": 33,
paugier's avatar
paugier committed
936
   "metadata": {},
paugier's avatar
paugier committed
937
938
939
940
941
942
943
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bc'"
      ]
     },
944
     "execution_count": 33,
paugier's avatar
paugier committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- it is very simple to manipulate strings in many ways:"
   ]
  },
  {
   "cell_type": "code",
962
   "execution_count": 34,
paugier's avatar
paugier committed
963
   "metadata": {},
paugier's avatar
paugier committed
964
965
966
967
968
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
969
970
971
972
      "Abcdef ABCDEF\n",
      "Abcdef ABCDEF\n",
      "Abcdef ABCDEF\n",
      "Abcdef ABCDEF\n",
paugier's avatar
paugier committed
973
974
975
976
977
      "\n"
     ]
    }
   ],
   "source": [
978
    "print((s.capitalize() + ' ' + s.upper() + '\\n') * 4 ) "
paugier's avatar
paugier committed
979
980
   ]
  },
paugier's avatar
paugier committed
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Slicing \n",
    "\n",
    "Very general, can be used on all sequences as `str`, `list`, etc... Not simple for beginners but very powerfull (see [here](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) and [here](http://avilpage.com/2015/03/a-slice-of-python-intelligence-behind.html)).\n",
    "\n",
    "Python indexes and slices for a six-element str. Indexes enumerate the elements, slices enumerate the spaces between the elements.\n",
    "\n",
    "```text\n",
    "Index from rear:    -6  -5  -4  -3  -2  -1\n",
    "Index from front:    0   1   2   3   4   5      \n",
    "                   +---+---+---+---+---+---+\n",
    "                   | a | b | c | d | e | f |    \n",
    "                   +---+---+---+---+---+---+    \n",
    "Slice from front:  0   1   2   3   4   5   6    \n",
paugier's avatar
paugier committed
1002
    "Slice from rear:  -6  -5  -4  -3  -2  -1   None\n",
paugier's avatar
paugier committed
1003
1004
1005
1006
1007
    "```"
   ]
  },
  {
   "cell_type": "code",
1008
   "execution_count": 35,
paugier's avatar
paugier committed
1009
1010
1011
1012
1013
1014
1015
1016
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ce'"
      ]
     },
1017
     "execution_count": 35,
paugier's avatar
paugier committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'abcdef'\n",
    "# s[start:stop:step]\n",
    "s[2:6:2]"
   ]
  },
  {
   "cell_type": "code",
1030
   "execution_count": 36,
paugier's avatar
paugier committed
1031
1032
1033
1034
1035
1036
1037
1038
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cdef'"
      ]
     },
1039
     "execution_count": 36,
paugier's avatar
paugier committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# s[start:stop]\n",
    "s[2:6]"
   ]
  },
  {
   "cell_type": "code",
1051
   "execution_count": 37,
paugier's avatar
paugier committed
1052
1053
1054
1055
1056
1057
1058
1059
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bcdef'"
      ]
     },
1060
     "execution_count": 37,
paugier's avatar
paugier committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# s[start:]\n",
    "s[1:]"
   ]
  },
  {
   "cell_type": "code",
1072
   "execution_count": 38,
paugier's avatar
paugier committed
1073
1074
1075
1076
1077
1078
1079
1080
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ab'"
      ]
     },
1081
     "execution_count": 38,
paugier's avatar
paugier committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# s[:stop]\n",
    "s[:2]"
   ]
  },
1091
1092
  {
   "cell_type": "code",
1093
   "execution_count": 39,
1094
1095
1096
1097
1098
1099
1100
1101
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fedcba'"
      ]
     },
1102
     "execution_count": 39,
1103
1104
1105
1106
1107
1108
1109
1110
1111
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# step = -1: goes through the string in reverse order\n",
    "s[::-1]"
   ]
  },
paugier's avatar
paugier committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Do it yourself\n",
    "\n",
    "Suppose we have a string representing a header line of the form:"
   ]
  },
  {
   "cell_type": "code",
1127
   "execution_count": 40,
paugier's avatar
paugier committed
1128
1129
1130
1131
1132
1133
1134
1135
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
paugier's avatar
paugier committed
1136
    "s = ' wind;temperature;;pressure '"
paugier's avatar
paugier committed
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
1147
    "- Remove leading and ending blanks (see `str.replace` and `str.strip`)\n",
paugier's avatar
paugier committed
1148
1149
1150
    "- Extract the first field (e.g. using `find` method and slicing)\n",
    "- Extract the last field (e.g. using `rfind` method and slicing)\n",
    "- Check for empty field (e.g. `find` \";;\" pattern)\n",
1151
1152
    "- Remove empty field (e.g. using replace method)\n",
    "- BONUS : Extract the second field (tip :`find` can take an argument that tells where to start the search)"
paugier's avatar
paugier committed
1153
1154
   ]
  },
paugier's avatar
paugier committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### A possible solution:"
   ]
  },
Franck Thollard's avatar
Franck Thollard committed
1166
1167
  {
   "cell_type": "code",
1168
   "execution_count": 41,
Franck Thollard's avatar
Franck Thollard committed
1169
1170
1171
1172
1173
1174
1175
1176
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--wind;temperature;;pressure--\n",
      "wind\n",
1177
      "temperature\n",
Franck Thollard's avatar
Franck Thollard committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
      "pressure\n",
      "16\n",
      "wind;temperature;pressure\n"
     ]
    }
   ],
   "source": [
    "s = ' wind;temperature;;pressure '\n",
    "# remove leading blanks\n",
    "s = s.strip()\n",
    "print(\"--{}--\".format(s))\n",
    "# extract the first field\n",
    "idx = s.find(\";\")\n",
    "s0 = s[0:idx]\n",
    "print(s0)\n",
1193
1194
1195
    "# extract the second field\n",
    "idx1 = s.find(\";\", idx+1) # start the search after the first \";\"\n",
    "s1 = s[idx+1:idx1]\n",
Franck Thollard's avatar
Franck Thollard committed
1196
    "print(s1)\n",
1197
1198
1199
1200
    "# extract the last field\n",
    "idx2 = s.rfind(\";\")\n",
    "s2 = s[idx2+1:]\n",
    "print(s2)\n",
Franck Thollard's avatar
Franck Thollard committed
1201
1202
1203
1204
1205
1206
1207
    "idx_first_empty_field = s.find(\";;\")\n",
    "print(idx_first_empty_field)\n",
    "# remove empty field\n",
    "s_no_empty = s.replace(\";;\", \";\")\n",
    "print(s_no_empty)"
   ]
  },
paugier's avatar
paugier committed
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### standard type `list`\n",
    "\n",
    "A list is a **mutable sequence of (possibly inhomogeneous) elements**."
   ]
  },
  {
   "cell_type": "code",
1223
   "execution_count": 42,
paugier's avatar
paugier committed
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
1234
     "execution_count": 42,
paugier's avatar
paugier committed
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type([0, 'a'])"
   ]
  },
  {
   "cell_type": "code",
1245
   "execution_count": 43,
paugier's avatar
paugier committed
1246
   "metadata": {},
paugier's avatar
paugier committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2', 6, 3.0]\n"
     ]
    }
   ],
   "source": [
    "# create an empty list\n",
    "l = []\n",
    "# fill the list (with the function append)\n",
    "l.append('2')\n",
    "# fill the list (with the function extend)\n",
    "l.extend([6, 3.])\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
1268
   "execution_count": 44,
paugier's avatar
paugier committed
1269
   "metadata": {},
paugier's avatar
paugier committed
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2', 6, 3.0, 'hello', 3]\n"
     ]
    }
   ],
   "source": [
    "# concatenate lists with the operator +\n",
    "print(l + ['hello', 3])"
   ]
  },
  {
   "cell_type": "code",
1286
   "execution_count": 45,
paugier's avatar
paugier committed
1287
   "metadata": {},
paugier's avatar
paugier committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3.0 6\n",
      "['2', 6]\n"
     ]
    }
   ],
   "source": [
    "# get values\n",
    "print(l[0], l[2], l[-2])\n",
    "# slicing\n",
    "print(l[0:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### standard type `tuple`\n",
    "\n",
    "A tuple is a **immutable sequence of (possibly inhomogeneous) elements**.\n",
    "\n",
    "Remark: when you need a sequence that won't be modified, tuple is usually more efficient than list."
   ]
  },
  {
   "cell_type": "code",
1322
   "execution_count": 46,
paugier's avatar
paugier committed
1323
   "metadata": {},
paugier's avatar
paugier committed
1324
1325
1326
1327
1328
1329
1330
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
1331
     "execution_count": 46,
paugier's avatar
paugier committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = 0, 'a', 1.2\n",
    "t1 = (5, 'hello')\n",
    "t2 = tuple([1.1, 2])\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
1345
   "execution_count": 47,
paugier's avatar
paugier committed
1346
   "metadata": {},
paugier's avatar
paugier committed
1347
1348
1349
1350
1351
1352
1353
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
1354
     "execution_count": 47,
paugier's avatar
paugier committed
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1]  # indexing"
   ]
  },
  {
   "cell_type": "code",
1365
   "execution_count": 48,
paugier's avatar
paugier committed
1366
   "metadata": {},
paugier's avatar
paugier committed
1367
1368
1369
1370
1371
1372
1373
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 1.2)"
      ]
     },
1374
     "execution_count": 48,
paugier's avatar
paugier committed
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1:]  # slicing"
   ]
  },
  {
   "cell_type": "code",
1385
   "execution_count": 49,
paugier's avatar
paugier committed
1386
   "metadata": {},
paugier's avatar
paugier committed
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "a, b = t1  # tuple assigment\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Mutable and immutable objects\n",
    "\n",
    "The objects of type `str`, `int`, `float`, `bool` are immutable. They can not be modified. Of course, a name that points towards an integer can point towards a different integer."
   ]
  },
  {
   "cell_type": "code",
1416
   "execution_count": 50,
paugier's avatar
paugier committed
1417
   "metadata": {},
paugier's avatar
paugier committed
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "i = i + 2  # (or i += 2)\n",
    "print(i)\n",
    "i = 10\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, the objects `1` and `3` have not been modified.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Mutable and immutable objects\n",
    "\n",
    "An object of type `list` is mutable:"
   ]
  },
  {
   "cell_type": "code",
1459
   "execution_count": 51,
paugier's avatar
paugier committed
1460
   "metadata": {},
paugier's avatar
paugier committed
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 5]\n",
      "[0, 5, 'hello']\n"
     ]
    }
   ],
   "source": [
    "l = [0, 5]\n",
    "print(l)\n",
    "l.append('hello')\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Here, the object list tagged by the name `l` has been modified inplace."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## References and `del` keyword\n",
    "\n",
    "`del` removes a reference. If an object in not binded to any names, Python can delete it from its internal memory."
   ]
  },
  {
   "cell_type": "code",
1504
   "execution_count": 52,
paugier's avatar
paugier committed
1505
   "metadata": {},
paugier's avatar
paugier committed
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a']\n"
     ]
    }
   ],
   "source": [
    "l = ['a', 'b']\n",
    "del l[1]\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### More on slicing \n",
    "\n",
    "Very general, can be used on all sequences as `str`, `list`, etc... Not simple for beginners but very powerfull (see [here](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) and [here](http://avilpage.com/2015/03/a-slice-of-python-intelligence-behind.html)).\n",
    "\n",
    "Python indexes and slices for a six-element str. Indexes enumerate the elements, slices enumerate the spaces between the elements.\n",
    "\n",
    "```text\n",
    "Index from rear:    -6  -5  -4  -3  -2  -1\n",
    "Index from front:    0   1   2   3   4   5      \n",
    "                   +---+---+---+---+---+---+\n",
    "                   | a | b | c | d | e | f |    \n",
    "                   +---+---+---+---+---+---+    \n",
    "Slice from front:  0   1   2   3   4   5   6    \n",
    "Slice from rear:  -6  -5  -4  -3  -2  -1   0\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
1548
   "execution_count": 53,
paugier's avatar
paugier committed
1549
   "metadata": {},
paugier's avatar
paugier committed
1550
1551
1552
1553
1554
1555
1556
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ce'"
      ]
     },
1557
     "execution_count": 53,
paugier's avatar
paugier committed
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'abcdef'\n",
    "# s[start:stop:step]\n",
    "s[2:6:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### More on slicing \n",
    "\n",
    "#### Assigment to mutable object"
   ]
  },
  {
   "cell_type": "code",
1583
   "execution_count": 54,
paugier's avatar
paugier committed
1584
   "metadata": {},
paugier's avatar
paugier committed
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 'a']\n",
      "[0, 1, 2, 3, 4, 5, 'a']\n"
     ]
    }
   ],
   "source": [
    "l = [0, 1, 2, 3, 4, 5]\n",
    "l1 = l  # assigment to a new name l1 (no copy of the object).\n",
    "# the names l and l1 points towards the same object.\n",
    "l1.append('a')\n",
    "print(l1)\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "#### Shallow copy"
   ]
  },
  {
   "cell_type": "code",
1617
   "execution_count": 55,
paugier's avatar
paugier committed
1618
   "metadata": {},
paugier's avatar
paugier committed
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 'a']\n",
      "[0, 1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "l = [0, 1, 2, 3, 4, 5]\n",
    "l1 = l[:] # shallow copy of l\n",
    "l1.append('a')\n",
    "print(l1)\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### More on slicing\n",
    "\n",
    "Other examples of slices for a six-element list. Indexes enumerate the elements, slices enumerate the spaces between the elements."
   ]
  },
  {
   "cell_type": "code",
1652
   "execution_count": 56,
paugier's avatar
paugier committed
1653
   "metadata": {},
paugier's avatar
paugier committed
1654
1655
1656
1657
1658
1659
1660
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
1661
     "execution_count": 56,
paugier's avatar
paugier committed
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [0, 1, 2, 3, 4, 5]  \n",
    "all([\n",
    "    len(a) == 6,\n",
    "    a[1:] == [1, 2, 3, 4, 5],\n",
    "    a[:5] == [0, 1, 2, 3, 4],\n",
    "    a[0] == 0,\n",
    "    a[:-2] == [0, 1, 2, 3],\n",
    "    a[5] == 5,\n",
    "    a[1:2] == [1],\n",
    "    a[-1] == 5,\n",
    "    a[1:-1] == [1, 2, 3, 4],\n",
paugier's avatar
paugier committed
1678
1679
    "    a[-2] == 4,\n",
    "])"
paugier's avatar
paugier committed
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Do it yourself\n",
    "\n",
    "Suppose we have the string containing header line."
   ]
  },
  {
   "cell_type": "code",
1697
   "execution_count": 57,
paugier's avatar
paugier committed
1698
1699
1700
1701
1702
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
paugier's avatar
paugier committed
1703
    "s = 'wind;temperature;pressure'"
paugier's avatar
paugier committed
1704
1705
1706
1707
1708
1709
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
1710
1711
    "- Extract the list of items (i.e. \"wind\", \"temperature\", \"pressure\"; see `str.split`).\n",
    "- Add \"Snow level\" to the list of items (e.g. using append)\n",
paugier's avatar
paugier committed
1712
    "- Build a new header such that items are capitalized (e.g. using the methods `str.join` and `str.capitalize` and iterating on the list)"
paugier's avatar
paugier committed
1713
1714
   ]
  },
paugier's avatar
paugier committed
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### A possible solution:"
   ]
  },
Franck Thollard's avatar
Franck Thollard committed
1726
1727
  {
   "cell_type": "code",
1728
   "execution_count": 58,
Franck Thollard's avatar
Franck Thollard committed
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['wind', 'temperature', 'pressure']\n",
      "['Wind', 'Temperature', 'Pressure', 'Snow level']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Wind;Temperature;Pressure;Snow level'"
      ]
     },
1745
     "execution_count": 58,
Franck Thollard's avatar
Franck Thollard committed
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'wind;temperature;pressure'\n",
    "list_items = s.split(\";\")\n",
    "print(list_items)\n",
    "list_items.append(\"snow level\".capitalize())\n",
    "list_items[0] = list_items[0].capitalize()\n",
    "list_items[1] = list_items[1].capitalize()\n",
    "list_items[2] = list_items[2].capitalize()\n",
    "\n",
    "print(list_items)\n",
    "\";\".join(list_items)"
   ]
  },
paugier's avatar
paugier committed
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### The function `range` \n",
    "\n",
    "The function returns a range object:"
   ]
  },
  {
   "cell_type": "code",
1778
   "execution_count": 59,
paugier's avatar
paugier committed
1779
   "metadata": {},
paugier's avatar
paugier committed
1780
1781
1782
1783
1784
1785
1786
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 8, 2)"
      ]
     },
1787
     "execution_count": 59,
paugier's avatar
paugier committed
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# start, stop, step\n",
    "range(1, 8, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can make a list with the range object:"
   ]
  },
  {
   "cell_type": "code",
1806
   "execution_count": 60,
paugier's avatar
paugier committed
1807
   "metadata": {},
paugier's avatar
paugier committed
1808
1809
1810
1811
1812
1813
1814
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7]"
      ]
     },
1815
     "execution_count": 60,
paugier's avatar
paugier committed
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# start, stop, step\n",
    "list(range(1, 8, 2))"
   ]
  },
  {
   "cell_type": "code",
1827
   "execution_count": 61,
paugier's avatar
paugier committed
1828
   "metadata": {},
paugier's avatar
paugier committed
1829
1830
1831
1832
1833
1834
1835
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5, 6, 7]"
      ]
     },
1836
     "execution_count": 61,
paugier's avatar
paugier committed
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# start, stop (step=1)\n",
    "list(range(2, 8))"
   ]
  },
  {
   "cell_type": "code",
1848
   "execution_count": 62,
paugier's avatar
paugier committed
1849
   "metadata": {},
paugier's avatar
paugier committed
1850
1851
1852
1853
1854
1855
1856
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7]"
      ]
     },
1857
     "execution_count": 62,
paugier's avatar
paugier committed
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# stop argument (start=0, step=1)\n",
    "list(range(8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Do it yourself\n",
    "\n",
    "Build a list of odd numbers in decreasing order."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Conditions: `if`, `elif`, `else`\n",
    "\n",
    "```python\n",
    "if expression:\n",
    "   statement(s)\n",
    "else:\n",
    "   statement(s)\n",
    "```\n",
    "\n",
    "The statement contains the block of code that executes if the conditional expression in the if statement resolves to 1 or a TRUE value."
   ]
  },
  {
   "cell_type": "code",
1902
   "execution_count": 63,
paugier's avatar
paugier committed
1903
   "metadata": {},
paugier's avatar
paugier committed
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is equal to 0.\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "if a == 0:\n",
    "    print('a is equal to 0.')\n"
   ]
  },
  {
   "cell_type": "code",
1921
   "execution_count": 64,
paugier's avatar
paugier committed
1922
   "metadata": {},
paugier's avatar
paugier committed
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is positive.\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "if a < 0:\n",
    "    print('a is negative.')\n",
    "elif a == 0:\n",
    "    print('a is equal to 0.')\n",
    "elif a > 0:\n",
    "    print('a is positive.')\n",
    "else:\n",
    "    print(\"I don't know.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Loops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Loops with the keyword `while`\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
1969
   "execution_count": 65,
paugier's avatar
paugier committed
1970
   "metadata": {},
paugier's avatar
paugier committed
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i = 4\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 4:\n",
    "    i += 1\n",
    "print('i =', i)"
   ]
  },
  {
   "cell_type": "code",
1989
   "execution_count": 66,
paugier's avatar
paugier committed
1990
   "metadata": {},
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i = 1\n",
      "i = 2\n",
      "i = 3\n",
      "i = 4\n"
     ]
    }
   ],
paugier's avatar
paugier committed
2003
2004
2005
2006
   "source": [
    "i = 0\n",
    "while i < 4:\n",
    "    i += 1\n",
Franck Thollard's avatar
Franck Thollard committed
2007
2008
    "    print('i =', i)\n",
    "    "
paugier's avatar
paugier committed
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Do it yourself\n",
    "\n",
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
    "- Edit a script with the spyder editor that calculates the average of a set of numbers. For example `numbers = [67, 12, 2, 9, 23, 5]`\n",
    "\n",
    "  * using the functions `sum` and `len`\n",
    "  * manually, using the keyword `while`\n",
    "  \n",
    "- Run the script \n",
    "\n",
    "  * in spyder,\n",
    "  * in a ipython session opened from another terminal,\n",
    "  * with the command `python`."
paugier's avatar
paugier committed
2031
2032
   ]
  },
paugier's avatar
paugier committed
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### A possible solution:"
   ]
  },
Franck Thollard's avatar
Franck Thollard committed
2044
2045
  {
   "cell_type": "code",
2046
   "execution_count": 67,
Franck Thollard's avatar
Franck Thollard committed
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19.666666666666668\n"
     ]
    }
   ],
   "source": [
2058
    "numbers = [67, 12, 2, 9, 23, 5]\n",
Franck Thollard's avatar
Franck Thollard committed
2059
2060
    "local_sum = 0\n",
    "i = 0\n",
2061
2062
    "while i < len(numbers):\n",
    "    local_sum = local_sum + numbers[i]\n",
Franck Thollard's avatar
Franck Thollard committed
2063
    "    i = i+1\n",
2064
    "avg = local_sum / len(numbers)\n",
Franck Thollard's avatar
Franck Thollard committed
2065
2066
2067
    "print(avg)"
   ]
  },
paugier's avatar
paugier committed
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Loops with the keyword `for`"
   ]
  },
  {
   "cell_type": "code",
2081
   "execution_count": 68,
paugier's avatar
paugier committed
2082
   "metadata": {},
paugier's avatar
paugier committed
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0, 1, 2, 3, 4, "
     ]
    }
   ],
   "source": [
2093
2094
    "for index in range(5):\n",
    "    print(index, end=', ')"
paugier's avatar
paugier committed
2095
2096
2097
2098
   ]
  },
  {
   "cell_type": "code",
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "grenoble, paris, berlin, london, "
     ]
    }
   ],
   "source": [
    "cities = [\"grenoble\", \"paris\", \"berlin\", \"london\"]\n",
    "for city in cities:\n",
    "    print(city, end=\", \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
paugier's avatar
paugier committed
2119
   "metadata": {},
paugier's avatar
paugier committed
2120
2121
2122
2123
2124
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
2125
2126
2127
2128
      "(0, grenoble)\n",
      "(1, paris)\n",
      "(2, berlin)\n",
      "(3, london)\n"
paugier's avatar
paugier committed
2129
2130
2131
2132
2133
     ]
    }
   ],
   "source": [
    "# the built-in function enumerate is very useful\n",
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
    "for index, city in enumerate(cities): \n",
    "    print('({}, {})'.format(index, city))\n",
    "    cities[index] = city.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Grenoble', 'Paris', 'Berlin', 'London']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities"
paugier's avatar
paugier committed
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Loops: keywords `continue` and `break`\n",
    "\n",
    "- `continue`: passes the block in the loop and continues the loop."
   ]
  },
  {
   "cell_type": "code",
2174
   "execution_count": 72,
paugier's avatar
paugier committed