-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathBasic_Objects.html
1283 lines (1181 loc) · 127 KB
/
Basic_Objects.html
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
335
336
337
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
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
897
898
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" /><meta content="Topic: Basics of Python Objects, Difficulty: Easy, Category: Section" name="description" />
<meta content="integers, booleans, floats, floating point precision, lists, strings, fundamentals" name="keywords" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basic Object Types — Python Like You Mean It</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<script src="../_static/gtag.js"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Sequence Types" href="SequenceTypes.html" />
<link rel="prev" title="Module 2: The Essentials of Python" href="../module_2.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<div class="version">
1.4
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Table of Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_1.html">Module 1: Getting Started with Python</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_2.html">Module 2: The Essentials of Python</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Basic Object Types</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Number-Types">Number Types</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Python’s-math-module">Python’s math module</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Integers">Integers</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Floating-Point-Numbers">Floating-Point Numbers</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Complex-Numbers">Complex Numbers</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Augmented-Assignment-Statements">Augmented Assignment Statements</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Improving-The-Readability-of-Numbers">Improving The Readability of Numbers</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#The-Boolean-Type">The Boolean Type</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Logic-Operators">Logic Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Boolean-Objects-are-Integers">Boolean Objects are Integers</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#The-None-Type">The None-Type</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Strings">Strings</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Introducing-the-string-type">Introducing the string type</a></li>
<li class="toctree-l4"><a class="reference internal" href="#String-essentials">String essentials</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Formatting-strings">Formatting strings</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Official-documentation-for-strings">Official documentation for strings</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#Lists">Lists</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Lists-are-sequences">Lists are sequences</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Lists-can-be-“mutated”">Lists can be “mutated”</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Official-documentation-for-lists">Official documentation for lists</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#Summary">Summary</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Links-to-Official-Documentation">Links to Official Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Reading-Comprehension-Exercise-Solutions:">Reading Comprehension Exercise Solutions:</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="SequenceTypes.html">Sequence Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="Variables_and_Assignment.html">Variables & Assignment</a></li>
<li class="toctree-l2"><a class="reference internal" href="Introduction.html">Introducing Control Flow</a></li>
<li class="toctree-l2"><a class="reference internal" href="ConditionalStatements.html">Conditional Statements</a></li>
<li class="toctree-l2"><a class="reference internal" href="ForLoops.html">For-Loops and While-Loops</a></li>
<li class="toctree-l2"><a class="reference internal" href="Iterables.html">Iterables</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generators_and_Comprehensions.html">Generators & Comprehension Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="Itertools.html">Python’s “Itertools”</a></li>
<li class="toctree-l2"><a class="reference internal" href="Functions.html">Basics of Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="Scope.html">Scope</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures.html">Data Structures (Part I): Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures_II_Dictionaries.html">Data Structures (Part II): Dictionaries</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures_III_Sets_and_More.html">Data Structures (Part III): Sets & the Collections Module</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_2_problems.html">Module 2: Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">Module 3: The Essentials of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3_problems.html">Module 3: Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_4.html">Module 4: Object Oriented Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_5.html">Module 5: Odds and Ends</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changes.html">Changelog</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../module_2.html">Module 2: The Essentials of Python</a> »</li>
<li>Basic Object Types</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module2_EssentialsOfPython/Basic_Objects.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast.container,
.nboutput.nblast.container {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast.container + .nbinput.container {
margin-top: -19px;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<div class="section" id="Basic-Object-Types">
<h1>Basic Object Types<a class="headerlink" href="#Basic-Object-Types" title="Permalink to this headline"></a></h1>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>Note</strong>:</p>
<p>There are reading-comprehension exercises included throughout the text. These are meant to help you put your reading to practice. Solutions for the exercises are included at the bottom of this page.</p>
</div>
<p>You will see the term “object” be used frequently throughout this text. In Python, the term “object” is quite the catch-all; including numbers, strings of characters, lists, functions - a Python object is essentially anything that you can assign to a variable. That being said, there are different <em>types</em> of objects: Python treats integers as a different <em>type</em> of object than a string, for instance.</p>
<p>The different object types have manifestly different purposes and thus have different built-in functions available to them. Here, we will review some of the basic types that are built into Python, as a natural entry point to writing code. We will cover:</p>
<ul class="simple">
<li><p>numbers (integers, floating-point numbers, and complex numbers)</p></li>
<li><p>booleans</p></li>
<li><p>the “null” type</p></li>
<li><p>strings</p></li>
<li><p>lists</p></li>
</ul>
<p>The built-in function <code class="docutils literal notranslate"><span class="pre">isinstance</span></code> will allow us to check if an object is of a given type. You can also use the built-in <code class="docutils literal notranslate"><span class="pre">type</span></code> function to check an object’s type. For example, the following code checks if an object is an integer:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># assign the variable `x` to the integer 1</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># checking the type of `x`</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="nb">int</span>
<span class="c1"># verifying that `x` is an integer-type object</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="kc">True</span>
</pre></div>
</div>
<p>In a later module, you will learn “object-oriented” programming, which will allow you to create your own, customized objects!</p>
<div class="section" id="Number-Types">
<h2>Number Types<a class="headerlink" href="#Number-Types" title="Permalink to this headline"></a></h2>
<p>Python has three basic types of numbers: integers, “floating-point” numbers, and complex numbers. Familiar mathematical symbols can be used to perform arithmetic on all of these numbers (comparison operators like “greater than” are not defined for complex numbers):</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 13%" />
<col style="width: 87%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">y</span></code></p></td>
<td><p>Sum of two numbers</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span></code></p></td>
<td><p>Difference of two numbers</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">*</span> <span class="pre">y</span></code></p></td>
<td><p>Product of two numbers</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code></p></td>
<td><p>Quotient of two numbers</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">//</span> <span class="pre">y</span></code></p></td>
<td><p>Quotient of two numbers, returned as an integer</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">x</span></code> “modulo”: <code class="docutils literal notranslate"><span class="pre">y</span></code>: The remainder of <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code> for positive <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">**</span> <span class="pre">y</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">x</span></code> raised to the power <code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">-x</span></code></p></td>
<td><p>A negated number</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">abs(x)</span></code></p></td>
<td><p>The absolute value of a number</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code></p></td>
<td><p>Check if two numbers have the same value</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">!=</span> <span class="pre">y</span></code></p></td>
<td><p>Check if two numbers have different values</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">></span> <span class="pre">y</span></code></p></td>
<td><p>Check if <code class="docutils literal notranslate"><span class="pre">x</span></code> is greater than <code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">>=</span> <span class="pre">y</span></code></p></td>
<td><p>Check if <code class="docutils literal notranslate"><span class="pre">x</span></code> is greater than or equal to <code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre"><</span> <span class="pre">y</span></code></p></td>
<td><p>Check if <code class="docutils literal notranslate"><span class="pre">x</span></code> is less than <code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre"><=</span> <span class="pre">y</span></code></p></td>
<td><p>Check if <code class="docutils literal notranslate"><span class="pre">x</span></code> is less than or equal to <code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
</tr>
</tbody>
</table>
<p>These operations obey the familiar order of operations from your mathematics class, with parentheses available for association:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># multiplication takes precedence over addition</span>
<span class="o">>>></span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">3</span>
<span class="mi">7</span>
<span class="c1"># grouping operations with parentheses</span>
<span class="o">>>></span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="mi">3</span>
<span class="mi">9</span>
<span class="c1"># finding the remainder of division between two positive numbers</span>
<span class="o">>>></span> <span class="mi">11</span> <span class="o">%</span> <span class="mi">5</span>
<span class="mi">1</span>
<span class="c1"># checking an inequality</span>
<span class="o">>>></span> <span class="p">(</span><span class="mi">2</span> <span class="o">**</span> <span class="mi">3</span><span class="p">)</span> <span class="o"><</span> <span class="p">(</span><span class="mi">2</span> <span class="o">**</span> <span class="mi">4</span><span class="p">)</span>
<span class="kc">True</span>
</pre></div>
</div>
<p>It should be noted that in many other programming languages, including the out-dated Python 2, dividing two integers would always return an integer - even if mathematically the result should be a fraction. In Python 3, <em>the quotient of two integers will always return a floating-point number</em> (i.e. a number that includes a decimal point):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># In many other languages, 3 / 2 returns the integer 1.</span>
<span class="c1"># In Python 3, division always returns a floating-point number:</span>
<span class="o">>>></span> <span class="mi">3</span> <span class="o">/</span> <span class="mi">2</span>
<span class="mf">1.5</span>
<span class="o">>>></span> <span class="mi">4</span> <span class="o">/</span> <span class="mi">2</span>
<span class="mf">2.0</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">//</span></code> operator is known as the “floor-divide” operator: it performs division between two numbers and returns the result as an integer by discarding any decimal-places for that number (thus returning the “floor” of that number). This can be used to perform the integer-division traditionally used in other programming languages:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># floor-division</span>
<span class="o">>>></span> <span class="mi">1</span> <span class="o">//</span> <span class="mi">3</span> <span class="c1"># 0.3333.. -> 0</span>
<span class="mi">0</span>
<span class="o">>>></span> <span class="mi">3</span> <span class="o">//</span> <span class="mi">2</span> <span class="c1"># 1.5 -> 1</span>
<span class="mi">1</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Understanding the modulo operator</strong></p>
<p>The modulo operator, <code class="docutils literal notranslate"><span class="pre">%</span></code>, is not commonly seen in mathematics textbooks. It is, however, a very useful operation to have at our disposal. <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code> (said as x “mod” y in programmer’s jargon) returns the <em>remainder</em> of <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code>, when <code class="docutils literal notranslate"><span class="pre">x</span></code> and `y are non-negative numbers. For example:</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(\frac{3}{2} = 1 + \frac{1}{2}\)</span>. 2 “goes into” 3 one time, leaving a remainder of 1. Thus <code class="docutils literal notranslate"><span class="pre">3</span> <span class="pre">%</span> <span class="pre">2</span></code> returns <code class="docutils literal notranslate"><span class="pre">1</span></code></p></li>
<li><p><span class="math notranslate nohighlight">\(\frac{9}{3} = 3\)</span>. 3 “goes into” 9 three times, and leaves no remainder. Thus <code class="docutils literal notranslate"><span class="pre">9</span> <span class="pre">%</span> <span class="pre">3</span></code> returns <code class="docutils literal notranslate"><span class="pre">0</span></code></p></li>
</ul>
<p>Given this description of the “mod” operator, simplify the following by hand, and then use the IPython console to check your work:</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">%</span> <span class="pre">5</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">%</span> <span class="pre">5</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">1</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">2</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">3</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">4</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">5</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">6</span></code></p></li>
</ol>
<p>Now, given any integer, <code class="docutils literal notranslate"><span class="pre">n</span></code>, what are the possible values that <code class="docutils literal notranslate"><span class="pre">n</span> <span class="pre">%</span> <span class="pre">2</span></code> can return? See if you can come up with a simple rule for explaining the behavior of <code class="docutils literal notranslate"><span class="pre">n</span> <span class="pre">%</span> <span class="pre">2</span></code>.</p>
</div>
<div class="section" id="Python’s-math-module">
<h3>Python’s math module<a class="headerlink" href="#Python’s-math-module" title="Permalink to this headline"></a></h3>
<p>The standard library’s math module provides us with many more mathematical functions, like logarithms and trigonometric functions. A complete listing of them <a class="reference external" href="https://docs.python.org/3/library/math.html#number-theoretic-and-representation-functions">can be found in the official Python documentation</a>. This module must be imported into your code in order to use its functions:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># using the `math` module to use</span>
<span class="c1"># additional mathematical functions</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">math</span>
<span class="o">>>></span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">4.</span><span class="p">)</span>
<span class="mf">2.0</span>
<span class="c1"># base-10 log</span>
<span class="o">>>></span> <span class="n">math</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="mf">10.</span><span class="p">)</span>
<span class="mf">1.0</span>
<span class="c1"># 4! = 4*3*2*1</span>
<span class="o">>>></span> <span class="n">math</span><span class="o">.</span><span class="n">factorial</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="mi">24</span>
</pre></div>
</div>
</div>
<div class="section" id="Integers">
<h3>Integers<a class="headerlink" href="#Integers" title="Permalink to this headline"></a></h3>
<p>As in traditional mathematics, an integer is any “whole” number: <span class="math notranslate nohighlight">\(\dots, -3, -2, -1, 0, 1, 2, 3, \dots\)</span>.</p>
<p>Integers belong to the built-in type <code class="docutils literal notranslate"><span class="pre">int</span></code>, which can be used to convert objects to integers:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">int</span>
<span class="go"># `1.3` is not an integer-type object</span>
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="mf">1.3</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="go">False</span>
<span class="go"># converting a string to an integer</span>
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="s2">"10"</span><span class="p">)</span>
<span class="go">10</span>
<span class="go"># converting a floating-point number to an integer</span>
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="mf">1.3</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<p>You can create as large an integer as you’d like; Python will allocate as much memory as needed (and ultimately, as is available) to store an integer’s exact value:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># you can make an integer as large as you'd like</span>
<span class="o">>>></span> <span class="n">large_int</span> <span class="o">=</span> <span class="mi">281938481039848500192847576920</span>
</pre></div>
</div>
<p>Integers have some built-in functions available to them, which are detailed in the <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#additional-methods-on-integer-types">official documentation</a>. The utility of these will likely be quite obscure to new programmers. Just note that they are here for now.</p>
</div>
<div class="section" id="Floating-Point-Numbers">
<h3>Floating-Point Numbers<a class="headerlink" href="#Floating-Point-Numbers" title="Permalink to this headline"></a></h3>
<p>A “floating-point” number is a number with a decimal point. Referred to as a “float” for short, this can be used to represent any number, up to a limited number of digits.</p>
<p>These objects belong to the built-in type <code class="docutils literal notranslate"><span class="pre">float</span></code>, which can be used to convert objects to floats:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># examples of "floating-point" numbers</span>
<span class="o">>>></span> <span class="mf">100.</span> <span class="o">**</span> <span class="mf">0.5</span>
<span class="mf">10.0</span>
<span class="o">>>></span> <span class="mi">1</span> <span class="o">/</span> <span class="mi">3</span>
<span class="mf">0.3333333333333333</span>
<span class="o">>>></span> <span class="mi">1</span> <span class="o">/</span> <span class="mi">2</span>
<span class="mf">0.5</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">(</span><span class="o">-</span><span class="mf">2.1</span><span class="p">)</span>
<span class="nb">float</span>
<span class="c1"># the integer 10 is not a float-type object</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span>
<span class="kc">False</span>
<span class="c1"># including a decimal makes the number a float</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">(</span><span class="mf">10.</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span>
<span class="kc">True</span>
<span class="c1"># converting a string to a floating-point number</span>
<span class="o">>>></span> <span class="nb">float</span><span class="p">(</span><span class="s2">"10.456"</span><span class="p">)</span>
<span class="mf">10.456</span>
<span class="c1"># converting an integer to a floating-point number</span>
<span class="o">>>></span> <span class="nb">float</span><span class="p">(</span><span class="o">-</span><span class="mi">22</span><span class="p">)</span>
<span class="o">-</span><span class="mf">22.0</span>
</pre></div>
</div>
<p>Floats have a couple of built-in functions available to them, as detailed in the <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#additional-methods-on-float">official documentation</a>.</p>
<div class="section" id="Scientific-Notation">
<h4>Scientific Notation<a class="headerlink" href="#Scientific-Notation" title="Permalink to this headline"></a></h4>
<p>A float can also be created using familiar scientific notation. The character <code class="docutils literal notranslate"><span class="pre">e</span></code> is used to represent <span class="math notranslate nohighlight">\(\times 10\)</span>, and the proceeding number is the exponent. Here are some examples of traditional scientific notation, and their corresponding representation in Python:</p>
<p><span class="math notranslate nohighlight">\(1.38 \times 10^{-4} \rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">1.38e-04</span></code></p>
<p><span class="math notranslate nohighlight">\(-4.2 \times 10^{10} \rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">-4.2e10</span></code></p>
<p>Python will automatically display a float that possesses many digits in scientific notation:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># python will display many-digit numbers using</span>
<span class="c1"># scientific notation</span>
<span class="o">>>></span> <span class="mf">0.0000001</span> <span class="c1"># seven leading-zeros</span>
<span class="mf">1e-07</span>
</pre></div>
</div>
</div>
<div class="section" id="Understanding-Numerical-Precision">
<h4>Understanding Numerical Precision<a class="headerlink" href="#Understanding-Numerical-Precision" title="Permalink to this headline"></a></h4>
<p>Whereas a Python integer can be made to be as large as you’d like, a floating-point number is <em>limited in the number of digits it can store</em>. That is, your computer will only use a set amount of memory - 8 bytes (64 bits) on most machines - to store the value of a floating-point number.</p>
<p>In effect, this means that a float can only be represented with a <em>numerical precision</em> of approximately 16 decimal places, when that number is written in scientific notation. The computer will not be able to reliably represent a number’s digits beyond those accounted for by the allotted 8 bytes. For instance, the following Python integer is defined with 100 digits, but when this number is converted to a float, it only retains 15 decimal places in scientific notation:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Demonstrating the finite-precision of a float.</span>
<span class="c1"># An integer with 100 digits - Python will use as</span>
<span class="c1"># much memory as needed to store an integer</span>
<span class="o">>>></span> <span class="nb">int</span><span class="p">(</span><span class="s2">"1"</span><span class="o">*</span><span class="mi">100</span><span class="p">)</span> <span class="c1"># creates a string with 100 1s and makes it an int</span>
<span class="mi">1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</span>
<span class="c1"># Converted to a float, it retains only</span>
<span class="c1"># 16 decimal places, when written in scientific</span>
<span class="c1"># notation. This is the precision permitted by</span>
<span class="c1"># 8 bytes of memory.</span>
<span class="o">>>></span> <span class="nb">float</span><span class="p">(</span><span class="s2">"1"</span><span class="o">*</span><span class="mi">100</span><span class="p">)</span> <span class="c1"># creates a string with 100 1s and makes it a float</span>
<span class="mf">1.111111111111111e+99</span>
</pre></div>
</div>
<p>The computer cannot keep track of those last 84 decimal places because doing so would require more than 8 bytes of memory to store the entire value of that float. If you had been diligently counting stars in the sky (perhaps across many universes, this number far exceeds the estimated number of stars in our universe), you would have just lost track of over <span class="math notranslate nohighlight">\(1\times10^{83}\)</span> of them simply by converting your integer count to a float!</p>
<p>As such, attempting to modify a floating point number in decimal places beyond its numerical precision does not have any effect:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># changing a float beyond its precision has no effect</span>
<span class="o">>>></span> <span class="mf">1.</span> <span class="o">+</span> <span class="mf">1e-16</span>
<span class="mf">1.0</span>
</pre></div>
</div>
<p>Even in light of this discussion on float precision, you may be shocked and dismayed to see the following outcome of float arithmetic:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># the finite-precision of floats</span>
<span class="c1"># result in non-obvious behavior</span>
<span class="o">>>></span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">-</span> <span class="mf">0.3</span> <span class="o">==</span> <span class="mf">0.</span>
<span class="kc">False</span>
<span class="c1"># the effects of having finite numerical precision</span>
<span class="o">>>></span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">-</span> <span class="mf">0.3</span>
<span class="mf">5.551115123125783e-17</span>
</pre></div>
</div>
<p>This is not a quirk of Python; this is a <a class="reference external" href="https://docs.python.org/3/tutorial/floatingpoint.html">well-understood</a> aspect of dealing with floating-point numbers with limited numerical precision. To accommodate this, don’t check if two floats are “equal”. Rather, you should check if they are “close enough in value”. Let me emphasize this:</p>
<p><strong>You should never check to see if two floats are exactly equal in value. Instead, you should only check that two floats are approximately equal to one another</strong>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">math</span></code> module has a very nice function for this; <code class="docutils literal notranslate"><span class="pre">math.isclose</span></code> will check if the relative difference between two numbers is less than <span class="math notranslate nohighlight">\(1 \times 10^{-9}\)</span>. You can change this tolerance value along with the type of tolerance-checking used by the function; see its documentation <a class="reference external" href="https://docs.python.org/3/library/math.html#math.isclose">here</a>. Because in the previous example we compare values that are close to 0, we will check if their absolute difference is sufficiently small:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># checking if two float values are "almost equal"</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">math</span>
<span class="c1"># check:</span>
<span class="c1"># | (0.1 + 0.1 + 0.1 - 0.3) - 0 | < 1x10^{-9}</span>
<span class="o">>>></span> <span class="n">math</span><span class="o">.</span><span class="n">isclose</span><span class="p">((</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">-</span> <span class="mf">0.3</span><span class="p">),</span> <span class="mf">0.</span><span class="p">,</span> <span class="n">abs_tol</span><span class="o">=</span><span class="mf">1e-9</span><span class="p">)</span>
<span class="kc">True</span>
</pre></div>
</div>
<p>If you do not heed this lesson, it is inevitable that you will end up with serious, hard-to-find bugs in your code. Lastly, when doing numerical work in Python (and any other programming language), you must understand that the finite numerical precision of floating-point numbers is a source of error, akin to error associated with imprecision with a measuring device, and should be accounted for in your analysis (if error analysis is warranted).</p>
<p>Python’s <a class="reference external" href="https://docs.python.org/3.0/library/decimal.html">decimal module</a> can be used to define higher (or lower) precision numbers than permitted by the standard 8-byte floats. Furthermore, all arithmetic involving decimal numbers from this module is guaranteed to be <em>exact</em>, meaning that <code class="docutils literal notranslate"><span class="pre">0.1</span> <span class="pre">+</span> <span class="pre">0.1</span> <span class="pre">+</span> <span class="pre">0.1</span> <span class="pre">-</span> <span class="pre">0.3</span></code> would be exactly <code class="docutils literal notranslate"><span class="pre">0.</span></code>. There is also a built-in <a class="reference external" href="https://docs.python.org/3/library/fractions.html#module-fractions">fractions module</a>, which provides tools for working with
exact representations of rational numbers. Although we will not be using them here, it is very important to keep in mind that these modules exist and that floating point numbers are not the only way around the number line in Python.</p>
</div>
</div>
<div class="section" id="Complex-Numbers">
<h3>Complex Numbers<a class="headerlink" href="#Complex-Numbers" title="Permalink to this headline"></a></h3>
<p>In mathematics, a “complex number” is a number with the form <span class="math notranslate nohighlight">\(a + bi\)</span>, where <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> are real-valued numbers, and <span class="math notranslate nohighlight">\(i\)</span> is defined to be the number that satisfies the relationship <span class="math notranslate nohighlight">\(i^2 = -1\)</span>. Because no real-valued number satisfies this relationship, <span class="math notranslate nohighlight">\(i\)</span> is called the “imaginary number”.</p>
<p>Weirdo electrical engineers use the symbol <span class="math notranslate nohighlight">\(j\)</span> in place of <span class="math notranslate nohighlight">\(i\)</span>, which is why Python displays the complex number <span class="math notranslate nohighlight">\(2 + 3i\)</span> as <code class="docutils literal notranslate"><span class="pre">2+3j</span></code> (this is actually because <span class="math notranslate nohighlight">\(i\)</span> typically denotes current; we like electrical engineers too).</p>
<p>Along with the <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">bj</span></code> syntax, built-in type <code class="docutils literal notranslate"><span class="pre">complex</span></code> can be used to create complex-type numbers:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># creating complex numbers</span>
<span class="o">>>></span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="n">j</span>
<span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="o">>>></span> <span class="nb">complex</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="o">>>></span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="nb">complex</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">(</span><span class="mi">2</span><span class="o">-</span><span class="mi">4</span><span class="n">j</span><span class="p">,</span> <span class="nb">complex</span><span class="p">)</span>
<span class="kc">True</span>
</pre></div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">j</span></code> is not, by itself, reserved as a special placeholder for <span class="math notranslate nohighlight">\(i\)</span>. Rather, <code class="docutils literal notranslate"><span class="pre">j</span></code> must be preceded immediately with a numerical literal (i.e. you cannot use a variable) in order for the Python interpreter to treat it as a complex number.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># `j` by itself is treated like any other character</span>
<span class="o">>>></span> <span class="n">j</span>
<span class="ne">NameError</span><span class="p">:</span> <span class="n">name</span> <span class="s1">'j'</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">defined</span>
<span class="c1"># `1j` is interpreted as the imaginary number</span>
<span class="o">>>></span> <span class="p">(</span><span class="mi">1</span><span class="n">j</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
<span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>
</pre></div>
</div>
<p>You can access <code class="docutils literal notranslate"><span class="pre">a</span></code> and <code class="docutils literal notranslate"><span class="pre">b</span></code> from <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">bj</span></code>, the real and imaginary parts of the complex number, respectively.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Accessing the real and imaginary parts of</span>
<span class="c1"># a complex number.</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="nb">complex</span><span class="p">(</span><span class="mf">1.2</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.4</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">x</span><span class="o">.</span><span class="n">real</span>
<span class="mf">1.2</span>
<span class="o">>>></span> <span class="n">x</span><span class="o">.</span><span class="n">imag</span>
<span class="o">-</span><span class="mf">3.4</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">cmath</span></code> (“complex math”) module provides a collection of mathematical functions defined for complex numbers. For a complete listing of these functions, refer to the <a class="reference external" href="https://docs.python.org/3/library/cmath.html#module-cmath">official documentation</a>.</p>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Working with numbers in Python</strong></p>
<ol class="arabic simple">
<li><p>In Python, performing an arithmetic operation, such as addition or multiplication, on two integers will return an integer, and performing an operation on two floats will return a float:</p></li>
</ol>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">2</span> <span class="o">*</span> <span class="mi">3</span>
<span class="go">6</span>
<span class="gp">>>> </span><span class="mf">2.0</span> <span class="o">*</span> <span class="mf">3.0</span>
<span class="go">6.0</span>
</pre></div>
</div>
<p>For which operation, among <code class="docutils literal notranslate"><span class="pre">+</span> <span class="pre">-</span> <span class="pre">*</span> <span class="pre">/</span> <span class="pre">**</span></code>, does this <em>not</em> hold?</p>
<ol class="arabic simple" start="2">
<li><p>What type of number will be returned if you perform a mathematical operation using an integer and a floating-point number? Does this hold for all the arithmetic operations? Determine this by trial and error.</p></li>
<li><p>Given the function <span class="math notranslate nohighlight">\(f(x) = e^{|x - 2|}\)</span>, make use of the <code class="docutils literal notranslate"><span class="pre">math</span></code> module to compute <span class="math notranslate nohighlight">\(f(-0.2)\)</span>.</p></li>
<li><p>Using Python’s syntax for scientific notation, write an expression that verifies that one trillion divided by one billion is equal to one thousand</p></li>
</ol>
</div>
</div>
<div class="section" id="Augmented-Assignment-Statements">
<h3>Augmented Assignment Statements<a class="headerlink" href="#Augmented-Assignment-Statements" title="Permalink to this headline"></a></h3>
<p>Python provides a nice “shortcut” for updating a variable via an arithmetic operation. For example, suppose you want to increase the value of <code class="docutils literal notranslate"><span class="pre">x</span></code> by 1. Currently, we would update <code class="docutils literal notranslate"><span class="pre">x</span></code> as follows:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># incrementing `x` by 1</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="o">>>></span> <span class="n">x</span>
<span class="mi">6</span>
</pre></div>
</div>
<p>We can make use of a special assignment operation <code class="docutils literal notranslate"><span class="pre">+=</span></code> to perform this update in an abbreviated way.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># using `+=` to increment `x` by 1</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1"># equivalent to: `x = x + 1`</span>
<span class="o">>>></span> <span class="n">x</span>
<span class="mi">6</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">+=</span></code> is a type of <em>augmented assignment statement</em>. In general, an augmented assignment performs a mathematical operation on a variable, and then updates that variable using the result. Augmented assignment statements are available for all of the arithmetic operations. Assuming <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">n</span></code> are both types of numbers, the following summarizes the available arithmetic augmented assignment statements that we can perform on <code class="docutils literal notranslate"><span class="pre">x</span></code>, using <code class="docutils literal notranslate"><span class="pre">n</span></code>:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+=</span> <span class="pre">n</span></code> <span class="math notranslate nohighlight">\(\rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">+</span> <span class="pre">n</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-=</span> <span class="pre">n</span></code> <span class="math notranslate nohighlight">\(\rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">-</span> <span class="pre">n</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">*=</span> <span class="pre">n</span></code> <span class="math notranslate nohighlight">\(\rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">*</span> <span class="pre">n</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/=</span> <span class="pre">n</span></code> <span class="math notranslate nohighlight">\(\rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">/</span> <span class="pre">n</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">//=</span> <span class="pre">n</span></code> <span class="math notranslate nohighlight">\(\rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">//</span> <span class="pre">n</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">**=</span> <span class="pre">n</span></code> <span class="math notranslate nohighlight">\(\rightarrow\)</span> <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">**</span> <span class="pre">n</span></code></p></li>
</ul>
</div>
<div class="section" id="Improving-The-Readability-of-Numbers">
<h3>Improving The Readability of Numbers<a class="headerlink" href="#Improving-The-Readability-of-Numbers" title="Permalink to this headline"></a></h3>
<p>Python version 3.6 introduced the ability to include underscores between the digits of a number as a visual delimiter. This character can be used to improve the readability of long numbers in your code. For example the number <code class="docutils literal notranslate"><span class="pre">662607004</span></code> can be rewritten as <code class="docutils literal notranslate"><span class="pre">662_607_004</span></code>, using <code class="docutils literal notranslate"><span class="pre">_</span></code> to delimit digits separated by orders of one thousand. Leading, trailing, or multiple underscores in a row are not allowed; otherwise this character can be included anywhere within a numerical literal.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># examples of using `_` as a visual delimiter in numbers</span>
<span class="o">>>></span> <span class="mi">1_000_000</span> <span class="c1"># this is nice!</span>
<span class="mi">1000000</span>
<span class="c1"># this is gross but is permitted</span>
<span class="o">>>></span> <span class="mf">2_3_4.5_6_7</span>
<span class="mf">234.567</span>
<span class="c1"># underscores work with all variety of numerical literals</span>
<span class="o">>>></span> <span class="mi">10_000</span><span class="n">j</span>
<span class="mi">10000</span><span class="n">j</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>Compatibility Warning</strong></p>
<p>The permitted use of the underscore character, <code class="docutils literal notranslate"><span class="pre">_</span></code>, in numerical literals was introduced in Python 3.6. Thus utilizing this syntax in your code will render it incompatible with Python 3.5 and earlier.</p>
</div>
</div>
</div>
<div class="section" id="The-Boolean-Type">
<h2>The Boolean Type<a class="headerlink" href="#The-Boolean-Type" title="Permalink to this headline"></a></h2>
<p>There are two boolean-type objects: <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code>; they belong to the built-in type <code class="docutils literal notranslate"><span class="pre">bool</span></code>. We have already seen that the <code class="docutils literal notranslate"><span class="pre">isinstance</span></code> function either returns <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>, as a given object either is or isn’t an instance of a specific type.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># the two boolean-objects: `True` and `False`</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="nb">bool</span>
<span class="c1"># `False` is a boolean-type object</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span>
<span class="kc">True</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code> must be specified with capital letters in Python. These should not be confused with strings; note that there are no quotation marks used here.</p>
<div class="section" id="Logic-Operators">
<h3>Logic Operators<a class="headerlink" href="#Logic-Operators" title="Permalink to this headline"></a></h3>
<p>Python provides familiar operators for performing basic boolean logic:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 47%" />
<col style="width: 53%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Logic Operation</p></th>
<th class="head"><p>Symbolic Operator</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">and</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">&</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">or</span></code></p></td>
<td><p>|</p></td>
</tr>
</tbody>
</table>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># demonstrating boolean-logic operators</span>
<span class="o">>>></span> <span class="kc">True</span> <span class="ow">or</span> <span class="kc">False</span>
<span class="kc">True</span>
<span class="o">>>></span> <span class="kc">True</span> <span class="ow">and</span> <span class="kc">False</span>
<span class="kc">False</span>
<span class="o">>>></span> <span class="ow">not</span> <span class="kc">False</span>
<span class="kc">True</span>
</pre></div>
</div>
<p>Operator symbols are available in place of the reserved words <code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># demonstrating the symbolic logic operators</span>
<span class="o">>>></span> <span class="kc">False</span> <span class="o">|</span> <span class="kc">True</span> <span class="c1"># equivalent to: `False or True`</span>
<span class="kc">True</span>
<span class="o">>>></span> <span class="kc">False</span> <span class="o">&</span> <span class="kc">True</span> <span class="c1"># equivalent to: `False and True`</span>
<span class="kc">False</span>
</pre></div>
</div>
<p>That being said, it is generally more “Pythonic” (i.e. in-vogue with Python users) to favor the use of the word-operators over the symbolic ones.</p>
<p>Multiple logic operators can be used in a single line and parentheses can be used to group expressions:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="kc">True</span> <span class="ow">or</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">and</span> <span class="kc">True</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Comparison statements used in basic mathematics naturally return boolean objects.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">2</span> <span class="o"><</span> <span class="mi">3</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="mf">10.5</span> <span class="o"><</span> <span class="mi">0</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="p">(</span><span class="mi">2</span> <span class="o"><</span> <span class="mi">4</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="mi">4</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">bool</span></code> type has additional utilities, which will be discussed in the “Conditional Statements” section.</p>
</div>
<div class="section" id="Boolean-Objects-are-Integers">
<h3>Boolean Objects are Integers<a class="headerlink" href="#Boolean-Objects-are-Integers" title="Permalink to this headline"></a></h3>
<p>The two boolean objects <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code> formally belong to the <code class="docutils literal notranslate"><span class="pre">int</span></code> type in addition to <code class="docutils literal notranslate"><span class="pre">bool</span></code>, and are associated with the values <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code>, respectively:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<p>As such, they can be used in mathematical expressions interchangeably with <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">3</span><span class="o">*</span><span class="kc">True</span> <span class="o">-</span> <span class="kc">False</span> <span class="c1"># equivalent to: 3*1 + 0</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="kc">True</span> <span class="o">/</span> <span class="kc">False</span> <span class="c1"># equivalent to: 1 / 0</span>
<span class="go">---------------------------------------------------------------------------</span>
<span class="go">ZeroDivisionError Traceback (most recent call last)</span>
<span class="go"><ipython-input-4-f8487d9d0863> in <module>()</span>
<span class="go">----> 1 True / False</span>
<span class="go">ZeroDivisionError: division by zero</span>
</pre></div>
</div>
<p>The purpose of having <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code> double as integers is beyond the scope of this section. It is simply useful to be aware of these facts so that this behavior is not completely alien to you as you begin to write code in Python.</p>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Boolean expressions</strong></p>
<ol class="arabic simple">
<li><p>Assuming <code class="docutils literal notranslate"><span class="pre">x</span></code> is a an integer-type, write a comparison statement that will return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">x</span></code> is an even number, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. (Hint: recall the purpose of the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator)</p></li>
<li><p>Assuming <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> are both real-valued numbers (i.e. not complex numbers), write a line of code that will return <code class="docutils literal notranslate"><span class="pre">False</span></code> if: <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> are within 0.9 of one another, and <code class="docutils literal notranslate"><span class="pre">x</span></code> is a positive number. (Hint: try writing the expression that will return <code class="docutils literal notranslate"><span class="pre">True</span></code> for this condition, and then negate it)</p></li>
<li><p>Write an expression that returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">x</span></code> is a boolean-type object or a float-type object.</p></li>
</ol>
</div>
</div>
</div>
<div class="section" id="The-None-Type">
<h2>The None-Type<a class="headerlink" href="#The-None-Type" title="Permalink to this headline"></a></h2>
<p>There is a simple type, <code class="docutils literal notranslate"><span class="pre">NoneType</span></code> that has exactly one object: <code class="docutils literal notranslate"><span class="pre">None</span></code>. <code class="docutils literal notranslate"><span class="pre">None</span></code> is used to represent “null”… nothing.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># `None` is the *only* object belonging to NoneType</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="n">NoneType</span>
</pre></div>
</div>
<p>As such, instead of checking if an object belongs to NoneType, you should simply check if the object is <code class="docutils literal notranslate"><span class="pre">None</span></code>. Python reserves <code class="docutils literal notranslate"><span class="pre">is</span></code> as an operation that checks if two objects are identical. This is different than <code class="docutils literal notranslate"><span class="pre">==</span></code>, which checks if two objects are associated with the same value or state:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Check if an object "is" None, instead</span>
<span class="c1"># of checking if it is of NoneType</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">22</span>
<span class="o">>>></span> <span class="n">x</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="kc">False</span>
<span class="o">>>></span> <span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
<span class="kc">True</span>
<span class="o">>>></span> <span class="n">y</span> <span class="o">=</span> <span class="kc">None</span>
<span class="o">>>></span> <span class="n">y</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="kc">True</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">None</span></code> appears frequently, and is often used as a placeholder in code. Here is a simple example where <code class="docutils literal notranslate"><span class="pre">None</span></code> could be useful; don’t worry that this code may not make perfect sense to you yet:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Demonstrating the use of `None` as a placeholder</span>
<span class="c1"># In this code, we want to get the first</span>
<span class="c1"># item in a list that is greater than 10, and notify</span>
<span class="c1"># the user if there is no such number</span>
<span class="n">large_num</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">for</span> <span class="n">number</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">number</span> <span class="o">></span> <span class="mi">10</span><span class="p">:</span>
<span class="n">large_num</span> <span class="o">=</span> <span class="n">number</span>
<span class="k">break</span>
<span class="k">if</span> <span class="n">large_num</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"The list did not contain any number larger than 10"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="Strings">
<h2>Strings<a class="headerlink" href="#Strings" title="Permalink to this headline"></a></h2>
<div class="section" id="Introducing-the-string-type">
<h3>Introducing the string type<a class="headerlink" href="#Introducing-the-string-type" title="Permalink to this headline"></a></h3>
<p>The string type is used to store written characters. A string can be formed using:</p>
<ul class="simple">
<li><p>single quotes: <code class="docutils literal notranslate"><span class="pre">'Hello</span> <span class="pre">world'</span></code></p></li>
<li><p>double quotes: <code class="docutils literal notranslate"><span class="pre">"Hello</span> <span class="pre">world"</span></code></p></li>
<li><p>triple quotes: <code class="docutils literal notranslate"><span class="pre">"""Hello</span> <span class="pre">world"""</span></code> or <code class="docutils literal notranslate"><span class="pre">'''Hello</span> <span class="pre">world'''</span></code></p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Strings contain written characters, even those</span>
<span class="c1"># not found in the english alphabet!</span>
<span class="o">>>></span> <span class="s2">"hello, 你好, Olá, 123"</span>
<span class="s1">'hello, 你好, Olá, 123'</span>
</pre></div>
</div>
<p>By default, Python 3 uses <a class="reference external" href="https://docs.python.org/3/howto/unicode.html#unicode-howto">UTF-8 unicode</a> to represent this wide variety of characters. Don’t worry about this detail beyond making note of it, for now.</p>
<p>Strings belong to the built-in <code class="docutils literal notranslate"><span class="pre">str</span></code> type, which can be used to convert non-string objects into strings.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># the type `str`</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>
<span class="nb">str</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">(</span><span class="s2">"83"</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
<span class="kc">True</span>
<span class="c1"># Using the type `str` to convert non-string objects</span>
<span class="c1"># into strings.</span>
<span class="o">>>></span> <span class="nb">str</span><span class="p">(</span><span class="mf">10.34</span><span class="p">)</span>
<span class="s1">'10.34'</span>
<span class="o">>>></span> <span class="nb">str</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="s1">'True'</span>
</pre></div>
</div>
<p>Once a string is formed, it cannot be changed (without creating an entirely new string). Thus a given string object cannot be “mutated” - a string is an <em>immutable</em> object.</p>
<p>As the string stores a <em>sequence</em> of characters, Python provides a means for accessing individual characters and subsequences of characters from a string:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sentence</span> <span class="o">=</span> <span class="s2">"The cat in the hat."</span>
<span class="gp">>>> </span><span class="n">sentence</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">'T'</span>
<span class="gp">>>> </span><span class="n">sentence</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="go">'The'</span>
</pre></div>
</div>
<p>Strings are not the only sequence-type in Python; lists and tuples are examples of sequences as well. We will reserve a separate section to learn about the common interface that Python has for all of its types that are sequential in nature, including the “indexing” and “slicing” demonstrated here.</p>
</div>
<div class="section" id="String-essentials">
<h3>String essentials<a class="headerlink" href="#String-essentials" title="Permalink to this headline"></a></h3>
<p>We will only scratch the surface with strings, touching on some essentials. Please refer to the <a class="reference external" href="https://docs.python.org/3/tutorial/introduction.html#strings">official Python tutorial</a> for a more extensive, but still informal, overview of strings.</p>
<p>In a string, <code class="docutils literal notranslate"><span class="pre">\n</span></code> is treated as a single character. It denotes a new-line in a string, and will be rendered thusly when the string is printed. Similarly, <code class="docutils literal notranslate"><span class="pre">\t</span></code> will render as a tab-character.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># using `\n` to create a newline</span>
<span class="o">>>></span> <span class="nb">print</span><span class="p">(</span><span class="s2">"hi...</span><span class="se">\n</span><span class="s2">...bye"</span><span class="p">)</span>
<span class="n">hi</span><span class="o">...</span>
<span class="o">...</span><span class="n">bye</span>
</pre></div>
</div>
<p>Using triple quotes allows you to write a block-string, meaning that you can include text on multiple lines, and it is all still treated as one string:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># using triple-quotes to write a multi-line string</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="s2">""" I am a string.</span>
<span class="s2">I am part of the same string.</span>
<span class="s2"> me... too!"""</span>
<span class="o">>>></span> <span class="n">x</span>
<span class="s1">' I am a string.</span><span class="se">\n</span><span class="s1">I am part of the same string.</span><span class="se">\n</span><span class="s1"> me... too!'</span>
</pre></div>
</div>
<p>Python’s strings have a large number of fantastic, built-in functions available to them. It is <em>very important</em> that you familiarize yourself with these functions by looking over <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#string-methods">the official documentation</a>. To demonstrate a few of these:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># demonstrating a few of the built-in functions for strings</span>
<span class="o">>>></span> <span class="s2">"hello"</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span>
<span class="s1">'Hello'</span>
<span class="c1"># join a list of strings, using "..."</span>
<span class="o">>>></span> <span class="s2">"..."</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">"item1"</span><span class="p">,</span> <span class="s2">"item2"</span><span class="p">,</span> <span class="s2">"item3"</span><span class="p">])</span>
<span class="s1">'item1...item2...item3'</span>
<span class="c1"># split a string wherever ", " occurs</span>
<span class="o">>>></span> <span class="s1">'item1, item2, item3'</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">", "</span><span class="p">)</span>
<span class="p">[</span><span class="s1">'item1'</span><span class="p">,</span> <span class="s1">'item2'</span><span class="p">,</span> <span class="s1">'item3'</span><span class="p">]</span>
<span class="c1"># does this string end with ".py"?</span>
<span class="o">>>></span> <span class="s2">"script.py"</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">".py"</span><span class="p">)</span>
<span class="kc">True</span>
<span class="c1"># does this string start with "sc"?</span>
<span class="o">>>></span> <span class="s2">"script.py"</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">"sc"</span><span class="p">)</span>
<span class="kc">True</span>
<span class="c1"># insert objects into a string, in its</span>
<span class="c1"># "formatting" fields {}</span>
<span class="o">>>></span> <span class="s2">"x: </span><span class="si">{}</span><span class="s2">, y: </span><span class="si">{}</span><span class="s2">, z: </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.2</span><span class="p">,</span> <span class="mf">8.4</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">)</span>
<span class="s1">'x: 3.2, y: 8.4, z: -1.0'</span>
<span class="c1"># Are the characters in the string</span>
<span class="c1"># numerical digits?</span>
<span class="o">>>></span> <span class="s2">"7"</span><span class="o">.</span><span class="n">isdigit</span><span class="p">()</span>
<span class="kc">True</span>
</pre></div>
</div>
</div>
<div class="section" id="Formatting-strings">
<h3>Formatting strings<a class="headerlink" href="#Formatting-strings" title="Permalink to this headline"></a></h3>
<p>Python provides multiple syntaxes for formatting strings; these permit us to do things like programmatically inject the values of variables into strings, align fields using whitespace, and control the number of decimal places with which numbers are displayed in a string. This section is designed to simply expose the reader to the different varieties of string-formatting.</p>
<p><a class="reference external" href="https://pyformat.info">pyformat.info</a> is the best resource to consult to see an exhaustive (but still intuitive) treatment of string-formatting in Python. You can also refer to the official documentation <a class="reference external" href="https://docs.python.org/3/library/string.html#format-examples">here</a>.</p>
<p>In Python 3, you can leverage the <code class="docutils literal notranslate"><span class="pre">format</span></code> method towards this end:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># using `format` to replace placeholders with values</span>
<span class="o">>>></span> <span class="s2">"</span><span class="si">{name}</span><span class="s2"> is </span><span class="si">{age}</span><span class="s2"> years old"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"Bruce"</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">80</span><span class="p">)</span>
<span class="s1">'Bruce is 80 years old'</span>
<span class="c1"># padding a string with leading-spaces so that it has at least 8 characters</span>
<span class="o">>>></span> <span class="s2">"</span><span class="si">{item:>8}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="s2">"stew"</span><span class="p">)</span>
<span class="s1">' stew'</span>
</pre></div>
</div>
<p>Note that you may encounter the use of the cryptic <code class="docutils literal notranslate"><span class="pre">%</span></code> operator to format strings to the same effect:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># using `%` to format strings (avoid this)</span>
<span class="o">>>></span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"Selina"</span>
<span class="o">>>></span> <span class="s2">"My name is </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="n">name</span>
<span class="s1">'My name is Selina'</span>
</pre></div>
</div>
<p>this is a relic of Python 2; it is recommend that you avoid this formatting syntax.</p>
<p>If you are using Python 3.6 or beyond, then you have the luxury of being able to use f-strings, which provide a supremely convenient means for formatting strings. Here is an example of an f-string in action:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># an example of an 'f-string'</span>
<span class="o">>>></span> <span class="n">batman</span> <span class="o">=</span> <span class="mi">12</span>
<span class="o">>>></span> <span class="n">catwoman</span> <span class="o">=</span> <span class="mi">10</span>
<span class="o">>>></span> <span class="sa">f</span><span class="s2">"Batman has </span><span class="si">{</span><span class="n">batman</span><span class="si">}</span><span class="s2"> apples. Catwoman has </span><span class="si">{</span><span class="n">catwoman</span><span class="si">}</span><span class="s2"> apples. Together, they have </span><span class="si">{</span><span class="n">batman</span> <span class="o">+</span> <span class="n">catwoman</span><span class="si">}</span><span class="s2"> apples"</span>
<span class="s1">'Batman has 12 apples. Catwoman has 10 apples. Together, they have 22 apples'</span>
</pre></div>
</div>
<p>See that an f-string has a special syntax; an f-string is denoted by preceding the opening quotation mark with the lowercase f character:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># this is a typical empty string</span>
<span class="o">>>></span> <span class="s2">""</span>
<span class="s1">''</span>
<span class="c1"># this is an empty f-string</span>
<span class="o">>>></span> <span class="sa">f</span><span class="s2">""</span>
<span class="s1">''</span>
</pre></div>
</div>
<p>An f-string is special because it permits us to write Python code <em>within</em> a string; any expression within curly brackets, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, will be executed as Python code, and the resulting value will be converted to a string and inserted into the f-string at that position.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mf">7.9</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"x is a </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="si">}</span><span class="s2">-number. Its value is </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">. The statement 'x is greater than 5' is </span><span class="si">{</span><span class="n">x</span> <span class="o">></span> <span class="mi">5</span><span class="si">}</span><span class="s2">"</span>
<span class="go">"x is a <class 'float'>-number. Its value is 7.9. The statement 'x is greater than 5' is True"</span>
</pre></div>
</div>
<p>As seen in the preceding examples, this permits us to elegantly include variables in our strings and even do things like call functions within the string construction syntax.</p>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>f-string Compatibility</strong>:</p>
<p>The ‘f-string’ syntax was introduced in Python 3.6. It is not available in earlier versions of Python.</p>
</div>
</div>
<div class="section" id="Official-documentation-for-strings">
<h3>Official documentation for strings<a class="headerlink" href="#Official-documentation-for-strings" title="Permalink to this headline"></a></h3>
<p>It is highly recommended that you take time to read over all of the functions that are built-in to a string.</p>
<ul class="simple">
<li><p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#string-methods">Built-in functions for strings</a></p></li>
<li><p><a class="reference external" href="https://docs.python.org/3/library/string.html#format-examples">Formatting strings</a></p></li>
</ul>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Strings</strong></p>
<p>To answer some of the following questions, you will need to peruse the documentation for the built-in functions of strings. It may take a bit of experimentation to understand the documentation’s use of square-brackets to indicate optional inputs for a function.</p>
<ol class="arabic simple">
<li><p>Use a function that will take the string <code class="docutils literal notranslate"><span class="pre">"cat"</span></code>, and returns the string <code class="docutils literal notranslate"><span class="pre">"</span>   <span class="pre">cat</span>    <span class="pre">"</span></code> (which has a length of 11, including the letters c, a, t). Now, change the way you call the function so that it returns <code class="docutils literal notranslate"><span class="pre">"----cat----"</span></code> instead.</p></li>
<li><p>Replace the first three periods of this string with a space-character: <code class="docutils literal notranslate"><span class="pre">"I.am.aware.that.spaces.are.a.thing"</span></code></p></li>
</ol>
<ol class="arabic simple" start="3">
<li><p>Remove the whitespace from both ends of: <code class="docutils literal notranslate"><span class="pre">"</span>  <span class="pre">basket</span>    <span class="pre">"</span></code></p></li>
</ol>
<ol class="arabic simple" start="4">
<li><p>Create a string that will print as (the second line begins with a tab-character):</p></li>
</ol>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Hello
over there
</pre></div>
</div>
<ol class="arabic simple" start="5">
<li><p>Convert the integer <code class="docutils literal notranslate"><span class="pre">12</span></code> to the string <code class="docutils literal notranslate"><span class="pre">"12"</span></code>.</p></li>
<li><p>Only kids 13 and up are allowed to see Wayne’s World. Given the variables <code class="docutils literal notranslate"><span class="pre">name</span></code> (a string) and <code class="docutils literal notranslate"><span class="pre">age</span></code> (an integer), use an f-string that will display: “NAME is old enough to watch the movie: BOOL”, where NAME is to be replaced with the kid’s name, and BOOL should be <code class="docutils literal notranslate"><span class="pre">True</span></code> if the kid is at least 13 years old, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p></li>
</ol>
</div>
</div>
</div>
<div class="section" id="Lists">
<h2>Lists<a class="headerlink" href="#Lists" title="Permalink to this headline"></a></h2>
<p>A <code class="docutils literal notranslate"><span class="pre">list</span></code> is a type of Python object that allows us to store a sequence of other objects. One of its major utilities is that it provides us with means for updating the contents of a list later on.</p>
<p>A list object is created using square-brackets, and its contents are separated by commas: <code class="docutils literal notranslate"><span class="pre">[item1,</span> <span class="pre">item2,</span> <span class="pre">...,</span> <span class="pre">itemN]</span></code>. Its contents need not be of the same type of object.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># a list-type object stores a sequence of other objects</span>
<span class="o">>>></span> <span class="p">[</span><span class="mf">3.5</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mf">3.5</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="s2">"hello"</span><span class="p">]</span>
<span class="p">[</span><span class="mf">3.5</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mf">3.5</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">'hello'</span><span class="p">]</span>
<span class="o">>>></span> <span class="nb">type</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="nb">list</span>
<span class="o">>>></span> <span class="nb">isinstance</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="nb">list</span><span class="p">)</span>
<span class="kc">True</span>
<span class="c1"># constructing an empty list</span>
<span class="o">>>></span> <span class="p">[]</span>
<span class="p">[]</span>
<span class="c1"># constructing a list with only one member</span>
<span class="o">>>></span> <span class="p">[</span><span class="s2">"hello"</span><span class="p">]</span>
<span class="p">[</span><span class="s2">"hello"</span><span class="p">]</span>
</pre></div>
</div>
<p>You can also include variables, equations, and other Python expressions in the list constructor; Python will simplify these expressions and construct the list with the resulting objects.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># the list constructor will simplify expressions</span>
<span class="c1"># and store their resulting objects</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="s2">"hello"</span>
<span class="o">>>></span> <span class="p">[</span><span class="mi">2</span> <span class="o"><</span> <span class="mi">3</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">capitalize</span><span class="p">(),</span> <span class="mi">5</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]]</span>
<span class="p">[</span><span class="kc">True</span><span class="p">,</span> <span class="s1">'Hello'</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]]</span>
</pre></div>
</div>
<p>The built-in <code class="docutils literal notranslate"><span class="pre">list</span></code> type can be used to convert other types of sequences (and more generally, any <em>iterable</em> object, which we will discuss later) into a list:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># `list` forms a list out of the contents of other sequences</span>
<span class="o">>>></span> <span class="nb">list</span><span class="p">(</span><span class="s2">"apple"</span><span class="p">)</span>
<span class="p">[</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'p'</span><span class="p">,</span> <span class="s1">'p'</span><span class="p">,</span> <span class="s1">'l'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">]</span>
</pre></div>
</div>
<div class="section" id="Lists-are-sequences">
<h3>Lists are sequences<a class="headerlink" href="#Lists-are-sequences" title="Permalink to this headline"></a></h3>
<p>Like a string, the ordering of a list’s contents matters, meaning that a list is sequential in nature.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># A list's ordering matters</span>
<span class="o">>>></span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s2">"a"</span><span class="p">,</span> <span class="kc">True</span><span class="p">]</span> <span class="o">==</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="s2">"a"</span><span class="p">]</span>
<span class="kc">False</span>
</pre></div>
</div>
<p>Thus a list supports the same mechanism for accessing its contents, via indexing and slicing, as does a string. Indexing and slicing will be covered in detail in the next section.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Accessing the contents of a list with indexing and slicing</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
<span class="c1"># `x` contains five items</span>
<span class="o">>>></span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mi">5</span>
<span class="c1"># access the 0th item in the list via "indexing"</span>
<span class="o">>>></span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="mi">2</span>
<span class="c1"># access a subsequence of the list via "slicing"</span>
<span class="o">>>></span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="Lists-can-be-“mutated”">
<h3>Lists can be “mutated”<a class="headerlink" href="#Lists-can-be-“mutated”" title="Permalink to this headline"></a></h3>
<p>We will encounter other types of containers in Python, what makes the list stand out is that the <em>contents of a list can be changed after the list has already been constructed</em>. Thus a list is an example of a <em>mutable</em> object.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># changing a list after it has been constructed</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
<span class="c1"># "set" the string 'apple' into position 1 of `x`</span>
<span class="o">>>></span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"apple"</span>
<span class="o">>>></span> <span class="n">x</span>
<span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="s1">'apple'</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
<span class="c1"># replace a subsequence of `y`</span>
<span class="o">>>></span> <span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">y</span>
<span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
</pre></div>
</div>
<p>The built-in list-functions “append” and “extend” allow us to add one item and multiple items to the end of a list, respectively:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
<span class="go"># use `append` to add a single object to the end of a list</span>
<span class="gp">>>> </span><span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"moo"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">x</span>
<span class="go">[2, 4, 6, 8, 10, 'moo']</span>
<span class="go"># use `extend` to add a sequence of items to the end of a list</span>
<span class="gp">>>> </span><span class="n">x</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">None</span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">x</span>
<span class="go">[2, 4, 6, 8, 10, 'moo', True, False, None]</span>
</pre></div>