-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2021-03-06_Python-Study-Guide-for-a-JavaScript-Programmer-5cfdf3d2bdfb.html
More file actions
1037 lines (1034 loc) · 81.9 KB
/
2021-03-06_Python-Study-Guide-for-a-JavaScript-Programmer-5cfdf3d2bdfb.html
File metadata and controls
1037 lines (1034 loc) · 81.9 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>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Python Study Guide for a JavaScript Programmer</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<article class="h-entry">
<header>
<h1 class="p-name">Python Study Guide for a JavaScript Programmer</h1>
</header>
<section data-field="subtitle" class="p-summary">
A guide to commands in Python from what you know in JavaScript
</section>
<section data-field="body" class="e-content">
<section name="24ac" class="section section--body section--first">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="03e2" id="03e2" class="graf graf--h3 graf--leading graf--title">Python Study Guide for a
JavaScript Programmer</h3>
<figure name="b836" id="b836" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="1*3V9VOfPk_hrFdbEAd3j-QQ.png" data-width="985" data-height="662"
data-is-featured="true" src="https://cdn-images-1.medium.com/max/800/1*3V9VOfPk_hrFdbEAd3j-QQ.png">
</figure>
<h3 name="5a5b" id="5a5b" class="graf graf--h3 graf-after--figure">Applications of Tutorial & Cheat
Sheet Respectivley (At Bottom Of Tutorial):</h3>
<h3 name="b0f3" id="b0f3" class="graf graf--h3 graf-after--h3">Basics</h3>
<ul class="postList">
<li name="f893" id="f893" class="graf graf--li graf-after--h3"><strong
class="markup--strong markup--li-strong">PEP8</strong> : Python Enhancement Proposals, style-guide
for Python.</li>
<li name="c0bf" id="c0bf" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">print</code> is the equivalent of <code
class="markup--code markup--li-code">console.log</code>.</li>
</ul>
<blockquote name="01c1" id="01c1"
class="graf graf--blockquote graf--startsWithSingleQuote graf-after--li">‘print() == console.log()’
</blockquote>
<h3 name="117c" id="117c" class="graf graf--h3 graf-after--blockquote"><code
class="markup--code markup--h3-code">#</code> is used to make comments in your code.</h3>
<pre name="02b0" id="02b0"
class="graf graf--pre graf-after--h3">def foo():<br> """<br> The foo function does many amazing things that you<br> should not question. Just accept that it exists and<br> use it with caution.<br> """<br> secretThing()</pre>
<blockquote name="0e6c" id="0e6c" class="graf graf--blockquote graf-after--pre graf--trailing"><em
class="markup--em markup--blockquote-em">Python has a built in help function that let’s you see a
description of the source code without having to navigate to it… “-SickNasty … Autor Unknown”</em>
</blockquote>
</div>
</div>
</section>
<section name="f301" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="e708" id="e708" class="graf graf--h3 graf--leading">Numbers</h3>
<ul class="postList">
<li name="4060" id="4060" class="graf graf--li graf-after--h3">Python has three types of numbers:</li>
</ul>
<ol class="postList">
<li name="8aef" id="8aef" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">Integer</strong></li>
<li name="723f" id="723f" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">Positive and Negative Counting Numbers.</strong></li>
</ol>
<p name="be62" id="be62" class="graf graf--p graf-after--li">No Decimal Point</p>
<blockquote name="58b4" id="58b4" class="graf graf--blockquote graf-after--p">Created by a literal
non-decimal point number … <strong class="markup--strong markup--blockquote-strong">or</strong> … with
the <code class="markup--code markup--blockquote-code"><em
class="markup--em markup--blockquote-em">int()</em></code> constructor.</blockquote>
<pre name="7bd4" id="7bd4"
class="graf graf--pre graf-after--blockquote">print(3) # => 3 <br>print(int(19)) # => 19 <br>print(int()) # => 0</pre>
<p name="9504" id="9504" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">3. Complex Numbers</strong></p>
<blockquote name="8129" id="8129" class="graf graf--blockquote graf-after--p">Consist of a real part and
imaginary part.</blockquote>
<h4 name="89c4" id="89c4" class="graf graf--h4 graf-after--blockquote">Boolean is a subtype of integer in
Python.🤷♂️</h4>
<blockquote name="db2e" id="db2e" class="graf graf--blockquote graf-after--h4">If you came from a
background in JavaScript and learned to accept the premise(s) of the following meme…</blockquote>
<figure name="ef66" id="ef66" class="graf graf--figure graf-after--blockquote"><img class="graf-image"
data-image-id="0*eC4EvZcv6hhH88jX.png" data-width="639" data-height="724"
src="https://cdn-images-1.medium.com/max/800/0*eC4EvZcv6hhH88jX.png"></figure>
<blockquote name="a118" id="a118" class="graf graf--blockquote graf-after--figure">Than I am sure you will
find the means to suspend your disbelief.</blockquote>
<pre name="ba74" id="ba74"
class="graf graf--pre graf-after--blockquote">print(2.24) # => 2.24 <br>print(2.) # => 2.0 <br>print(float()) # => 0.0 <br>print(27e-5) # => 0.00027</pre>
<h3 name="2c13" id="2c13" class="graf graf--h3 graf-after--pre">KEEP IN MIND:</h3>
<blockquote name="96b8" id="96b8" class="graf graf--blockquote graf-after--h3"><strong
class="markup--strong markup--blockquote-strong">The </strong><code
class="markup--code markup--blockquote-code"><strong
class="markup--strong markup--blockquote-strong">i</strong></code><strong
class="markup--strong markup--blockquote-strong"> is switched to a </strong><code
class="markup--code markup--blockquote-code"><strong
class="markup--strong markup--blockquote-strong">j</strong></code><strong
class="markup--strong markup--blockquote-strong"> in programming.</strong></blockquote>
<p name="359c" id="359c"
class="graf graf--p graf--hasDropCapModel graf--hasDropCap graf-after--blockquote"><span
class="graf-dropCap">T</span><em class="markup--em markup--p-em">his is because the letter i is common
place as the de facto index for any and all enumerable entities so it just makes sense not to compete
for name-</em><strong class="markup--strong markup--p-strong"><em
class="markup--em markup--p-em">space </em></strong><em class="markup--em markup--p-em">when there’s
another 25 letters that don’t get used for every loop under the sun. My most medium apologies to
Leonhard Euler.</em></p>
<pre name="3750" id="3750"
class="graf graf--pre graf-after--p">print(7j) # => 7j <br>print(5.1+7.7j)) # => 5.1+7.7j <br>print(complex(3, 5)) # => 3+5j <br>print(complex(17)) # => 17+0j <br>print(complex()) # => 0j</pre>
<ul class="postList">
<li name="2579" id="2579" class="graf graf--li graf-after--pre"><strong
class="markup--strong markup--li-strong">Type Casting</strong> : The process of converting one
number to another.</li>
</ul>
<pre name="7273" id="7273"
class="graf graf--pre graf-after--li"># Using Float<br>print(17) # => 17<br>print(float(17)) # => 17.0</pre>
<pre name="7779" id="7779"
class="graf graf--pre graf-after--pre"># Using Int<br>print(17.0) # => 17.0<br>print(int(17.0)) # => 17</pre>
<pre name="67f3" id="67f3"
class="graf graf--pre graf-after--pre"># Using Str<br>print(str(17.0) + ' and ' + str(17)) # => 17.0 and 17</pre>
<p name="94a0" id="94a0" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">The arithmetic operators are the same between JS and Python,
with two additions:</strong></p>
<ul class="postList">
<li name="8cf4" id="8cf4" class="graf graf--li graf--startsWithDoubleQuote graf-after--p"><em
class="markup--em markup--li-em">“**” : Double asterisk for exponent.</em></li>
<li name="03b4" id="03b4" class="graf graf--li graf--startsWithDoubleQuote graf-after--li"><em
class="markup--em markup--li-em">“//” : Integer Division.</em></li>
<li name="2ce5" id="2ce5" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">There are no spaces between math operations in
Python.</strong></li>
<li name="1686" id="1686" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">Integer Division gives the other part of the number from
Module; it is a way to do round down numbers replacing </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">Math.floor()</strong></code><strong
class="markup--strong markup--li-strong"> in JS.</strong></li>
<li name="a6a3" id="a6a3" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">There are no </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">++</strong></code><strong
class="markup--strong markup--li-strong"> and </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">--</strong></code><strong
class="markup--strong markup--li-strong"> in Python, the only shorthand operators are:</strong></li>
</ul>
<figure name="00d8" id="00d8"
class="graf graf--figure graf--layoutOutsetLeft graf-after--li graf--trailing"><img class="graf-image"
data-image-id="0*Ez_1PZ93N4FfvkRr.png" data-width="600" data-height="606"
src="https://cdn-images-1.medium.com/max/600/0*Ez_1PZ93N4FfvkRr.png"></figure>
</div>
</div>
</section>
<section name="b4ba" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="f25c" id="f25c" class="graf graf--h3 graf--leading">Strings</h3>
<ul class="postList">
<li name="e98c" id="e98c" class="graf graf--li graf-after--h3">Python uses both single and double
quotes.</li>
<li name="225e" id="225e" class="graf graf--li graf-after--li">You can escape strings like so <code
class="markup--code markup--li-code">'Jodi asked, "What\'s up, Sam?"'</code>
</li>
<li name="9f74" id="9f74" class="graf graf--li graf-after--li">Multiline strings use triple quotes.</li>
</ul>
<pre name="669b" id="669b"
class="graf graf--pre graf-after--li">print('''My instructions are very long so to make them<br>more readable in the code I am putting them on<br>more than one line. I can even include "quotes"<br>of any kind because they won't get confused with<br>the end of the string!''')</pre>
<p name="1e40" id="1e40" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">Use the </strong><code
class="markup--code markup--p-code"><strong
class="markup--strong markup--p-strong">len()</strong></code><strong
class="markup--strong markup--p-strong"> function to get the length of a string.</strong></p>
<pre name="425a" id="425a" class="graf graf--pre graf-after--p">print(len(“Spaghetti”)) # => 9</pre>
<h3 name="3f9c" id="3f9c" class="graf graf--h3 graf-after--pre"><strong
class="markup--strong markup--h3-strong">Python uses </strong><code
class="markup--code markup--h3-code"><strong class="markup--strong markup--h3-strong">zero-based
indexing</strong></code></h3>
<h4 name="55b9" id="55b9" class="graf graf--h4 graf-after--h3">Python allows negative indexing
(thank god!)</h4>
<pre name="7d56" id="7d56" class="graf graf--pre graf-after--h4">print(“Spaghetti”[-1]) # => i </pre>
<pre name="f11f" id="f11f" class="graf graf--pre graf-after--pre">print(“Spaghetti”[-4]) # => e</pre>
<ul class="postList">
<li name="7567" id="7567" class="graf graf--li graf-after--pre">Python let’s you use ranges</li>
</ul>
<p name="440b" id="440b" class="graf graf--p graf-after--li">You can think of this as roughly equivalent
to the slice method called on a JavaScript object or string… <em class="markup--em markup--p-em">(mind
you that in JS … strings are wrapped in an object (under the hood)… upon which the string methods are
actually called. As a immutable privative type </em><strong
class="markup--strong markup--p-strong"><em class="markup--em markup--p-em">by textbook
definition</em></strong><em class="markup--em markup--p-em">, a string literal could not hope to
invoke most of it’s methods without violating the state it was bound to on initialization if it were
not for this bit of syntactic sugar.)</em></p>
<pre name="f1fb" id="f1fb"
class="graf graf--pre graf-after--p">print(“Spaghetti”[1:4]) # => pag <br>print(“Spaghetti”[4:-1]) # => hett <br>print(“Spaghetti”[4:4]) # => (empty string)</pre>
<ul class="postList">
<li name="1366" id="1366" class="graf graf--li graf-after--pre">The end range is exclusive just like
<code class="markup--code markup--li-code">slice</code> in JS.</li>
</ul>
<pre name="5f2c" id="5f2c"
class="graf graf--pre graf-after--li"># Shortcut to get from the beginning of a string to a certain index.<br>print("Spaghetti"[:4]) # => Spag<br>print("Spaghetti"[:-1]) # => Spaghett</pre>
<pre name="b0df" id="b0df"
class="graf graf--pre graf-after--pre"># Shortcut to get from a certain index to the end of a string.<br>print("Spaghetti"[1:]) # => paghetti<br>print("Spaghetti"[-4:]) # => etti</pre>
<ul class="postList">
<li name="c786" id="c786" class="graf graf--li graf-after--pre">The <code
class="markup--code markup--li-code">index</code> string function is the equiv. of <code
class="markup--code markup--li-code">indexOf()</code> in JS</li>
</ul>
<pre name="2a43" id="2a43"
class="graf graf--pre graf-after--li">print("Spaghetti".index("h")) # => 4<br>print("Spaghetti".index("t")) # => 6</pre>
<ul class="postList">
<li name="fbb6" id="fbb6" class="graf graf--li graf-after--pre">The <code
class="markup--code markup--li-code">count</code> function finds out how many times a substring
appears in a string… pretty nifty for a hard coded feature of the language.</li>
</ul>
<pre name="6cb2" id="6cb2"
class="graf graf--pre graf-after--li">print("Spaghetti".count("h")) # => 1<br>print("Spaghetti".count("t")) # => 2<br>print("Spaghetti".count("s")) # => 0<br>print('''We choose to go to the moon in this decade and do the other things,<br>not because they are easy, but because they are hard, because that goal will<br>serve to organize and measure the best of our energies and skills, because that<br>challenge is one that we are willing to accept, one we are unwilling to<br>postpone, and one which we intend to win, and the others, too.<br>'''.count('the ')) # => 4</pre>
<ul class="postList">
<li name="7816" id="7816" class="graf graf--li graf-after--pre"><strong
class="markup--strong markup--li-strong">You can use </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">+</strong></code><strong
class="markup--strong markup--li-strong"> to concatenate strings, just like in JS.</strong></li>
<li name="ed0a" id="ed0a" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">You can also use “*” to repeat strings or multiply
strings.</strong></li>
<li name="f95c" id="f95c" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">Use the </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">format()</strong></code><strong
class="markup--strong markup--li-strong"> function to use placeholders in a string to input values
later on.</strong></li>
</ul>
<pre name="dd9a" id="dd9a"
class="graf graf--pre graf-after--li">first_name = "Billy"<br>last_name = "Bob"<br>print('Your name is {0} {1}'.format(first_name, last_name)) # => Your name is Billy Bob</pre>
<ul class="postList">
<li name="445b" id="445b" class="graf graf--li graf-after--pre"><em
class="markup--em markup--li-em">Shorthand way to use format function is:<br></em><code
class="markup--code markup--li-code">print(f'Your name is {first_name} {last_name}')</code>
</li>
</ul>
<h4 name="8ff4" id="8ff4" class="graf graf--h4 graf-after--li">Some useful string methods.</h4>
<ul class="postList">
<li name="118c" id="118c" class="graf graf--li graf-after--h4"><strong
class="markup--strong markup--li-strong">Note that in JS </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">join</strong></code><strong
class="markup--strong markup--li-strong"> is used on an Array, in Python it is used on
String.</strong></li>
</ul>
<figure name="9d1f" id="9d1f" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*eE3E5H0AoqkhqK1z.png" data-width="1628" data-height="824"
src="https://cdn-images-1.medium.com/max/800/0*eE3E5H0AoqkhqK1z.png"></figure>
<ul class="postList">
<li name="e95e" id="e95e" class="graf graf--li graf-after--figure">There are also many handy testing
methods.</li>
</ul>
<figure name="26b6" id="26b6" class="graf graf--figure graf-after--li graf--trailing"><img
class="graf-image" data-image-id="0*Q0CMqFd4PozLDFPB.png" data-width="1618" data-height="522"
src="https://cdn-images-1.medium.com/max/800/0*Q0CMqFd4PozLDFPB.png"></figure>
</div>
</div>
</section>
<section name="477c" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="920e" id="920e" class="graf graf--h3 graf--leading">Variables and Expressions</h3>
<ul class="postList">
<li name="a255" id="a255" class="graf graf--li graf-after--h3"><strong
class="markup--strong markup--li-strong">Duck-Typing</strong> : Programming Style which avoids
checking an object’s type to figure out what it can do.</li>
<li name="6e70" id="6e70" class="graf graf--li graf-after--li">Duck Typing is the fundamental approach
of Python.</li>
<li name="5666" id="5666" class="graf graf--li graf-after--li">Assignment of a value automatically
declares a variable.</li>
</ul>
<pre name="302d" id="302d"
class="graf graf--pre graf-after--li">a = 7<br>b = 'Marbles'<br>print(a) # => 7<br>print(b) # => Marbles</pre>
<ul class="postList">
<li name="f6cf" id="f6cf" class="graf graf--li graf-after--pre"><strong
class="markup--strong markup--li-strong"><em class="markup--em markup--li-em">You can chain variable
assignments to give multiple var names the same value.</em></strong></li>
</ul>
<h4 name="ab40" id="ab40" class="graf graf--h4 graf-after--li">Use with caution as this is highly
unreadable</h4>
<pre name="e46a" id="e46a"
class="graf graf--pre graf-after--h4">count = max = min = 0<br>print(count) # => 0<br>print(max) # => 0<br>print(min) # => 0</pre>
<h4 name="c91a" id="c91a" class="graf graf--h4 graf-after--pre">The value and type of a variable can be
re-assigned at any time.</h4>
<pre name="9b2f" id="9b2f"
class="graf graf--pre graf-after--h4">a = 17<br>print(a) # => 17<br>a = 'seventeen'<br>print(a) # => seventeen</pre>
<ul class="postList">
<li name="4605" id="4605" class="graf graf--li graf-after--pre"><code
class="markup--code markup--li-code"><em class="markup--em markup--li-em">NaN</em></code><em
class="markup--em markup--li-em"> does not exist in Python, but you can 'create' it like
so:<br></em><code class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong"><em
class="markup--em markup--li-em">print(float("nan"))</em></strong></code></li>
<li name="d150" id="d150" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Python replaces </em><code class="markup--code markup--li-code"><em
class="markup--em markup--li-em">null</em></code><em class="markup--em markup--li-em"> with
</em><code class="markup--code markup--li-code"><em
class="markup--em markup--li-em">none</em></code><em class="markup--em markup--li-em">.</em></li>
<li name="6fa7" id="6fa7" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code"><strong class="markup--strong markup--li-strong"><em
class="markup--em markup--li-em">none</em></strong></code><strong
class="markup--strong markup--li-strong"><em class="markup--em markup--li-em"> is an
object</em></strong><em class="markup--em markup--li-em"> and can be directly assigned to a
variable.</em></li>
</ul>
<blockquote name="ba42" id="ba42" class="graf graf--blockquote graf-after--li graf--trailing">Using none
is a convenient way to check to see why an action may not be operating correctly in your program.
</blockquote>
</div>
</div>
</section>
<section name="1a73" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="6f4e" id="6f4e" class="graf graf--h3 graf--leading">Boolean Data Type</h3>
<ul class="postList">
<li name="b843" id="b843" class="graf graf--li graf-after--h3">One of the biggest benefits of Python is
that it reads more like English than JS does.</li>
</ul>
<figure name="46a3" id="46a3" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*HQpndNhm1Z_xSoHb.png" data-width="1608" data-height="344"
src="https://cdn-images-1.medium.com/max/800/0*HQpndNhm1Z_xSoHb.png"></figure>
<pre name="5ba1" id="5ba1"
class="graf graf--pre graf-after--figure"># Logical AND<br>print(True and True) # => True<br>print(True and False) # => False<br>print(False and False) # => False</pre>
<pre name="46fe" id="46fe"
class="graf graf--pre graf-after--pre"># Logical OR<br>print(True or True) # => True<br>print(True or False) # => True<br>print(False or False) # => False</pre>
<pre name="4857" id="4857"
class="graf graf--pre graf-after--pre"># Logical NOT<br>print(not True) # => False<br>print(not False and True) # => True<br>print(not True or False) # => False</pre>
<ul class="postList">
<li name="18cc" id="18cc" class="graf graf--li graf-after--pre">By default, Python considers an object
to be true UNLESS it is one of the following:</li>
<li name="6e0a" id="6e0a" class="graf graf--li graf-after--li">Constant <code
class="markup--code markup--li-code">None</code> or <code
class="markup--code markup--li-code">False</code></li>
<li name="9552" id="9552" class="graf graf--li graf-after--li">Zero of any numeric type.</li>
<li name="e7ce" id="e7ce" class="graf graf--li graf-after--li">Empty Sequence or Collection.</li>
<li name="11d6" id="11d6" class="graf graf--li graf-after--li graf--trailing"><code
class="markup--code markup--li-code">True</code> and <code
class="markup--code markup--li-code">False</code> must be capitalized</li>
</ul>
</div>
</div>
</section>
<section name="61e9" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="586f" id="586f" class="graf graf--h3 graf--leading">Comparison Operators</h3>
<ul class="postList">
<li name="a4fa" id="a4fa" class="graf graf--li graf-after--h3">Python uses all the same equality
operators as JS.</li>
<li name="7f98" id="7f98" class="graf graf--li graf-after--li">In Python, equality operators are
processed from left to right.</li>
<li name="fb68" id="fb68" class="graf graf--li graf-after--li">Logical operators are processed in this
order:</li>
</ul>
<ol class="postList">
<li name="bf08" id="bf08" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">NOT</strong></li>
<li name="4888" id="4888" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">AND</strong></li>
<li name="2c55" id="2c55" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">OR</strong></li>
</ol>
<blockquote name="0e5e" id="0e5e" class="graf graf--blockquote graf-after--li">Just like in JS, you can
use <code class="markup--code markup--blockquote-code">parentheses</code> to change the inherent order
of operations.</blockquote>
<blockquote name="4f81" id="4f81" class="graf graf--blockquote graf-after--blockquote"><strong
class="markup--strong markup--blockquote-strong">Short Circuit</strong> : Stopping a program when a
<code class="markup--code markup--blockquote-code">true</code> or <code
class="markup--code markup--blockquote-code">false</code> has been reached.</blockquote>
<figure name="f83b" id="f83b" class="graf graf--figure graf-after--blockquote graf--trailing"><img
class="graf-image" data-image-id="0*qHzGRLTOMTf30miT.png" data-width="1594" data-height="404"
src="https://cdn-images-1.medium.com/max/800/0*qHzGRLTOMTf30miT.png"></figure>
</div>
</div>
</section>
<section name="ac4f" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="4ed8" id="4ed8" class="graf graf--h3 graf--leading">Identity vs Equality</h3>
<pre name="c7d8" id="c7d8"
class="graf graf--pre graf-after--h3">print (2 == '2') # => False<br>print (2 is '2') # => False</pre>
<pre name="2ab6" id="2ab6"
class="graf graf--pre graf-after--pre">print ("2" == '2') # => True<br>print ("2" is '2') # => True</pre>
<pre name="8ce9" id="8ce9"
class="graf graf--pre graf-after--pre"># There is a distinction between the number types.<br>print (2 == 2.0) # => True<br>print (2 is 2.0) # => False</pre>
<ul class="postList">
<li name="c5a5" id="c5a5" class="graf graf--li graf-after--pre graf--trailing">In the Python community
it is better to use <code class="markup--code markup--li-code">is</code> and <code
class="markup--code markup--li-code">is not</code> over <code
class="markup--code markup--li-code">==</code> or <code
class="markup--code markup--li-code">!=</code></li>
</ul>
</div>
</div>
</section>
<section name="fa32" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="a959" id="a959" class="graf graf--h3 graf--leading">If Statements</h3>
<pre name="5558" id="5558"
class="graf graf--pre graf-after--h3">if name == 'Monica':<br> print('Hi, Monica.')</pre>
<pre name="7e04" id="7e04"
class="graf graf--pre graf-after--pre">if name == 'Monica':<br> print('Hi, Monica.')<br>else:<br> print('Hello, stranger.')</pre>
<pre name="4d41" id="4d41"
class="graf graf--pre graf-after--pre">if name == 'Monica':<br> print('Hi, Monica.')<br>elif age < 12:<br> print('You are not Monica, kiddo.')<br>elif age > 2000:<br> print('Unlike you, Monica is not an undead, immortal vampire.')<br>elif age > 100:<br> print('You are not Monica, grannie.')</pre>
<blockquote name="3c0d" id="3c0d" class="graf graf--blockquote graf-after--pre graf--trailing">Remember
the order of <code class="markup--code markup--blockquote-code">elif</code> statements matter.
</blockquote>
</div>
</div>
</section>
<section name="eea8" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="bc26" id="bc26" class="graf graf--h3 graf--leading">While Statements</h3>
<pre name="280d" id="280d"
class="graf graf--pre graf-after--h3">spam = 0<br>while spam < 5:<br> print('Hello, world.')<br> spam = spam + 1</pre>
<ul class="postList">
<li name="c7f3" id="c7f3" class="graf graf--li graf-after--pre"><code
class="markup--code markup--li-code">Break</code> statement also exists in Python.</li>
</ul>
<pre name="1ef2" id="1ef2"
class="graf graf--pre graf-after--li">spam = 0<br>while True:<br> print('Hello, world.')<br> spam = spam + 1<br> if spam >= 5:<br> break</pre>
<ul class="postList">
<li name="7a99" id="7a99" class="graf graf--li graf-after--pre">As are <code
class="markup--code markup--li-code">continue</code> statements</li>
</ul>
<pre name="5a23" id="5a23"
class="graf graf--pre graf-after--li graf--trailing">spam = 0<br>while True:<br> print('Hello, world.')<br> spam = spam + 1<br> if spam < 5:<br> continue<br> break</pre>
</div>
</div>
</section>
<section name="d2d3" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="fb57" id="fb57" class="graf graf--h3 graf--leading">Try/Except Statements</h3>
<ul class="postList">
<li name="72ec" id="72ec" class="graf graf--li graf-after--h3">Python equivalent to <code
class="markup--code markup--li-code">try/catch</code></li>
</ul>
<pre name="ed26" id="ed26"
class="graf graf--pre graf-after--li">a = 321<br>try:<br> print(len(a))<br>except:<br> print('Silently handle error here')</pre>
<pre name="62f6" id="62f6"
class="graf graf--pre graf-after--pre"> # Optionally include a correction to the issue<br> a = str(a)<br> print(len(a)</pre>
<pre name="b98d" id="b98d"
class="graf graf--pre graf-after--pre">a = '321'<br>try:<br> print(len(a))<br>except:<br> print('Silently handle error here')</pre>
<pre name="080f" id="080f"
class="graf graf--pre graf-after--pre"> # Optionally include a correction to the issue<br> a = str(a)<br> print(len(a))</pre>
<ul class="postList">
<li name="dcd1" id="dcd1" class="graf graf--li graf-after--pre">You can name an error to give the output
more specificity.</li>
</ul>
<pre name="d527" id="d527"
class="graf graf--pre graf-after--li">a = 100<br>b = 0<br>try:<br> c = a / b<br>except ZeroDivisionError:<br> c = None<br>print(c)</pre>
<ul class="postList">
<li name="4027" id="4027" class="graf graf--li graf-after--pre">You can also use the <code
class="markup--code markup--li-code">pass</code> commmand to by pass a certain error.</li>
</ul>
<pre name="30f0" id="30f0"
class="graf graf--pre graf-after--li">a = 100<br>b = 0<br>try:<br> print(a / b)<br>except ZeroDivisionError:<br> pass</pre>
<ul class="postList">
<li name="030b" id="030b" class="graf graf--li graf-after--pre">The <code
class="markup--code markup--li-code">pass</code> method won't allow you to bypass every single
error so you can chain an exception series like so:</li>
</ul>
<pre name="b0c7" id="b0c7"
class="graf graf--pre graf-after--li">a = 100<br># b = "5"<br>try:<br> print(a / b)<br>except ZeroDivisionError:<br> pass<br>except (TypeError, NameError):<br> print("ERROR!")</pre>
<ul class="postList">
<li name="bf45" id="bf45" class="graf graf--li graf-after--pre">You can use an <code
class="markup--code markup--li-code">else</code> statement to end a chain of <code
class="markup--code markup--li-code">except</code> statements.</li>
</ul>
<pre name="bc50" id="bc50"
class="graf graf--pre graf-after--li"># tuple of file names<br>files = ('one.txt', 'two.txt', 'three.txt')</pre>
<pre name="ca0d" id="ca0d"
class="graf graf--pre graf-after--pre"># simple loop<br>for filename in files:<br> try:<br> # open the file in read mode<br> f = open(filename, 'r')<br> except OSError:<br> # handle the case where file does not exist or permission is denied<br> print('cannot open file', filename)<br> else:<br> # do stuff with the file object (f)<br> print(filename, 'opened successfully')<br> print('found', len(f.readlines()), 'lines')<br> f.close()</pre>
<ul class="postList">
<li name="0e91" id="0e91" class="graf graf--li graf-after--pre"><code
class="markup--code markup--li-code">finally</code> is used at the end to clean up all actions under
any circumstance.</li>
</ul>
<pre name="0162" id="0162"
class="graf graf--pre graf-after--li">def divide(x, y):<br> try:<br> result = x / y<br> except ZeroDivisionError:<br> print("Cannot divide by zero")<br> else:<br> print("Result is", result)<br> finally:<br> print("Finally...")</pre>
<ul class="postList">
<li name="84ee" id="84ee" class="graf graf--li graf-after--pre">Using duck typing to check to see if
some value is able to use a certain method.</li>
</ul>
<pre name="133c" id="133c"
class="graf graf--pre graf-after--li"># Try a number - nothing will print out<br>a = 321<br>if hasattr(a, '__len__'):<br> print(len(a))</pre>
<pre name="5dc9" id="5dc9"
class="graf graf--pre graf-after--pre graf--trailing"># Try a string - the length will print out (4 in this case)<br>b = "5555"<br>if hasattr(b, '__len__'):<br> print(len(b))</pre>
</div>
</div>
</section>
<section name="b06a" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="e485" id="e485" class="graf graf--h3 graf--leading">Pass</h3>
<ul class="postList">
<li name="2b80" id="2b80" class="graf graf--li graf-after--h3">Pass Keyword is required to write the JS
equivalent of :</li>
</ul>
<pre name="650c" id="650c" class="graf graf--pre graf-after--li">if (true) {<br>}</pre>
<pre name="add9" id="add9" class="graf graf--pre graf-after--pre">while (true) {}</pre>
<pre name="72c8" id="72c8" class="graf graf--pre graf-after--pre">if True:<br> pass</pre>
<pre name="2576" id="2576"
class="graf graf--pre graf-after--pre graf--trailing">while True:<br> pass</pre>
</div>
</div>
</section>
<section name="fd82" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="2623" id="2623" class="graf graf--h3 graf--leading">Functions</h3>
<ul class="postList">
<li name="7091" id="7091" class="graf graf--li graf-after--h3"><strong
class="markup--strong markup--li-strong">Function definition includes:</strong></li>
<li name="1f11" id="1f11" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">The </strong><code
class="markup--code markup--li-code"><strong
class="markup--strong markup--li-strong">def</strong></code><strong
class="markup--strong markup--li-strong"> keyword</strong></li>
<li name="ec14" id="ec14" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">The name of the function</strong></li>
<li name="7733" id="7733" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">A list of parameters enclosed in parentheses.</strong></li>
<li name="1516" id="1516" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">A colon at the end of the line.</strong></li>
<li name="b2dd" id="b2dd" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">One tab indentation for the code to run.</strong></li>
<li name="bcef" id="bcef" class="graf graf--li graf-after--li"><strong
class="markup--strong markup--li-strong">You can use default parameters just like in JS</strong>
</li>
</ul>
<pre name="10df" id="10df"
class="graf graf--pre graf-after--li">def greeting(name, saying="Hello"):<br> print(saying, name)</pre>
<pre name="1e17" id="1e17"
class="graf graf--pre graf-after--pre">greeting("Monica")<br># Hello Monica</pre>
<pre name="3702" id="3702"
class="graf graf--pre graf-after--pre">greeting("Barry", "Hey")<br># Hey Barry</pre>
<h4 name="f3f1" id="f3f1" class="graf graf--h4 graf-after--pre"><strong
class="markup--strong markup--h4-strong">Keep in mind, default parameters must always come after
regular parameters.</strong></h4>
<pre name="9657" id="9657"
class="graf graf--pre graf-after--h4"># THIS IS BAD CODE AND WILL NOT RUN<br>def increment(delta=1, value):<br> return delta + value</pre>
<ul class="postList">
<li name="c1aa" id="c1aa" class="graf graf--li graf-after--pre"><em class="markup--em markup--li-em">You
can specify arguments by name without destructuring in Python.</em></li>
</ul>
<pre name="e495" id="e495"
class="graf graf--pre graf-after--li">def greeting(name, saying="Hello"):<br> print(saying, name)</pre>
<pre name="e924" id="e924"
class="graf graf--pre graf-after--pre"># name has no default value, so just provide the value<br># saying has a default value, so use a keyword argument<br>greeting("Monica", saying="Hi")</pre>
<ul class="postList">
<li name="54ac" id="54ac" class="graf graf--li graf-after--pre">The <code
class="markup--code markup--li-code">lambda</code> keyword is used to create anonymous functions and
are supposed to be <code class="markup--code markup--li-code">one-liners</code>.</li>
</ul>
<p name="bf63" id="bf63" class="graf graf--p graf-after--li graf--trailing"><code
class="markup--code markup--p-code">toUpper = lambda s: s.upper()</code></p>
</div>
</div>
</section>
<section name="db19" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="4564" id="4564" class="graf graf--h3 graf--leading">Notes</h3>
<h4 name="b43f" id="b43f" class="graf graf--h4 graf-after--h3">Formatted Strings</h4>
<blockquote name="9f21" id="9f21" class="graf graf--blockquote graf-after--h4">Remember that in Python
join() is called on a string with an array/list passed in as the argument.<br>Python has a very powerful
formatting engine.<br>format() is also applied directly to strings.</blockquote>
<pre name="c199" id="c199"
class="graf graf--pre graf-after--blockquote">shopping_list = [‘bread’,’milk’,’eggs’]<br>print(‘,’.join(shopping_list))</pre>
<h3 name="5fda" id="5fda" class="graf graf--h3 graf-after--pre">Comma Thousands Separator</h3>
<pre name="5939" id="5939"
class="graf graf--pre graf-after--h3">print(‘{:,}’.format(1234567890))<br>‘1,234,567,890’</pre>
<h3 name="d948" id="d948" class="graf graf--h3 graf-after--pre">Date and Time</h3>
<pre name="94fd" id="94fd"
class="graf graf--pre graf-after--h3">d = datetime.datetime(2020, 7, 4, 12, 15, 58)<br>print(‘{:%Y-%m-%d %H:%M:%S}’.format(d))<br>‘2020–07–04 12:15:58’</pre>
<h3 name="37d5" id="37d5" class="graf graf--h3 graf-after--pre">Percentage</h3>
<pre name="4b6c" id="4b6c"
class="graf graf--pre graf-after--h3">points = 190<br>total = 220<br>print(‘Correct answers: {:.2%}’.format(points/total))<br>Correct answers: 86.36%</pre>
<h3 name="92ea" id="92ea" class="graf graf--h3 graf-after--pre">Data Tables</h3>
<pre name="198e" id="198e"
class="graf graf--pre graf-after--h3">width=8<br>print(‘ decimal hex binary’)<br>print(‘-’*27)<br>for num in range(1,16):<br>for base in ‘dXb’:<br>print(‘{0:{width}{base}}’.format(num, base=base, width=width), end=’ ‘)<br>print()<br>Getting Input from the Command Line<br>Python runs synchronously, all programs and processes will stop when listening for a user input.<br>The input function shows a prompt to a user and waits for them to type ‘ENTER’.<br>Scripts vs Programs<br>Programming Script : A set of code that runs in a linear fashion.<br>The largest difference between scripts and programs is the level of complexity and purpose. Programs typically have many UI’s.</pre>
<p name="4256" id="4256" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">Python can be used to display html, css, and
JS.</strong><br><em class="markup--em markup--p-em">It is common to use Python as an API (Application
Programming Interface)</em></p>
<h4 name="71b4" id="71b4" class="graf graf--h4 graf-after--p">Structured Data</h4>
<h4 name="d9a2" id="d9a2" class="graf graf--h4 graf-after--h4">Sequence : The most basic data structure in
Python where the index determines the order.</h4>
<blockquote name="55e7" id="55e7" class="graf graf--blockquote graf-after--h4 graf--trailing">
List<br>Tuple<br>Range<br>Collections : Unordered data structures, hashable values.</blockquote>
</div>
</div>
</section>
<section name="d4ae" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h4 name="56c2" id="56c2" class="graf graf--h4 graf--leading">Dictionaries<br>Sets</h4>
<h4 name="b683" id="b683" class="graf graf--h4 graf-after--h4">Iterable : Generic name for a sequence or
collection; any object that can be iterated through.</h4>
<h4 name="8011" id="8011" class="graf graf--h4 graf-after--h4 graf--trailing">Can be mutable or
immutable.<br>Built In Data Types</h4>
</div>
</div>
</section>
<section name="dbde" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="71e2" id="71e2" class="graf graf--h3 graf--leading">Lists are the python equivalent of arrays.
</h3>
<pre name="7f66" id="7f66"
class="graf graf--pre graf-after--h3">empty_list = []<br>departments = [‘HR’,’Development’,’Sales’,’Finance’,’IT’,’Customer Support’]</pre>
<h3 name="97e6" id="97e6" class="graf graf--h3 graf-after--pre">You can instantiate</h3>
<pre name="5302" id="5302" class="graf graf--pre graf-after--h3">specials = list()</pre>
<h4 name="e60f" id="e60f" class="graf graf--h4 graf-after--pre">Test if a value is in a list.</h4>
<pre name="37ea" id="37ea"
class="graf graf--pre graf-after--h4">print(1 in [1, 2, 3]) #> True<br>print(4 in [1, 2, 3]) #> False<br># Tuples : Very similar to lists, but they are immutable</pre>
<h4 name="6c7d" id="6c7d" class="graf graf--h4 graf-after--pre">Instantiated with parentheses</h4>
<pre name="9549" id="9549" class="graf graf--pre graf-after--h4">time_blocks = (‘AM’,’PM’)</pre>
<h4 name="60f5" id="60f5" class="graf graf--h4 graf-after--pre">Sometimes instantiated without</h4>
<pre name="f1f1" id="f1f1"
class="graf graf--pre graf-after--h4">colors = ‘red’,’blue’,’green’<br>numbers = 1, 2, 3</pre>
<h4 name="5ffb" id="5ffb" class="graf graf--h4 graf-after--pre">Tuple() built in can be used to convert
other data into a tuple</h4>
<pre name="9cb9" id="9cb9"
class="graf graf--pre graf-after--h4">tuple(‘abc’) # returns (‘a’, ‘b’, ‘c’)<br>tuple([1,2,3]) # returns (1, 2, 3)<br># Think of tuples as constant variables.</pre>
<h4 name="9fca" id="9fca" class="graf graf--h4 graf-after--pre">Ranges : A list of numbers which can’t be
changed; often used with for loops.</h4>
<p name="e7c9" id="e7c9" class="graf graf--p graf-after--h4"><strong
class="markup--strong markup--p-strong">Declared using one to three parameters</strong>.</p>
<blockquote name="6e20" id="6e20" class="graf graf--blockquote graf-after--p">Start : opt. default 0,
first # in sequence.<br>Stop : required next number past the last number in the sequence.<br>Step : opt.
default 1, difference between each number in the sequence.</blockquote>
<pre name="19f5" id="19f5"
class="graf graf--pre graf-after--blockquote">range(5) # [0, 1, 2, 3, 4]<br>range(1,5) # [1, 2, 3, 4]<br>range(0, 25, 5) # [0, 5, 10, 15, 20]<br>range(0) # [ ]<br>for let (i = 0; i < 5; i++)<br>for let (i = 1; i < 5; i++)<br>for let (i = 0; i < 25; i+=5)<br>for let(i = 0; i = 0; i++)<br># Keep in mind that stop is not included in the range.</pre>
<h4 name="926c" id="926c" class="graf graf--h4 graf-after--pre">Dictionaries : Mappable collection where a
hashable value is used as a key to ref. an object stored in the dictionary.</h4>
<h4 name="364d" id="364d" class="graf graf--h4 graf-after--h4">Mutable.</h4>
<pre name="43ed" id="43ed"
class="graf graf--pre graf-after--h4">a = {‘one’:1, ‘two’:2, ‘three’:3}<br>b = dict(one=1, two=2, three=3)<br>c = dict([(‘two’, 2), (‘one’, 1), (‘three’, 3)])<br># a, b, and c are all equal</pre>
<p name="8244" id="8244" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong"><em class="markup--em markup--p-em">Declared with curly braces
of the built in dict()</em></strong></p>
<blockquote name="8a61" id="8a61" class="graf graf--blockquote graf-after--p"><em
class="markup--em markup--blockquote-em">Benefit of dictionaries in Python is that it doesn’t matter
how it is defined, if the keys and values are the same the dictionaries are considered equal.</em>
</blockquote>
<p name="f880" id="f880" class="graf graf--p graf-after--blockquote"><strong
class="markup--strong markup--p-strong">Use the in operator to see if a key exists in a
dictionary.</strong></p>
<p name="6ec7" id="6ec7" class="graf graf--p graf--hasDropCapModel graf--hasDropCap graf-after--p"><span
class="graf-dropCap">S</span><strong class="markup--strong markup--p-strong">ets : Unordered
collection of distinct objects; objects that need to be hashable.</strong></p>
<blockquote name="c664" id="c664" class="graf graf--blockquote graf-after--p"><em
class="markup--em markup--blockquote-em">Always be unique, duplicate items are auto dropped from the
set.</em></blockquote>
<h4 name="e4df" id="e4df" class="graf graf--h4 graf-after--blockquote">Common Uses:</h4>
<blockquote name="0d8b" id="0d8b" class="graf graf--blockquote graf-after--h4">Removing
Duplicates<br>Membership Testing<br>Mathematical Operators: Intersection, Union, Difference, Symmetric
Difference.</blockquote>
<p name="598e" id="598e" class="graf graf--p graf-after--blockquote"><strong
class="markup--strong markup--p-strong">Standard Set is mutable, Python has a immutable version called
frozenset.<br>Sets created by putting comma seperated values inside braces:</strong></p>
<pre name="d97e" id="d97e"
class="graf graf--pre graf-after--p">school_bag = {‘book’,’paper’,’pencil’,’pencil’,’book’,’book’,’book’,’eraser’}<br>print(school_bag)</pre>
<h4 name="08b8" id="08b8" class="graf graf--h4 graf-after--pre">Also can use set constructor to
automatically put it into a set.</h4>
<pre name="6ad6" id="6ad6"
class="graf graf--pre graf-after--h4">letters = set(‘abracadabra’)<br>print(letters)<br>#Built-In Functions<br>#Functions using iterables</pre>
<p name="6250" id="6250" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">filter(function, iterable) : creates new iterable of the same
type which includes each item for which the function returns true.</strong></p>
<p name="d9f6" id="d9f6" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">map(function, iterable) : creates new iterable of the same
type which includes the result of calling the function on every item of the iterable.</strong></p>
<p name="a09a" id="a09a" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">sorted(iterable, key=None, reverse=False) : creates a new
sorted list from the items in the iterable.</strong></p>
<p name="e285" id="e285" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">Output is always a list</strong></p>
<p name="2229" id="2229" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">key: opt function which coverts and item to a value to be
compared.</strong></p>
<p name="2975" id="2975" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">reverse: optional boolean.</strong></p>
<p name="6933" id="6933" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">enumerate(iterable, start=0) : starts with a sequence and
converts it to a series of tuples</strong></p>
<pre name="64d4" id="64d4"
class="graf graf--pre graf-after--p">quarters = [‘First’, ‘Second’, ‘Third’, ‘Fourth’]<br>print(enumerate(quarters))<br>print(enumerate(quarters, start=1))</pre>
<h4 name="127e" id="127e" class="graf graf--h4 graf-after--pre">(0, ‘First’), (1, ‘Second’), (2, ‘Third’),
(3, ‘Fourth’)</h4>
<h4 name="3da0" id="3da0" class="graf graf--h4 graf-after--h4">(1, ‘First’), (2, ‘Second’), (3, ‘Third’),
(4, ‘Fourth’)</h4>
<blockquote name="879c" id="879c" class="graf graf--blockquote graf-after--h4">zip(*iterables) : creates a
zip object filled with tuples that combine 1 to 1 the items in each provided iterable.<br>Functions that
analyze iterable</blockquote>
<p name="6c20" id="6c20" class="graf graf--p graf-after--blockquote"><strong
class="markup--strong markup--p-strong">len(iterable) : returns the count of the number of
items.</strong></p>
<p name="588c" id="588c" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">max(*args, key=None) : returns the largest of two or more
arguments.</strong></p>
<p name="5e07" id="5e07" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">max(iterable, key=None) : returns the largest item in the
iterable.</strong></p>
<p name="326b" id="326b" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">key
optional function which converts an item to a value to be compared.<br>min works the same way as
max</em></p>
<p name="3f07" id="3f07" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">sum(iterable) : used with a list of numbers to generate the
total.</strong></p>
<p name="5c0b" id="5c0b" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">There is a
faster way to concatenate an array of strings into one string, so do not use sum for that.</em></p>
<p name="6ae7" id="6ae7" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">any(iterable) : returns True if any items in the iterable are
true.</strong></p>
<p name="5b99" id="5b99" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">all(iterable) : returns True is all items in the iterable are
true.</strong></p>
<h3 name="92ad" id="92ad" class="graf graf--h3 graf-after--p">Working with dictionaries</h3>
<p name="38a0" id="38a0" class="graf graf--p graf-after--h3"><strong
class="markup--strong markup--p-strong">dir(dictionary) : returns the list of keys in the
dictionary.<br>Working with sets</strong></p>
<p name="f7dc" id="f7dc" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">Union : The pipe | operator or union(*sets) function can be
used to produce a new set which is a combination of all elements in the provided set.</strong></p>
<pre name="bf18" id="bf18"
class="graf graf--pre graf-after--p">a = {1, 2, 3}<br>b = {2, 4, 6}<br>print(a | b) # => {1, 2, 3, 4, 6}</pre>
<h4 name="e5aa" id="e5aa" class="graf graf--h4 graf-after--pre">Intersection : The & operator ca be
used to produce a new set of only the elements that appear in all sets.</h4>
<pre name="7571" id="7571"
class="graf graf--pre graf-after--h4"><br>a = {1, 2, 3}<br>b = {2, 4, 6}<br>print(a & b) # => {2}<br>Difference : The — operator can be used to produce a new set of only the elements that appear in the first set and NOT the others.</pre>
<p name="4f40" id="4f40" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">Symmetric Difference : The ^ operator can be used to produce a
new set of only the elements that appear in exactly one set and not in both.</strong></p>
<pre name="7e3b" id="7e3b"
class="graf graf--pre graf-after--p graf--trailing">a = {1, 2, 3}<br>b = {2, 4, 6}<br>print(a — b) # => {1, 3}<br>print(b — a) # => {4, 6}<br>print(a ^ b) # => {1, 3, 4, 6}</pre>
</div>
</div>
</section>
<section name="1f43" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="ed03" id="ed03" class="graf graf--h3 graf--leading"><strong
class="markup--strong markup--h3-strong">For Statements<br>In python, there is only one
for loop.</strong></h3>
<p name="0d28" id="0d28" class="graf graf--p graf-after--h3">Always Includes:</p>
<blockquote name="dfb4" id="dfb4" class="graf graf--blockquote graf-after--p">1. The for keyword<br>2. A
variable name<br>3. The ‘in’ keyword<br>4. An iterable of some kid<br>5. A colon<br>6. On the next line,
an indented block of code called the for clause.</blockquote>
<p name="d64d" id="d64d" class="graf graf--p graf-after--blockquote"><strong
class="markup--strong markup--p-strong">You can use break and continue statements inside for loops as
well.</strong></p>
<p name="beac" id="beac" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">You can use the range function as the iterable for the for
loop.</strong></p>
<pre name="9439" id="9439"
class="graf graf--pre graf-after--p">print(‘My name is’)<br>for i in range(5):<br>print(‘Carlita Cinco (‘ + str(i) + ‘)’)</pre>
<pre name="946d" id="946d"
class="graf graf--pre graf-after--pre">total = 0<br>for num in range(101):<br>total += num<br>print(total)<br>Looping over a list in Python<br>for c in [‘a’, ‘b’, ‘c’]:<br>print(c)</pre>
<pre name="9ae7" id="9ae7"
class="graf graf--pre graf-after--pre">lst = [0, 1, 2, 3]<br>for i in lst:<br>print(i)</pre>
<p name="9607" id="9607" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong"><em class="markup--em markup--p-em">Common technique is to use
the len() on a pre-defined list with a for loop to iterate over the indices of the
list.</em></strong></p>
<pre name="e5df" id="e5df"
class="graf graf--pre graf-after--p">supplies = [‘pens’, ‘staplers’, ‘flame-throwers’, ‘binders’]<br>for i in range(len(supplies)):<br>print(‘Index ‘ + str(i) + ‘ in supplies is: ‘ + supplies[i])<br><br></pre>
<p name="f87b" id="f87b" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">You can loop and destructure at the same time.</strong></p>
<pre name="ce98" id="ce98"
class="graf graf--pre graf-after--p">l = 1, 2], [3, 4], [5, 6<br>for a, b in l:<br>print(a, ‘, ‘, b)</pre>
<blockquote name="ce21" id="ce21" class="graf graf--blockquote graf-after--pre">Prints 1, 2</blockquote>
<blockquote name="225f" id="225f" class="graf graf--blockquote graf-after--blockquote">Prints 3, 4
</blockquote>
<blockquote name="b5bc" id="b5bc" class="graf graf--blockquote graf-after--blockquote">Prints 5, 6
</blockquote>
<p name="c743" id="c743" class="graf graf--p graf-after--blockquote"><strong
class="markup--strong markup--p-strong">You can use values() and keys() to loop over
dictionaries.</strong></p>
<pre name="5888" id="5888"
class="graf graf--pre graf-after--p">spam = {‘color’: ‘red’, ‘age’: 42}<br>for v in spam.values():<br>print(v)</pre>
<p name="a9b3" id="a9b3" class="graf graf--p graf-after--pre"><em class="markup--em markup--p-em">Prints
red</em></p>
<p name="ba6b" id="ba6b" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Prints
42</em></p>
<pre name="b021" id="b021" class="graf graf--pre graf-after--p">for k in spam.keys():<br>print(k)</pre>
<p name="4b5c" id="4b5c" class="graf graf--p graf-after--pre"><em class="markup--em markup--p-em">Prints
color</em></p>
<p name="1f20" id="1f20" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Prints
age</em></p>
<p name="f9b7" id="f9b7" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">For loops can also iterate over both keys and values.</strong>
</p>
<p name="c118" id="c118" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">Getting tuples</strong></p>
<pre name="d2cf" id="d2cf" class="graf graf--pre graf-after--p">for i in spam.items():<br>print(i)</pre>
<p name="0bdc" id="0bdc" class="graf graf--p graf-after--pre"><em class="markup--em markup--p-em">Prints
(‘color’, ‘red’)</em></p>
<p name="6534" id="6534" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Prints
(‘age’, 42)</em></p>
<p name="5178" id="5178" class="graf graf--p graf-after--p"><em
class="markup--em markup--p-em">Destructuring to values</em></p>
<pre name="e21f" id="e21f"
class="graf graf--pre graf-after--p">for k, v in spam.items():<br>print(‘Key: ‘ + k + ‘ Value: ‘ + str(v))</pre>
<p name="1aad" id="1aad" class="graf graf--p graf-after--pre"><em class="markup--em markup--p-em">Prints
Key: age Value: 42</em></p>
<p name="d029" id="d029" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Prints
Key: color Value: red</em></p>
<p name="876a" id="876a" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">Looping over string</strong></p>
<pre name="e44e" id="e44e" class="graf graf--pre graf-after--p">for c in “abcdefg”:<br>print(c)</pre>
<p name="d6f2" id="d6f2" class="graf graf--p graf-after--pre"><strong
class="markup--strong markup--p-strong">When you order arguments within a function or function call,
the args need to occur in a particular order:</strong></p>
<p name="f99c" id="f99c" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">formal
positional args.</em></p>
<p name="b239" id="b239" class="graf graf--p graf-after--p">*args</p>
<p name="fe81" id="fe81" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">keyword
args with default values</em></p>
<p name="55ce" id="55ce" class="graf graf--p graf-after--p">**kwargs</p>
<pre name="3042" id="3042"
class="graf graf--pre graf-after--p">def example(arg_1, arg_2, *args, **kwargs):<br>pass</pre>
<pre name="137b" id="137b"
class="graf graf--pre graf-after--pre graf--trailing">def example2(arg_1, arg_2, *args, kw_1=”shark”, kw_2=”blowfish”, **kwargs):<br>pass<br><br></pre>
</div>
</div>
</section>
<section name="7787" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="4451" id="4451" class="graf graf--h3 graf--leading"><strong
class="markup--strong markup--h3-strong">Importing in Python</strong></h3>
<p name="e90b" id="e90b" class="graf graf--p graf-after--h3"><strong
class="markup--strong markup--p-strong">Modules are similar to packages in Node.js</strong><br>Come in
different types:</p>
<p name="d2c5" id="d2c5" class="graf graf--p graf-after--p">Built-In,</p>
<p name="1276" id="1276" class="graf graf--p graf-after--p">Third-Party,</p>
<p name="dac1" id="dac1" class="graf graf--p graf-after--p">Custom.</p>
<p name="e9a4" id="e9a4" class="graf graf--p graf-after--p graf--trailing"><strong
class="markup--strong markup--p-strong">All loaded using import statements.</strong></p>
</div>
</div>
</section>
<section name="cf65" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="7c4f" id="7c4f" class="graf graf--h3 graf--leading"><strong
class="markup--strong markup--h3-strong">Terms</strong></h3>
<blockquote name="1846" id="1846" class="graf graf--blockquote graf-after--h3">module : Python code in a
separate file.<br>package : Path to a directory that contains modules.<br><a href="http://init.py"
data-href="http://init.py" class="markup--anchor markup--blockquote-anchor" rel="noopener"
target="_blank"><strong class="markup--strong markup--blockquote-strong">init.py</strong></a> :
Default file for a package.<br>submodule : Another file in a module’s folder.<br>function : Function in
a module.</blockquote>
<p name="01b9" id="01b9" class="graf graf--p graf-after--blockquote"><strong
class="markup--strong markup--p-strong">A module can be any file but it is usually created by placing
a special file init.py into a folder. pic</strong></p>
<p name="5f96" id="5f96" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Try to
avoid importing with wildcards in Python.</em></p>
<p name="d333" id="d333" class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Use
multiple lines for clarity when importing.</em></p>
<pre name="5b37" id="5b37"
class="graf graf--pre graf-after--p graf--trailing">from urllib.request import (<br>HTTPDefaultErrorHandler as ErrorHandler,<br>HTTPRedirectHandler as RedirectHandler,<br>Request,<br>pathname2url,<br>url2pathname,<br>urlopen,<br>)</pre>
</div>
</div>
</section>
<section name="75a0" class="section section--body section--last">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="7040" id="7040" class="graf graf--h3 graf--leading">Watching Out for Python 2</h3>
<p name="fa5b" id="fa5b" class="graf graf--p graf-after--h3"><strong
class="markup--strong markup--p-strong">Python 3 removed <> and only uses !=</strong></p>
<p name="fc99" id="fc99" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">format() was introduced with P3</strong></p>
<p name="a8d6" id="a8d6" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">All strings in P3 are unicode and encoded.<br>md5 was
removed.</strong></p>
<p name="9670" id="9670" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">ConfigParser was renamed to configparser<br>sets were killed
in favor of set() class.</strong></p>
<h4 name="6db9" id="6db9" class="graf graf--h4 graf-after--p"><strong
class="markup--strong markup--h4-strong">print was a statement in P2, but is a function
in P3.</strong></h4>
<h3 name="9427" id="9427" class="graf graf--h3 graf-after--h4">Topics revisited (in python syntax)</h3>
<figure name="d09a" id="d09a" class="graf graf--figure graf--iframe graf-after--h3">
<script src="https://gist.github.com/bgoonz/82154f50603f73826c27377ebaa498b5.js"></script>
</figure>
<h3 name="3beb" id="3beb" class="graf graf--h3 graf-after--figure">Cheat Sheet:</h3>
<figure name="8c29" id="8c29" class="graf graf--figure graf--iframe graf-after--h3">
<script src="https://gist.github.com/bgoonz/282774d28326ff83d8b42ae77ab1fee3.js"></script>
</figure>
<h4 name="e89c" id="e89c" class="graf graf--h4 graf-after--figure">If you found this guide helpful feel
free to checkout my github/gists where I host similar content:</h4>
<p name="1c2f" id="1c2f" class="graf graf--p graf-after--h4"><a href="https://gist.github.com/bgoonz"
data-href="https://gist.github.com/bgoonz" class="markup--anchor markup--p-anchor" rel="noopener"
target="_blank">bgoonz’s gists · GitHub</a></p>
<div name="3585" id="3585" class="graf graf--mixtapeEmbed graf-after--p"><a
href="https://github.com/bgoonz" data-href="https://github.com/bgoonz"
class="markup--anchor markup--mixtapeEmbed-anchor" title="https://github.com/bgoonz"><strong
class="markup--strong markup--mixtapeEmbed-strong">bgoonz — Overview</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Web Developer, Electrical Engineer JavaScript | CSS |
Bootstrap | Python | React | Node.js | Express | Sequelize…</em>github.com</a><a
href="https://github.com/bgoonz" class="js-mixtapeImage mixtapeImage u-ignoreBlock"
data-media-id="6ee74d5200d495ddc7ddad0c92bd6dce" data-thumbnail-img-id="0*Udg3rbeFyslZ9dyl"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/0*Udg3rbeFyslZ9dyl);"></a>
</div>
<p name="cb1a" id="cb1a" class="graf graf--p graf-after--mixtapeEmbed">Or Checkout my personal Resource
Site:</p>
<div name="4bce" id="4bce" class="graf graf--mixtapeEmbed graf-after--p"><a
href="https://goofy-euclid-1cd736.netlify.app/" data-href="https://goofy-euclid-1cd736.netlify.app/"
class="markup--anchor markup--mixtapeEmbed-anchor"
title="https://goofy-euclid-1cd736.netlify.app/"><strong
class="markup--strong markup--mixtapeEmbed-strong">a/A-Student-Resources</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Edit
description</em>goofy-euclid-1cd736.netlify.app</a><a
href="https://goofy-euclid-1cd736.netlify.app/" class="js-mixtapeImage mixtapeImage u-ignoreBlock"
data-media-id="260adefce95974b3b8f27566d0434b9c" data-thumbnail-img-id="0*kHvsYWw7LFYl0PB_"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/0*kHvsYWw7LFYl0PB_);"></a>
</div>
<h3 name="2eba" id="2eba" class="graf graf--h3 graf-after--mixtapeEmbed">Python Cheat Sheet:</h3>
<figure name="d6ab" id="d6ab" class="graf graf--figure graf--iframe graf-after--h3">
<script src="https://gist.github.com/bgoonz/999163a278b987fe47fb247fd4d66904.js"></script>
</figure>
<h3 name="56d3" id="56d3" class="graf graf--h3 graf-after--figure">If you found this guide helpful feel
free to checkout my GitHub/gists where I host similar content:</h3>
<div name="357f" id="357f" class="graf graf--mixtapeEmbed graf-after--h3"><a
href="https://gist.github.com/bgoonz" data-href="https://gist.github.com/bgoonz"
class="markup--anchor markup--mixtapeEmbed-anchor" title="https://gist.github.com/bgoonz"><strong
class="markup--strong markup--mixtapeEmbed-strong">bgoonz’s gists</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Instantly share code, notes, and snippets. Web Developer,
Electrical Engineer JavaScript | CSS | Bootstrap | Python |…</em>gist.github.com</a><a
href="https://gist.github.com/bgoonz" class="js-mixtapeImage mixtapeImage u-ignoreBlock"
data-media-id="ab25adbb500306703daab23d08a7739a" data-thumbnail-img-id="0*3O67jrqm3EHjTK2H"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/0*3O67jrqm3EHjTK2H);"></a>
</div>
<div name="78cc" id="78cc" class="graf graf--mixtapeEmbed graf-after--mixtapeEmbed"><a