-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCS_Guide.html
More file actions
1595 lines (1351 loc) · 57.8 KB
/
CS_Guide.html
File metadata and controls
1595 lines (1351 loc) · 57.8 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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2018-03-14 Wed 21:35 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The Computer Science Survival Guide - Taming Python</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Mr Kelly -|- [[http://creativecommons.org/licenses/by-nc/4.0/][Creative Commons Attribution-NonCommercial License]]Creative Commons Attribution-NonCommercial License" />
<link rel="stylesheet" type="text/css" href="css/solarized-light.css" />
<style>.keyword {color:#002b36; text-align: center; background-color:#fdf6e3; border-style: dashed;}</style>
<style>.note {font-size: 12pt; text-indent: 14pt; color:#002b36; text-align: center; background-color:#fdf6e3; border-style: dashed;}</style>
<script type="text/javascript" src="http://thomasf.github.io/solarized-css/org-info.min.js">
/**
*
* @source: http://thomasf.github.io/solarized-css/org-info.min.js
*
* @licstart The following is the entire license notice for the
* JavaScript code in http://thomasf.github.io/solarized-css/org-info.min.js.
*
* Copyright (C) 2012-2017 Free Software Foundation, Inc.
*
*
* The JavaScript code in this tag is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. The code is distributed WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* @licend The above is the entire license notice
* for the JavaScript code in http://thomasf.github.io/solarized-css/org-info.min.js.
*
*/
</script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2017 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
org_html_manager.set("TOC_DEPTH", "3");
org_html_manager.set("LINK_HOME", "");
org_html_manager.set("LINK_UP", "");
org_html_manager.set("LOCAL_TOC", "1");
org_html_manager.set("VIEW_BUTTONS", "0");
org_html_manager.set("MOUSE_HINT", "underline");
org_html_manager.set("FIXED_TOC", "0");
org_html_manager.set("TOC", "1");
org_html_manager.set("VIEW", "1");
org_html_manager.setup(); // activate after the parameters are set
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">The Computer Science Survival Guide - Taming Python</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgdc01ab2">Computational Thinking</a>
<ul>
<li><a href="#orge5d8fe5">Step1: Decomposition</a></li>
<li><a href="#orgee76d01">Step2: Pattern Recognition</a></li>
<li><a href="#orge94c4bf">Step3: Abstraction</a></li>
<li><a href="#orgb73d2a6">Step4: Algorithms</a>
<ul>
<li><a href="#orgbea2486">Flowchart Shapes</a></li>
</ul>
</li>
<li><a href="#orgd1bcec8">What is Programming/Coding</a></li>
</ul>
</li>
<li><a href="#org1a8663d">Python</a>
<ul>
<li><a href="#org7413e5b">Syntax</a>
<ul>
<li><a href="#orgdf963bf">Comments</a></li>
<li><a href="#org119eae3">Variables</a></li>
<li><a href="#orgad87821">Operators</a></li>
<li><a href="#org7be2ae6">Data Types</a></li>
<li><a href="#orgcb3be0a">Type Conversion/Casting</a></li>
<li><a href="#org23aa232">Functions</a></li>
<li><a href="#org9c054e7">Defining New Functions</a></li>
<li><a href="#orgacdd1dc">Codeblocks / Indentation</a></li>
<li><a href="#org88c0b37">Syntax Reference Table</a></li>
</ul>
</li>
<li><a href="#org40d6d05">Logic</a>
<ul>
<li><a href="#org438aa1d">Comparison Operators</a></li>
<li><a href="#orge5991db">Boolean Operators</a></li>
<li><a href="#org30f9c3f">IF Statement</a></li>
<li><a href="#org7a72ef4">Loops</a></li>
<li><a href="#org3aaa478">Nested Logic</a></li>
</ul>
</li>
<li><a href="#org593e2f5">Data Structures</a>
<ul>
<li><a href="#orgf1897f5">Sequence Data Structures</a></li>
</ul>
</li>
<li><a href="#org3f5468e">File I/O</a>
<ul>
<li><a href="#org90efa43">Read</a></li>
<li><a href="#org03e7fa3">Write</a></li>
<li><a href="#org5824086">Append</a></li>
<li><a href="#orgbb6a6a7">Parsing</a></li>
</ul>
</li>
<li><a href="#org72f8054">SQL</a></li>
<li><a href="#org1d0bfa3">Handling Errors</a>
<ul>
<li><a href="#org8e24ef9">Syntax Errors</a></li>
<li><a href="#org6c5c240">Logic Error</a></li>
<li><a href="#org8f938d3">Run Time Error</a></li>
<li><a href="#org78ce5d7">Input Validation</a></li>
<li><a href="#orgcad9e08">Debugging</a></li>
<li><a href="#org3a38216">Maintainability / Robustness</a></li>
</ul>
</li>
<li><a href="#orgd3827ea">Advanced</a>
<ul>
<li><a href="#org3fdc911">OOP in Python</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-orgdc01ab2" class="outline-2">
<h2 id="orgdc01ab2">Computational Thinking</h2>
<div class="outline-text-2" id="text-orgdc01ab2">
<div class="keyword">
<p>
Computational thinking is the skill of converting a complex problem into many simple, solvable solutions.
</p>
</div>
<ul class="org-ul">
<li>Thinking computationally is not programming!</li>
<li>The answer a computer can provide is only as good as the question that is first asked of it.</li>
</ul>
</div>
<div id="outline-container-orge5d8fe5" class="outline-3">
<h3 id="orge5d8fe5">Step1: Decomposition</h3>
<div class="outline-text-3" id="text-orge5d8fe5">
<ul class="org-ul">
<li>Firstly take the complex problem and break it down into finite states or actions.</li>
<li>Then take each of these individual smaller problems and check that they can not be broken down further.</li>
</ul>
<div id="org03a8343" class="figure">
<p><object type="image/svg+xml" data="img/decomposition.svg" class="org-svg" width="70%">
Sorry, your browser does not support SVG.</object>
</p>
<p><span class="figure-number">Figure 1: </span>For example consider a vending machine (a complex problem)</p>
</div>
</div>
</div>
<div id="outline-container-orgee76d01" class="outline-3">
<h3 id="orgee76d01">Step2: Pattern Recognition</h3>
<div class="outline-text-3" id="text-orgee76d01">
<p>
Once we have decomposed complex problems we often find find patterns among the smaller problems we create.
The patterns are similarities or characteristics that some of the problems share.
</p>
<p>
We look for:
</p>
<ul class="org-ul">
<li>Problems that repeat/loop</li>
<li>Objects that have similar characteristics</li>
</ul>
<p>
Referring back to the vending machine example:
</p>
<ul class="org-ul">
<li>inserting the money and displaying a balance would likely loop.</li>
<li>items for sale in the vending machine would likely have a cost and description but others be treated similarly.</li>
</ul>
<p>
Finding these patterns allows us to better abstract like elements from the complex problem and solve it more efficiently.
</p>
</div>
</div>
<div id="outline-container-orge94c4bf" class="outline-3">
<h3 id="orge94c4bf">Step3: Abstraction</h3>
<div class="outline-text-3" id="text-orge94c4bf">
<p>
For each individual problem note the important details outline the entities (nouns) and their actions (verbs), ignore information not absolutely essential to the problem.
</p>
<p>
The vending machine abstracted
</p>
<ul class="org-ul">
<li>the user inserts <inputs> currency</li>
<li>the user makes <inputs> a selection from a menu or a refund</li>
<li>the machine [processes] if the user can afford the selected item</li>
<li>the machine calculates [processes] the change</li>
<li>the machine <outputs> the paid item and change</li>
</ul>
</div>
</div>
<div id="outline-container-orgb73d2a6" class="outline-3">
<h3 id="orgb73d2a6">Step4: Algorithms</h3>
<div class="outline-text-3" id="text-orgb73d2a6">
<div class="keyword">
<p>
An algorithm is a simple set of steps to solve a problem.
</p>
</div>
<ul class="org-ul">
<li>Algorithims are the final result of computational thinking.</li>
<li>For each of the individual problems provide simple step by step solutions.</li>
</ul>
<p>
Almost all problems simplify into three stages:
</p>
<ul class="org-ul">
<li>Input : enter values/entities (nouns)</li>
<li>Process : do a calculation or decision (verbs)</li>
<li>Output : print answer to the screen/file (nouns)</li>
</ul>
<div id="org4ca481c" class="figure">
<p><object type="image/svg+xml" data="img/algorithim.svg" class="org-svg" width="80%">
Sorry, your browser does not support SVG.</object>
</p>
<p><span class="figure-number">Figure 2: </span>Algorithms are written as pseudocode and flowcharts.</p>
</div>
</div>
<div id="outline-container-orgbea2486" class="outline-4">
<h4 id="orgbea2486">Flowchart Shapes</h4>
<div class="outline-text-4" id="text-orgbea2486">
<div id="orgb755cc7" class="figure">
<p><img src="img/REPLACEME.png" alt="REPLACEME.png" width="80%" />
</p>
<p><span class="figure-number">Figure 3: </span>Flowchart Shapes.</p>
</div>
</div>
</div>
</div>
<div id="outline-container-orgd1bcec8" class="outline-3">
<h3 id="orgd1bcec8">What is Programming/Coding</h3>
<div class="outline-text-3" id="text-orgd1bcec8">
<ul class="org-ul">
<li>Programming or coding is the process of entering the steps of an algorithim (instructions) into a computer.</li>
<li>To run an algorithim on a computer it needs to be coded into very explicit binary instructions like 11100110.</li>
<li>Fortunately we no longer program computers directly in binary but use a simpler (higher) programming language like python.</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-org1a8663d" class="outline-2">
<h2 id="org1a8663d">Python</h2>
<div class="outline-text-2" id="text-org1a8663d">
<ul class="org-ul">
<li>Python is a high level English like programming language</li>
<li>look at the following python example :</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"hello world"</span><span style="color: #4f97d7;">)</span>
</pre>
</div>
<ul class="org-ul">
<li>When the Python code above is run/executed it is interpreted one line at a time into bytecode instructions that the computer can understand.</li>
</ul>
</div>
<div id="outline-container-org7413e5b" class="outline-3">
<h3 id="org7413e5b">Syntax</h3>
<div class="outline-text-3" id="text-org7413e5b">
<p>
Computer Languages such as Python have rules just like human Languages. For example in English a sentence starts with a capital letter and ends in a full stop.
</p>
<div class="keyword">
<p>
Syntax: The rules of a language
</p>
</div>
<p>
Breaking the rules of any language for example incorrect spelling, punctuation or grammar renders it incomprehensible.
</p>
<ul class="org-ul">
<li>Python is no exception and will fail to interpret/understand the code you have entered.</li>
<li>When python is unable to understand your code it will do its best to highlight the <a href="#org8e24ef9">Syntax Errors</a>.</li>
</ul>
</div>
<div id="outline-container-orgdf963bf" class="outline-4">
<h4 id="orgdf963bf">Comments</h4>
<div class="outline-text-4" id="text-orgdf963bf">
<p>
Comments are notes added to code to remind programmers what a piece of code does.
In python a hash character # will begin a comment that extends to the end of the line.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">this is a comment ignored by the interpreter</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"this is code"</span><span style="color: #4f97d7;">)</span> <span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">this is also a comment</span>
</pre>
</div>
<div class="keyword">
<p>
Comments are notes for humans ignored by the interpreter
</p>
</div>
<div class="note">
<p>
Comments: Always begin with a #
</p>
</div>
</div>
</div>
<div id="outline-container-org119eae3" class="outline-4">
<h4 id="org119eae3">Variables</h4>
<div class="outline-text-4" id="text-org119eae3">
<p>
Variables are a way of labelling data that is going to be used in a our code.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #7590db;">name</span> = <span style="color: #2d9574;">"Mr Kelly"</span>
</pre>
</div>
<div class="keyword">
<p>
A variable is a named value that can be changed
</p>
</div>
<div class="note">
<ul class="org-ul">
<li>Variable names should always relate to the data they hold</li>
<li>Variable names can’t start with a number or contain spaces (use under_scores or CamelCase instead).</li>
</ul>
</div>
<div class="note">
<p>
Variables = are always set after they are named
</p>
</div>
</div>
</div>
<div id="outline-container-orgad87821" class="outline-4">
<h4 id="orgad87821">Operators</h4>
<div class="outline-text-4" id="text-orgad87821">
<p>
Operators are symbols that change the value of a variable :
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Symbol</th>
<th scope="col" class="org-left">Operation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">=</td>
<td class="org-left">Assign the variable on the left the value of the right</td>
</tr>
<tr>
<td class="org-left">+</td>
<td class="org-left">add</td>
</tr>
<tr>
<td class="org-left">-</td>
<td class="org-left">subtract</td>
</tr>
<tr>
<td class="org-left">*</td>
<td class="org-left">multiply</td>
</tr>
<tr>
<td class="org-left">/ </td>
<td class="org-left">divide</td>
</tr>
<tr>
<td class="org-left">//</td>
<td class="org-left">integer division</td>
</tr>
<tr>
<td class="org-left">%</td>
<td class="org-left">Modulus</td>
</tr>
</tbody>
</table>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">This code sets the variable answer to the value of variable1 plus 2</span>
<span style="color: #7590db;">answer</span> = variable1 + <span style="color: #a45bad;">2</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org7be2ae6" class="outline-4">
<h4 id="org7be2ae6">Data Types</h4>
<div class="outline-text-4" id="text-org7be2ae6">
<p>
A data type describes how data within a variable is stored.
</p>
<ul class="org-ul">
<li>A python variable can store the following types :</li>
</ul>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Type</th>
<th scope="col" class="org-left">Definition</th>
<th scope="col" class="org-left">Literal</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">integer</td>
<td class="org-left">Whole Number</td>
<td class="org-left">1</td>
</tr>
<tr>
<td class="org-left">float</td>
<td class="org-left">Decimal Number</td>
<td class="org-left">1.0</td>
</tr>
<tr>
<td class="org-left">boolean</td>
<td class="org-left">A value that can only be TRUE or FALSE</td>
<td class="org-left">True or False</td>
</tr>
<tr>
<td class="org-left">string</td>
<td class="org-left">Strings are variables that hold "Text"</td>
<td class="org-left">"Text"</td>
</tr>
</tbody>
</table>
<div class="note">
<p>
"String": Literals are always inside "quotes"
to tell the computer that this is
"TEXT" not code like
print()
</p>
</div>
</div>
</div>
<div id="outline-container-orgcb3be0a" class="outline-4">
<h4 id="orgcb3be0a">Type Conversion/Casting</h4>
<div class="outline-text-4" id="text-orgcb3be0a">
<p>
When using operators we frequently need to cast variables into compatible types.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">This code has a Type Error </span>
<span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">python can't add strings(text) and intergers(numbers) together.</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span> <span style="color: #a45bad;">99</span> + <span style="color: #2d9574;">" Years old"</span> <span style="color: #4f97d7;">)</span>
<span style="color: #7590db;">answer</span> = <span style="color: #4f97d7;">input</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"Please enter a number : "</span> <span style="color: #4f97d7;">)</span> + <span style="color: #a45bad;">1</span>
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-right" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Function</th>
<th scope="col" class="org-left">Conversion</th>
<th scope="col" class="org-right">Output</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">float(“99”)</td>
<td class="org-left">string -> float</td>
<td class="org-right">99.0</td>
</tr>
<tr>
<td class="org-left">int(“99”)</td>
<td class="org-left">string -> integer</td>
<td class="org-right">99</td>
</tr>
<tr>
<td class="org-left">str(99)</td>
<td class="org-left">number -> string</td>
<td class="org-right">“99”</td>
</tr>
</tbody>
</table>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">However Python can add strings(text) with strings(text)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span> <span style="color: #4f97d7;">str</span><span style="color: #bc6ec5;">(</span><span style="color: #a45bad;">99</span><span style="color: #bc6ec5;">)</span> + <span style="color: #2d9574;">" Years old"</span> <span style="color: #4f97d7;">)</span>
<span style="color: #2aa1ae; background-color: #292e34;"># </span><span style="color: #2aa1ae; background-color: #292e34;">Python can also add intergers(numbers) with intergers(numbers) </span>
<span style="color: #7590db;">answer</span> = <span style="color: #4f97d7;">int</span><span style="color: #4f97d7;">(</span> <span style="color: #4f97d7;">input</span><span style="color: #bc6ec5;">(</span> <span style="color: #2d9574;">"Please enter a number : "</span> <span style="color: #bc6ec5;">)</span> <span style="color: #4f97d7;">)</span> + <span style="color: #a45bad;">1</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org23aa232" class="outline-4">
<h4 id="org23aa232">Functions</h4>
<div class="outline-text-4" id="text-org23aa232">
<div id="org3ee6601" class="figure">
<p><object type="image/svg+xml" data="img/Function.svg" class="org-svg" width="70%">
Sorry, your browser does not support SVG.</object>
</p>
<p><span class="figure-number">Figure 4: </span>Function</p>
</div>
<p>
Functions are a way of reusing sets of instructions that perform the same task.
</p>
<ul class="org-ul">
<li>For example two common functions used in python are print and input:</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #7590db;">name</span> = <span style="color: #4f97d7;">input</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"Please enter your name : "</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"hello "</span>, name<span style="color: #4f97d7;">)</span>
</pre>
</div>
<ul class="org-ul">
<li>input( "Question ?" )
<ul class="org-ul">
<li>Takes a string to display</li>
<li>captures the users keystrokes until the enter key is pressed</li>
<li>then returns what the user has typed</li>
</ul></li>
<li>print( "OUTPUT" )
<ul class="org-ul">
<li>takes a "string" and displays it on the screen</li>
</ul></li>
</ul>
<div class="keyword">
<p>
Functions are named commands() that may take inputs and/or return an output
</p>
</div>
<div class="note">
<p>
Note Functions always have brackets() after their names
</p>
</div>
</div>
</div>
<div id="outline-container-org9c054e7" class="outline-4">
<h4 id="org9c054e7">Defining New Functions</h4>
<div class="outline-text-4" id="text-org9c054e7">
<p>
You can also define your own functions using the syntax below:
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #4f97d7; font-weight: bold;">def</span> <span style="color: #bc6ec5; font-weight: bold;">function_name</span><span style="color: #4f97d7;">(</span>input_variable1,input_variable2<span style="color: #4f97d7;">)</span>:
<span style="color: #2aa1ae;">""" Comment describing what the function does """</span>
CODEBLOCK
CODEBLOCK
CODEBLOCK
<span style="color: #4f97d7; font-weight: bold;">return</span> output_variable
</pre>
</div>
<div class="note">
<p>
Whenever you reuse your code it should be in a function…
</p>
</div>
<div class="note">
<p>
note the use of : to start an indented codeblock
</p>
</div>
</div>
</div>
<div id="outline-container-orgacdd1dc" class="outline-4">
<h4 id="orgacdd1dc">Codeblocks / Indentation</h4>
<div class="outline-text-4" id="text-orgacdd1dc">
<p>
The purpose of a code block is to define statements that will be executed together.
Codeblocks are needed when code forks, loops or is otherwise referenced or reused.
</p>
<p>
In many high level languages instructions can be grouped together into blocks using curly brackets {…}.
</p>
<div class="org-src-container">
<pre class="src src-c++"><span style="color: #ce537a; font-weight: bold;">int</span> <span style="color: #bc6ec5; font-weight: bold;">add</span><span style="color: #4f97d7;">(</span><span style="color: #ce537a; font-weight: bold;">int</span> <span style="color: #7590db;">x</span>, <span style="color: #ce537a; font-weight: bold;">int</span> <span style="color: #7590db;">y</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7;">{</span> <span style="color: #2aa1ae; background-color: #292e34;">// </span><span style="color: #2aa1ae; background-color: #292e34;">start a block</span>
<span style="color: #4f97d7; font-weight: bold;">return</span> x + y;
<span style="color: #4f97d7;">}</span> <span style="color: #2aa1ae; background-color: #292e34;">// </span><span style="color: #2aa1ae; background-color: #292e34;">end a block</span>
</pre>
</div>
<p>
Python separates code-blocks purely on indentation alone as follows :
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #4f97d7; font-weight: bold;">def</span> <span style="color: #bc6ec5; font-weight: bold;">add</span><span style="color: #4f97d7;">(</span>x,y<span style="color: #4f97d7;">)</span>:
<span style="color: #2aa1ae;">""" As the following lines are indented they belong to the add function """</span>
<span style="color: #7590db;">Ans</span> = x + y
<span style="color: #4f97d7; font-weight: bold;">return</span> Ans
</pre>
</div>
</div>
</div>
<div id="outline-container-org88c0b37" class="outline-4">
<h4 id="org88c0b37">Syntax Reference Table</h4>
<div class="outline-text-4" id="text-org88c0b37">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Syntax Table</th>
<th scope="col" class="org-left"> </th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"># Comment</td>
<td class="org-left">Text for humans ignored by the computer</td>
</tr>
<tr>
<td class="org-left">Variable =</td>
<td class="org-left">A named value that can be changed</td>
</tr>
<tr>
<td class="org-left">“String”</td>
<td class="org-left">A variable that holds "Text"</td>
</tr>
<tr>
<td class="org-left">Function( inputs )</td>
<td class="org-left">A named command() that may take inputs and/or return an output</td>
</tr>
<tr>
<td class="org-left">Operators</td>
<td class="org-left">+ plus - minus * times / divide = assign</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="outline-container-org40d6d05" class="outline-3">
<h3 id="org40d6d05">Logic</h3>
<div class="outline-text-3" id="text-org40d6d05">
<p>
Logic statements allow the programmer to control the flow of a program to repeat or execute codeblocks selectively.
</p>
</div>
<div id="outline-container-org438aa1d" class="outline-4">
<h4 id="org438aa1d">Comparison Operators</h4>
<div class="outline-text-4" id="text-org438aa1d">
<p>
Frequently an algorithm needs to compare values (variables) in order make a decision.
</p>
<div class="note">
<p>
Note the result of a comparison operation is always a boolean ( True or False ).
</p>
</div>
<p>
Python has the following operators to compare variables :
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Symbol</th>
<th scope="col" class="org-left">Operation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">==</td>
<td class="org-left">Is equal too</td>
</tr>
<tr>
<td class="org-left">!=</td>
<td class="org-left">Is not equal too</td>
</tr>
<tr>
<td class="org-left"><</td>
<td class="org-left">Less than</td>
</tr>
<tr>
<td class="org-left"><=</td>
<td class="org-left">Less than or equal too</td>
</tr>
<tr>
<td class="org-left">></td>
<td class="org-left">Greater than</td>
</tr>
<tr>
<td class="org-left">>=</td>
<td class="org-left">Greater than or equal too</td>
</tr>
</tbody>
</table>
<div class="note">
<p>
Note the double == means test if the left is equal to the right.
</p>
</div>
<div class="note">
<p>
Be careful with the use of > GREATER THAN and < LESS THAN it is very easy to mix them up.
</p>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">1</span>==<span style="color: #a45bad;">1</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">1</span>!=<span style="color: #a45bad;">2</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">1</span><<span style="color: #a45bad;">2</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">1</span>><span style="color: #a45bad;">2</span><span style="color: #4f97d7;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orge5991db" class="outline-4">
<h4 id="orge5991db">Boolean Operators</h4>
<div class="outline-text-4" id="text-orge5991db">
<p>
For many algorithms logic conditions/statements need to be combined.
</p>
<p>
Python provides the following boolean operators:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Symbol</th>
<th scope="col" class="org-left">Operation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">and</td>
<td class="org-left">TRUE and FALSE = FALSE</td>
</tr>
<tr>
<td class="org-left">or</td>
<td class="org-left">TRUE or FALSE = TRUE</td>
</tr>
<tr>
<td class="org-left">not</td>
<td class="org-left">Inverts Statement Logic</td>
</tr>
</tbody>
</table>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">True</span> <span style="color: #4f97d7; font-weight: bold;">and</span> <span style="color: #a45bad;">True</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">True</span> <span style="color: #4f97d7; font-weight: bold;">and</span> <span style="color: #a45bad;">True</span> <span style="color: #4f97d7; font-weight: bold;">and</span> <span style="color: #a45bad;">False</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #a45bad;">True</span> <span style="color: #4f97d7; font-weight: bold;">or</span> <span style="color: #a45bad;">False</span> <span style="color: #4f97d7; font-weight: bold;">or</span> <span style="color: #a45bad;">False</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #4f97d7; font-weight: bold;">not</span> <span style="color: #a45bad;">True</span><span style="color: #4f97d7;">)</span>
</pre>
</div>
<div class="note">
<ul class="org-ul">
<li>A single False in a boolean AND statement equates to False.</li>
<li>A single True in a boolean OR statement equates to True.</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-org30f9c3f" class="outline-4">
<h4 id="org30f9c3f">IF Statement</h4>
<div class="outline-text-4" id="text-org30f9c3f">
<div class="keyword">
<p>
IF : Selectively run a codeblock only if a condition is meet (True).
</p>
</div>
<div id="org0c09ee5" class="figure">
<p><object type="image/svg+xml" data="img/if.svg" class="org-svg" width="80%">
Sorry, your browser does not support SVG.</object>
</p>
<p><span class="figure-number">Figure 5: </span>if, else if (elif) and else</p>
</div>
<p>
For example :
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #7590db;">score</span> = <span style="color: #a45bad;">42</span>
<span style="color: #4f97d7; font-weight: bold;">if</span> score > <span style="color: #a45bad;">80</span>:
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"Excellent"</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">elif</span> score >= <span style="color: #a45bad;">50</span>
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"Well Done"</span><span style="color: #4f97d7;">)</span>
<span style="color: #4f97d7; font-weight: bold;">else</span>:
<span style="color: #4f97d7; font-weight: bold;">print</span><span style="color: #4f97d7;">(</span><span style="color: #2d9574;">"Please Try Again"</span><span style="color: #4f97d7;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org7a72ef4" class="outline-4">
<h4 id="org7a72ef4">Loops</h4>
<div class="outline-text-4" id="text-org7a72ef4">
<p>
Loops allow us to repeat code according to a condition. Ie:
</p>
<div id="orgb2d79ec" class="figure">
<p><object type="image/svg+xml" data="img/loops.svg" class="org-svg" width="50%">
Sorry, your browser does not support SVG.</object>
</p>
<p><span class="figure-number">Figure 6: </span>Loops</p>
</div>
<ul class="org-ul">
<li>Open umbrella while it is raining</li>
<li>For each student in classroom give candy</li>
<li>Repeat this instruction 10 times</li>
</ul>
</div>
<ul class="org-ul"><li><a id="org7d47559"></a>While Loop<br /><div class="outline-text-5" id="text-org7d47559">
<div class="keyword">
<p>
While: A statement that repeats a codeblock while the condition is True.
</p>