Repository navigation
Expand file tree
/
Copy pathBasic_Objects.html
More file actions
1432 lines (1226 loc) · 100 KB
/
Copy pathBasic_Objects.html
File metadata and controls
1432 lines (1226 loc) · 100 KB
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>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic Object Types — Python Like You Mean It 0.9.1 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<link rel="index" title="Index"
href="../genindex.html"/>
<link rel="search" title="Search" href="../search.html"/>
<link rel="top" title="Python Like You Mean It 0.9.1 documentation" href="../index.html"/>
<link rel="up" title="Module 2: The Essentials of Python" href="../module_2.html"/>
<link rel="next" title="Sequence Types" href="SequenceTypes.html"/>
<link rel="prev" title="Module 2: The Essentials of Python" href="../module_2.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<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">
0.9.1
</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="main navigation">
<p class="caption"><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>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#The-Boolean-Type">The Boolean Type</a></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="#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="#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_3.html">Module 3: The Essentials of NumPy</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<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="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</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.ipynb.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,
.nboutput.nblast {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast + .nbinput {
margin-top: -19px;
}
/* nice headers on first paragraph of info/warning boxes */
.admonition .first {
margin: -12px;
padding: 6px 12px;
margin-bottom: 12px;
color: #fff;
line-height: 1;
display: block;
}
.admonition.warning .first {
background: #f0b37e;
}
.admonition.note .first {
background: #6ab0de;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
</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="first fa fa-exclamation-circle"><strong>Note</strong>:</p>
<p class="last">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>numbers (integers, floating-point numbers, and complex numbers)</li>
<li>booleans</li>
<li>the “null” type</li>
<li>strings</li>
<li>lists</li>
</ul>
<p>The built-in function <code class="docutils literal"><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"><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"><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="bp">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 border="1" class="docutils">
<colgroup>
<col width="19%" />
<col width="81%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operation</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">y</span></code></td>
<td>Sum of two numbers</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span></code></td>
<td>Difference of two numbers</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">*</span> <span class="pre">y</span></code></td>
<td>Product of two numbers</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code></td>
<td>Quotient of two numbers</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">//</span> <span class="pre">y</span></code></td>
<td>Quotient of two numbers, returned as an integer</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code></td>
<td><code class="docutils literal"><span class="pre">x</span></code> “modulo”: <code class="docutils literal"><span class="pre">y</span></code>: The remainder of <code class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">**</span> <span class="pre">y</span></code></td>
<td><code class="docutils literal"><span class="pre">x</span></code> raised to the power <code class="docutils literal"><span class="pre">y</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">-x</span></code></td>
<td>A negated number</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">abs(x)</span></code></td>
<td>The absolute value of a number</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code></td>
<td>Check if two numbers have the same value</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">!=</span> <span class="pre">y</span></code></td>
<td>Check if two numbers have different values</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">></span> <span class="pre">y</span></code></td>
<td>Check if <code class="docutils literal"><span class="pre">x</span></code> is greater than <code class="docutils literal"><span class="pre">y</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">>=</span> <span class="pre">y</span></code></td>
<td>Check if <code class="docutils literal"><span class="pre">x</span></code> is greater than or equal to <code class="docutils literal"><span class="pre">y</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre"><</span> <span class="pre">y</span></code></td>
<td>Check if <code class="docutils literal"><span class="pre">x</span></code> is less than <code class="docutils literal"><span class="pre">y</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre"><=</span> <span class="pre">y</span></code></td>
<td>Check if <code class="docutils literal"><span class="pre">x</span></code> is less than or equal to <code class="docutils literal"><span class="pre">y</span></code></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"><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</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="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="bp">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"><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"><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"><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>
<hr class="docutils" />
<p><strong>Reading Comprehension: Understanding the modulo operator</strong></p>
<p>The modulo operator, <code class="docutils literal"><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"><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"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code>. For example:</p>
<ul class="simple">
<li><span class="math">\(\frac{3}{2} = 1 + \frac{1}{2}\)</span>. 2 “goes into” 3 one time,
leaving a remainder of 1. Thus <code class="docutils literal"><span class="pre">3</span> <span class="pre">%</span> <span class="pre">2</span></code> returns <code class="docutils literal"><span class="pre">1</span></code></li>
<li><span class="math">\(\frac{9}{3} = 3\)</span>. 3 “goes into” 9 three times, and leaves no
remainder. Thus <code class="docutils literal"><span class="pre">9</span> <span class="pre">%</span> <span class="pre">3</span></code> returns <code class="docutils literal"><span class="pre">0</span></code></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><code class="docutils literal"><span class="pre">1</span> <span class="pre">%</span> <span class="pre">5</span></code></li>
<li><code class="docutils literal"><span class="pre">2</span> <span class="pre">%</span> <span class="pre">5</span></code></li>
<li><code class="docutils literal"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">1</span></code></li>
<li><code class="docutils literal"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">2</span></code></li>
<li><code class="docutils literal"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">3</span></code></li>
<li><code class="docutils literal"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">4</span></code></li>
<li><code class="docutils literal"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">5</span></code></li>
<li><code class="docutils literal"><span class="pre">22</span> <span class="pre">%</span> <span class="pre">6</span></code></li>
</ol>
<p>Now, given any integer, <code class="docutils literal"><span class="pre">n</span></code>, what are the possible values that
<code class="docutils literal"><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"><span class="pre">n</span> <span class="pre">%</span> <span class="pre">2</span></code>.</p>
<hr class="docutils" />
<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"><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">\(\dots, -3, -2, -1, 0, 1, 2, 3, \dots\)</span>.</p>
<p>Integers belong to the built-in type <code class="docutils literal"><span class="pre">int</span></code>, which can be used convert
objects to integers:</p>
<div class="highlight-python"><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"><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"><span class="pre">float</span></code>, which can be used
to convert objects to floats:</p>
<div class="highlight-python"><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="bp">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="bp">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"><span class="pre">e</span></code> is used to represent <span class="math">\(\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">\(1.38 \times 10^{-4} \rightarrow\)</span> <code class="docutils literal"><span class="pre">1.38e-04</span></code></p>
<p><span class="math">\(-4.2 \times 10^{10} \rightarrow\)</span> <code class="docutils literal"><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"><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 (32
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 23 digits, but when this number is converted to a float, it
only retains 16 decimal places in scientific notation:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Demonstrating the finite-precision of a float.</span>
<span class="c1"># An integer with 23 digits - Python will use as</span>
<span class="c1"># much memory as needed to store an integer</span>
<span class="o">>>></span> <span class="mi">18181818181818181818181</span>
<span class="mi">18181818181818181818181</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="mi">18181818181818181818181</span><span class="p">)</span>
<span class="mf">1.8181818181818182e+22</span>
</pre></div>
</div>
<p>The computer cannot keep track of those last 6 decimal places, because
doing so would require more than 8 bytes of memory to store the entire
value of that float. As such, attempting to modify a number in decimal
places beyond its numerical precision does not have any effect:</p>
<div class="highlight-python"><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"><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="bp">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"><span class="pre">math</span></code> module has a very nice function for this; <code class="docutils literal"><span class="pre">math.isclose</span></code>
will check if the relative difference between two numbers is less than
<span class="math">\(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 comparing values that are close to 0,
we will check if their absolute difference is sufficiently small:</p>
<div class="highlight-python"><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="bp">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"><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"><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">\(a + bi\)</span>, where <span class="math">\(a\)</span> and <span class="math">\(b\)</span> are real-valued numbers,
and <span class="math">\(i\)</span> is defined to be the number that satisfies the
relationship <span class="math">\(i^2 = -1\)</span>. Because no real-valued number satisfies
this relationship, <span class="math">\(i\)</span> is called the “imaginary number”.</p>
<p>Weirdo electrical engineers use the symbol <span class="math">\(j\)</span> in place of
<span class="math">\(i\)</span>, which is why Python displays the complex number
<span class="math">\(2 + 3i\)</span> as <code class="docutils literal"><span class="pre">2+3j</span></code> (this is actually because <span class="math">\(i\)</span> typically
denotes current; we like electrical engineers too).</p>
<p>Along with the <code class="docutils literal"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">bj</span></code> syntax, built-in type <code class="docutils literal"><span class="pre">complex</span></code> can be used
to create complex-type numbers:</p>
<div class="highlight-python"><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">3j</span>
<span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">3j</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">3j</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">3j</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">4j</span><span class="p">,</span> <span class="nb">complex</span><span class="p">)</span>
<span class="bp">True</span>
</pre></div>
</div>
<p>You can access <code class="docutils literal"><span class="pre">a</span></code> and <code class="docutils literal"><span class="pre">b</span></code> from <code class="docutils literal"><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"><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"><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>
<hr class="docutils" />
<p><strong>Reading Comprehension: Working with numbers in Python</strong></p>
<p>1. 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>
<div class="highlight-python"><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"><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>
<p>2. 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>
<p>3. Given the function <span class="math">\(f(x) = e^{|x - 2|}\)</span>, make use of the
<code class="docutils literal"><span class="pre">math</span></code> module to compute <span class="math">\(f(-0.2)\)</span>.</p>
</div>
<hr class="docutils" />
<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"><span class="pre">x</span></code> by 1. Currently, we would update <code class="docutils literal"><span class="pre">x</span></code> as follows:</p>
<div class="highlight-python"><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"><span class="pre">+=</span></code> to perform this
update in an abbreviated way.</p>
<div class="highlight-python"><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"><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"><span class="pre">x</span></code> and <code class="docutils literal"><span class="pre">n</span></code> are both types numbers, the following summarizes the
available arithmetic augmented assignment statements that we can perform
on <code class="docutils literal"><span class="pre">x</span></code>, using <code class="docutils literal"><span class="pre">n</span></code>:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">+=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">+</span> <span class="pre">n</span></code></li>
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">-=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">-</span> <span class="pre">n</span></code></li>
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">*=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">*</span> <span class="pre">n</span></code></li>
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">/=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">/</span> <span class="pre">n</span></code></li>
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">//=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">//</span> <span class="pre">n</span></code></li>
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">**=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">**</span> <span class="pre">n</span></code></li>
</ul>
</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"><span class="pre">True</span></code> and <code class="docutils literal"><span class="pre">False</span></code>; they belong
to the built-in type <code class="docutils literal"><span class="pre">bool</span></code>. We have already seen that the
<code class="docutils literal"><span class="pre">isinstance</span></code> function either returns <code class="docutils literal"><span class="pre">True</span></code> or <code class="docutils literal"><span class="pre">False</span></code>, as a given
object either is or isn’t and instance of a specific type.</p>
<div class="highlight-python"><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="bp">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="bp">False</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span>
<span class="bp">True</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">True</span></code> and <code class="docutils literal"><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>
<p>Python provides familiar operators for performing basic boolean logic:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">and</span></code></li>
<li><code class="docutils literal"><span class="pre">or</span></code></li>
<li><code class="docutils literal"><span class="pre">not</span></code></li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># demonstrating boolean-logic operators</span>
<span class="o">>>></span> <span class="bp">True</span> <span class="ow">or</span> <span class="bp">False</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="bp">True</span> <span class="ow">and</span> <span class="bp">False</span>
<span class="bp">False</span>
<span class="o">>>></span> <span class="ow">not</span> <span class="bp">False</span>
<span class="bp">True</span>
</pre></div>
</div>
<p>Grouping with parentheses also works, and multiple operators can be used
in a single line:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="bp">True</span> <span class="ow">or</span> <span class="bp">False</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">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"><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"><span class="pre">bool</span></code> type has additional utilities, which will be discussed in
the “Conditional Statements” section.</p>
<hr class="docutils" />
<p><strong>Reading Comprehension: Boolean expressions</strong></p>
<p>1. Assuming <code class="docutils literal"><span class="pre">x</span></code> is a an integer-type, write a comparison statement
that will return <code class="docutils literal"><span class="pre">True</span></code> if <code class="docutils literal"><span class="pre">x</span></code> is an even number, and <code class="docutils literal"><span class="pre">False</span></code>
otherwise. (Hint: recall the purpose of the <code class="docutils literal"><span class="pre">%</span></code> operator)</p>
<p>2. Assuming <code class="docutils literal"><span class="pre">x</span></code> and <code class="docutils literal"><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"><span class="pre">False</span></code> if:
<code class="docutils literal"><span class="pre">x</span></code> and <code class="docutils literal"><span class="pre">y</span></code> are within 0.9 of one another, and <code class="docutils literal"><span class="pre">x</span></code> is a positive
number. (Hint: try writing the expression that will return <code class="docutils literal"><span class="pre">True</span></code> for
this condition, and then negate it)</p>
<p>3. Write an expression that returns <code class="docutils literal"><span class="pre">True</span></code> if <code class="docutils literal"><span class="pre">x</span></code> is a boolean-type
object or a float-type object.</p>
</div>
<hr class="docutils" />
<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"><span class="pre">NoneType</span></code> that has exactly one object:
<code class="docutils literal"><span class="pre">None</span></code>. <code class="docutils literal"><span class="pre">None</span></code> is used to represent “null”… nothing.</p>
<div class="highlight-python"><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="bp">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"><span class="pre">None</span></code>. Python reserves <code class="docutils literal"><span class="pre">is</span></code> as
an operation that checks if two objects are identical. This is different
than <code class="docutils literal"><span class="pre">==</span></code>, which checks if two objects are associated with the same
value or state:</p>
<div class="highlight-python"><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="bp">None</span>
<span class="bp">False</span>
<span class="o">>>></span> <span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="n">y</span> <span class="o">=</span> <span class="bp">None</span>
<span class="o">>>></span> <span class="n">y</span> <span class="ow">is</span> <span class="bp">None</span>
<span class="bp">True</span>
</pre></div>
</div>
<p><code class="docutils literal"><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"><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"><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="bp">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="bp">None</span><span class="p">:</span>
<span class="k">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>single quotes: <code class="docutils literal"><span class="pre">'Hello</span> <span class="pre">world'</span></code></li>
<li>double quotes: <code class="docutils literal"><span class="pre">"Hello</span> <span class="pre">world"</span></code></li>
<li>triple quotes: <code class="docutils literal"><span class="pre">"""Hello</span> <span class="pre">world"""</span></code> or <code class="docutils literal"><span class="pre">'''Hello</span> <span class="pre">world'''</span></code></li>
</ul>
<div class="highlight-python"><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"><span class="pre">str</span></code> type, which can be used to
convert non-string objects into strings.</p>
<div class="highlight-python"><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="bp">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="bp">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"><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"><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"><span class="pre">\t</span></code> will render as a tab-character.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># using `\n` to create a newline</span>
<span class="o">>>></span> <span class="k">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"><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"><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="bp">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="bp">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: {}, y: {}, z: {}"</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="bp">True</span>
</pre></div>
</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>
<ul class="simple">
<li><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#string-methods">Built-in functions for
strings</a></li>
<li><a class="reference external" href="https://docs.python.org/3.4/library/string.html#format-specification-mini-language">Specification for formatting
strings</a></li>
</ul>
<hr class="docutils" />
<p><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>
<p>1. Use a function that will take the string <code class="docutils literal"><span class="pre">"cat"</span></code>, and returns the
string <code class="docutils literal"><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"><span class="pre">"----cat----"</span></code> instead.</p>
<p>2. Replace the first three periods of this string with a
space-character: <code class="docutils literal"><span class="pre">"I.am.aware.that.spaces.are.a.thing"</span></code></p>
<ol class="arabic simple" start="3">
<li>Remove the whitespace from both ends of: <code class="docutils literal"><span class="pre">"</span>  <span class="pre">basket</span>    <span class="pre">"</span></code></li>
</ol>