-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.html
11915 lines (11757 loc) · 439 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>README</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="python-programming-for-beginners">
Python Programming For Beginners
</h1>
<p>
Python is a very popular coding language. Little people use it for serious
projects, but it is still useful to learn. It was created in 1991 by
<code>Guido van Rossum</code>.<br />
Look at a few uses of python:
</p>
<pre><code>.
├── DOCS
│ ├── _static
│ └── tutorial
├── Readings
├── exploring-python
├── learn-python3-samples
│ ├── advance
│ ├── async
│ ├── basic
│ ├── commonlib
│ ├── context
│ ├── db
│ ├── debug
│ ├── function
│ ├── functional
│ ├── gui
│ │ └── turtle
│ ├── io
│ ├── mail
│ ├── micropython
│ │ ├── hellorobot
│ │ ├── rccar
│ │ ├── smallcar
│ │ └── tank
│ ├── module
│ ├── multitask
│ ├── oop_advance
│ ├── oop_basic
│ ├── regex
│ ├── socket
│ ├── test
│ └── web
│ └── mvc
│ └── templates
├── prac-4-beginners
│ ├── code
│ │ ├── 05-interactive-code
│ │ │ └── m-n-m
│ │ ├── 06-organizing-code-with-functions
│ │ │ └── rocks-game
│ │ ├── 07-data-structures
│ │ │ ├── rocks-game
│ │ │ └── simple_dict
│ │ ├── 08-problem-solving
│ │ │ └── tictactoe
│ │ ├── 09-working-with-files
│ │ │ └── rocks-game
│ │ ├── 10-external-libraries
│ │ │ └── rocks-game
│ │ └── 11-error-handling
│ │ └── rocks-game
│ ├── practices
│ │ ├── ch04_first_lines
│ │ ├── ch05_interactive_code
│ │ ├── ch06-organizing-code-with-functions
│ │ ├── ch07_data_structures
│ │ ├── ch08_problem_solving
│ │ │ └── readme_files
│ │ ├── ch09_working_with_files
│ │ ├── ch10_external_libraries
│ │ └── ch11-error-handling
│ ├── readme_resources
│ └── solutions
│ ├── ch-04-writing-your-first-lines-of-code
│ ├── ch-05-interactive-code
│ ├── ch-06-organizing-code-with-functions
│ ├── ch-07-data-structures
│ ├── ch-08-problem-solving
│ ├── ch-09-working-with-files
│ ├── ch-10-external-libraries
│ └── ch-11-error-handling
├── projecteuler-solutions
├── projects-DS
│ ├── Data-Structures-Notes-master
│ │ ├── BST_and_Heaps
│ │ ├── Doubly_Linked_List
│ │ ├── Interview_Problems
│ │ └── Linked_Lists
│ ├── Data-Structures-and-Algorithms-master
│ │ ├── assets
│ │ └── src
│ │ ├── algorithms
│ │ │ ├── strings
│ │ │ │ └── knuth-morris-pratt
│ │ │ └── tree
│ │ │ ├── breadth-first-search
│ │ │ └── depth-first-search
│ │ ├── analysis-of-algorithms
│ │ │ └── asymptotic-notation
│ │ └── data-structures
│ │ ├── graph
│ │ ├── linked lists
│ │ └── tree
│ │ ├── binary-search-tree
│ │ └── binary-tree
│ ├── Data-Structures-master
│ │ ├── avl_tree
│ │ ├── binary_search_tree
│ │ ├── doubly_linked_list
│ │ ├── heap
│ │ ├── javascript
│ │ │ └── tests
│ │ ├── lru_cache
│ │ ├── python
│ │ │ ├── binary_search_tree
│ │ │ ├── doubly_linked_list
│ │ │ ├── heap
│ │ │ ├── linked_list
│ │ │ └── queue
│ │ └── queue
│ └── data_struct_and_algo-master
└── python-mega-algo
├── arithmetic_analysis
│ └── image_data
├── backtracking
├── bit_manipulation
├── blockchain
├── boolean_algebra
├── cellular_automata
├── ciphers
├── compression
│ └── image_data
├── computer_vision
├── conversions
├── data_structures
│ ├── binary_tree
│ ├── disjoint_set
│ ├── hashing
│ │ └── number_theory
│ ├── heap
│ ├── linked_list
│ ├── queue
│ ├── stacks
│ └── trie
├── digital_image_processing
│ ├── dithering
│ ├── edge_detection
│ ├── filters
│ ├── histogram_equalization
│ │ ├── image_data
│ │ └── output_data
│ ├── image_data
│ ├── resize
│ └── rotation
├── divide_and_conquer
├── dynamic_programming
├── electronics
├── file_transfer
│ └── tests
├── fractals
├── fuzzy_logic
├── genetic_algorithm
├── geodesy
├── graphics
├── graphs
│ └── tests
├── hashes
├── knapsack
│ └── tests
├── linear_algebra
│ └── src
├── machine_learning
│ ├── forecasting
│ └── lstm
├── maths
│ ├── images
│ └── series
├── matrix
│ └── tests
├── networking_flow
├── neural_network
├── other
├── physics
├── project_euler
│ ├── problem_001
│ ├── problem_002
│ ├── problem_003
│ ├── problem_004
│ ├── problem_005
│ ├── problem_006
│ ├── problem_007
│ ├── problem_008
│ ├── problem_009
│ ├── problem_010
│ ├── problem_011
│ ├── problem_012
│ ├── problem_013
│ ├── problem_014
│ ├── problem_015
│ ├── problem_016
│ ├── problem_017
│ ├── problem_018
│ ├── problem_019
│ ├── problem_020
│ ├── problem_021
│ ├── problem_022
│ ├── problem_023
│ ├── problem_024
│ ├── problem_025
│ ├── problem_026
│ ├── problem_027
│ ├── problem_028
│ ├── problem_029
│ ├── problem_030
│ ├── problem_031
│ ├── problem_032
│ ├── problem_033
│ ├── problem_034
│ ├── problem_035
│ ├── problem_036
│ ├── problem_037
│ ├── problem_038
│ ├── problem_039
│ ├── problem_040
│ ├── problem_041
│ ├── problem_042
│ ├── problem_043
│ ├── problem_044
│ ├── problem_045
│ ├── problem_046
│ ├── problem_047
│ ├── problem_048
│ ├── problem_049
│ ├── problem_050
│ ├── problem_051
│ ├── problem_052
│ ├── problem_053
│ ├── problem_054
│ ├── problem_055
│ ├── problem_056
│ ├── problem_057
│ ├── problem_058
│ ├── problem_059
│ ├── problem_062
│ ├── problem_063
│ ├── problem_064
│ ├── problem_065
│ ├── problem_067
│ ├── problem_069
│ ├── problem_070
│ ├── problem_071
│ ├── problem_072
│ ├── problem_074
│ ├── problem_075
│ ├── problem_076
│ ├── problem_077
│ ├── problem_080
│ ├── problem_081
│ ├── problem_085
│ ├── problem_086
│ ├── problem_087
│ ├── problem_089
│ ├── problem_091
│ ├── problem_097
│ ├── problem_099
│ ├── problem_101
│ ├── problem_102
│ ├── problem_107
│ ├── problem_109
│ ├── problem_112
│ ├── problem_113
│ ├── problem_119
│ ├── problem_120
│ ├── problem_121
│ ├── problem_123
│ ├── problem_125
│ ├── problem_129
│ ├── problem_135
│ ├── problem_173
│ ├── problem_174
│ ├── problem_180
│ ├── problem_188
│ ├── problem_191
│ ├── problem_203
│ ├── problem_206
│ ├── problem_207
│ ├── problem_234
│ ├── problem_301
│ └── problem_551
├── quantum
├── scheduling
├── scripts
├── searches
├── sorts
├── strings
└── web_programming
286 directories</code></pre>
<p>
Python syntax was made for readability, and easy editing. For example, the
python language uses a <code>:</code> and indented code, while javascript
and others generally use <code>{}</code> and indented code.
</p>
<p>
Lets create a
<a href="https://repl.it/languages/python3">python 3</a> repl, and call it
<em>Hello World</em>. Now you have a blank file called <em>main.py</em>.
Now let us write our first line of code:
</p>
<p><em>helloworld.py</em></p>
<pre><code>print('Hello world!')</code></pre>
<blockquote>
<p>
Brian Kernighan actually wrote the first “Hello, World!” program as part
of the documentation for the BCPL programming language developed by
Martin Richards.
</p>
</blockquote>
<p>
Now, press the run button, which <sub>obviously</sub> runs the code. If
you are not using replit, this will not work. You should research how to
run a file with your text editor.
</p>
<p>
If you look to your left at the console where hello world was just
printed, you can see a <code>></code>, <code>>>></code>, or
<code>$</code> depending on what you are using. After the prompt, try
typing a line of code.
</p>
<pre><code>Python 3.6.1 (default, Jun 21 2017, 18:48:35)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
> print('Testing command line')
Testing command line
> print('Are you sure this works?')
Are you sure this works?
></code></pre>
<p>
The command line allows you to execute single lines of code at a time. It
is often used when trying out a new function or method in the language.
</p>
<p>
Another cool thing that you can generally do with all languages, are
comments. In python, a comment starts with a <code>#</code>. The computer
ignores all text starting after the <code>#</code>.
</p>
<p><em>shortcom.py</em></p>
<pre><code># Write some comments!</code></pre>
<p>
If you have a huge comment, do <strong>not</strong> comment all the 350
lines, just put <code>'''</code> before it, and <code>'''</code> at the
end. Technically, this is not a comment but a string, but the computer
still ignores it, so we will use it.
</p>
<p><em>longcom.py</em></p>
<pre><code>'''
Dear PYer,
I am confused about how you said you could use triple quotes to make
SUPER
LONG
COMMENTS
!
I am wondering if this is true,
and if so,
I am wondering if this is correct.
Could you help me with this?
Thanks,
Random guy who used your tutorial.
'''
print('Testing')</code></pre>
<p>
Unlike many other languages, there is no <code>var</code>,
<code>let</code>, or <code>const</code> to declare a variable in python.
You simply go <code>name = 'value'</code>.
</p>
<p><em>vars1.py</em></p>
<pre><code>x = 5
y = 7
z = x*y # 35
print(z) # => 35</code></pre>
<p>
Remember, there is a difference between integers and strings.
<em>Remember: String = <code>""</code>.</em> To convert between these two,
you can put an int in a <code>str()</code> function, and a string in a
<code>int()</code> function. There is also a less used one, called a
float. Mainly, these are integers with decimals. Change them using the
<code>float()</code> command.
</p>
<p><em>vars2.py</em></p>
<pre><code>x = 5
x = str(x)
b = '5'
b = int(b)
print('x = ', x, '; b = ', str(b), ';') # => x = 5; b = 5;</code></pre>
<p>
Instead of using the <code>,</code> in the print function, you can put a
<code>+</code> to combine the variables and string.
</p>
<p>There are many operators in python:</p>
<ul>
<li><code>+</code></li>
<li><code>-</code></li>
<li><code>/</code></li>
<li>
<code>*</code><br />
These operators are the same in most languages, and allow for addition,
subtraction, division, and multiplicaiton.<br />
Now, we can look at a few more complicated ones:
</li>
<li><code>%</code></li>
<li><code>//</code></li>
<li><code>**</code></li>
<li><code>+=</code></li>
<li><code>-=</code></li>
<li><code>/=</code></li>
<li>
<code>*=</code><br />
Research these if you want to find out more…
</li>
</ul>
<p><em>simpleops.py</em></p>
<pre><code>x = 4
a = x + 1
a = x - 1
a = x * 2
a = x / 2</code></pre>
<p>
You should already know everything shown above, as it is similar to other
languages. If you continue down, you will see more complicated ones.
</p>
<p><em>complexop.py</em></p>
<pre><code>a += 1
a -= 1
a *= 2
a /= 2</code></pre>
<p>
The ones above are to edit the current value of the variable.<br />
Sorry to JS users, as there is no <code>i++;</code> or anything.
</p>
<blockquote>
<p>
Fun Fact:<br />
The python language was named after Monty Python.
</p>
</blockquote>
<p>
If you really want to know about the others, view
<a href="https://www.tutorialspoint.com/python/python_basic_operators.htm"
>Py Operators</a
>
</p>
<p>
Like the title?<br />
Anyways, a <code>'</code> and a <code>"</code> both indicate a string, but
<strong>do not combine them!</strong>
</p>
<p><em>quotes.py</em></p>
<pre><code>x = 'hello' # Good
x = "hello" # Good
x = "hello' # ERRORRR!!!</code></pre>
<p><em>slicing.py</em></p>
<h3 id="string-slicing">String Slicing</h3>
<p>
You can look at only certain parts of the string by slicing it, using
<code>[num:num]</code>.<br />
The first number stands for how far in you go from the front, and the
second stands for how far in you go from the back.
</p>
<pre><code>x = 'Hello everybody!'
x[1] # 'e'
x[-1] # '!'
x[5] # ' '
x[1:] # 'ello everybody!'
x[:-1] # 'Hello everybod'
x[2:-3] # 'llo everyb'</code></pre>
<h3 id="methods-and-functions">Methods and Functions</h3>
<p>Here is a list of functions/methods we will go over:</p>
<ul>
<li><code>.strip()</code></li>
<li><code>len()</code></li>
<li><code>.lower()</code></li>
<li><code>.upper()</code></li>
<li><code>.replace()</code></li>
<li><code>.split()</code></li>
</ul>
<p>
I will make you try these out yourself. See if you can figure out how they
work.
</p>
<p><em>strings.py</em></p>
<pre><code>x = " Testing, testing, testing, testing "
print(x.strip())
print(len(x))
print(x.lower())
print(x.upper())
print(x.replace('test', 'runn'))
print(x.split(','))</code></pre>
<p>Good luck, see you when you come back!</p>
<p>
Input is a function that gathers input entered from the user in the
command line. It takes one optional parameter, which is the users prompt.
</p>
<p><em>inp.py</em></p>
<pre><code>print('Type something: ')
x = input()
print('Here is what you said: ', x)</code></pre>
<p>
If you wanted to make it smaller, and look neater to the user, you could
do…
</p>
<p><em>inp2.py</em></p>
<pre><code>print('Here is what you said: ', input('Type something: '))</code></pre>
<p>
Running:<br />
<em>inp.py</em>
</p>
<pre><code>Type something:
Hello World
Here is what you said: Hello World</code></pre>
<p><em>inp2.py</em></p>
<pre><code>Type something: Hello World
Here is what you said: Hello World</code></pre>
<p>
Python has created a lot of functions that are located in other .py files.
You need to import these <strong>modules</strong> to gain access to the,,
You may wonder why python did this. The purpose of separate modules is to
make python faster. Instead of storing millions and millions of functions,
, it only needs a few basic ones. To import a module, you must write
<code>input <modulename></code>. Do not add the .py extension to the
file name. In this example , we will be using a python created module
named random.
</p>
<p><em>module.py</em></p>
<pre><code>import random</code></pre>
<p>
Now, I have access to all functions in the random.py file. To access a
specific function in the module, you would do
<code><module>.<function></code>. For example:
</p>
<p><em>module2.py</em></p>
<pre><code>import random
print(random.randint(3,5)) # Prints a random number between 3 and 5</code></pre>
<blockquote>
<p>
Pro Tip:<br />
Do <code>from random import randint</code> to not have to do
<code>random.randint()</code>, just <code>randint()</code><br />
To import all functions from a module, you could do
<code>from random import *</code>
</p>
</blockquote>
<p>
Loops allow you to repeat code over and over again. This is useful if you
want to print Hi with a delay of one second 100 times.
</p>
<h4 id="for-loop"><code>for</code> Loop</h4>
<p>
The for loop goes through a list of variables, making a seperate variable
equal one of the list every time.<br />
Let’s say we wanted to create the example above.
</p>
<p><em>loop.py</em></p>
<pre><code>from time import sleep
for i in range(100):
print('Hello')
sleep(.3)</code></pre>
<p>
This will print Hello with a .3 second delay 100 times. This is just one
way to use it, but it is usually used like this:
</p>
<p><em>loop2.py</em></p>
<pre><code>import time
for number in range(100):
print(number)
time.sleep(.1)</code></pre>
<p>
<a
href="https://storage.googleapis.com/replit/images/1539649280875_37d22e6d49e8e8fbc453631def345387.pn"
></a>
</p>
<h4 id="while-loop"><code>while</code> Loop</h4>
<p>
The while loop runs the code while something stays true. You would put
<code>while <expression></code>. Every time the loop runs, it
evaluates if the expression is True. It it is, it runs the code, if not it
continues outside of the loop. For example:
</p>
<p><em>while.py</em></p>
<pre><code>while True: # Runs forever
print('Hello World!')</code></pre>
<p>Or you could do:</p>
<p><em>while2.py</em></p>
<pre><code>import random
position = '<placeholder>'
while position != 1: # will run at least once
position = random.randint(1, 10)
print(position)</code></pre>
<p>
The if statement allows you to check if something is True. If so, it runs
the code, if not, it continues on. It is kind of like a while loop, but it
executes <strong>only once</strong>. An if statement is written:
</p>
<p><em>if.py</em></p>
<pre><code>import random
num = random.randint(1, 10)
if num == 3:
print('num is 3. Hooray!!!')
if num > 5:
print('Num is greater than 5')
if num == 12:
print('Num is 12, which means that there is a problem with the python language, see if you can figure it out. Extra credit if you can figure it out!')</code></pre>
<p>
Now, you may think that it would be better if you could make it print only
one message. Not as many that are True. You can do that with an
<code>elif</code> statement:
</p>
<p><em>elif.py</em></p>
<pre><code>import random
num = random.randint(1, 10)
if num == 3:
print('Num is three, this is the only msg you will see.')
elif num > 2:
print('Num is not three, but is greater than 1')</code></pre>
<p>
Now, you may wonder how to run code if none work. Well, there is a simple
statement called <code>else:</code>
</p>
<p><em>else.py</em></p>
<pre><code>import random
num = random.randint(1, 10)
if num == 3:
print('Num is three, this is the only msg you will see.')
elif num > 2:
print('Num is not three, but is greater than 1')
else:
print('No category')</code></pre>
<p>
So far, you have only seen how to use functions other people have made.
Let use the example that you want to print the a random number between 1
and 9, and print different text every time.<br />
It is quite tiring to type:
</p>
<p>Characters: 389</p>
<p><em>nofunc.py</em></p>
<pre><code>import random
print(random.randint(1, 9))
print('Wow that was interesting.')
print(random.randint(1, 9))
print('Look at the number above ^')
print(random.randint(1, 9))
print('All of these have been interesting numbers.')
print(random.randint(1, 9))
print("these random.randint's are getting annoying to type")
print(random.randint(1, 9))
print('Hi')
print(random.randint(1, 9))
print('j')</code></pre>
<p>Now with functions, you can seriously lower the amount of characters:</p>
<p>Characters: 254</p>
<p><em>functions.py</em></p>
<pre><code>import random
def r(t):
print(random.randint(1, 9))
print(t)
r('Wow that was interesting.')
r('Look at the number above ^')
r('All of these have been interesting numbers.')
r("these random.randint's are getting annoying to type")
r('Hi')
r('j')</code></pre>
<p>
<a
href="https://storage.googleapis.com/replit/images/1539906570970_88961cdd6d7c76c15c92cd79307dbb06.pn"
></a>
</p>
<h2 id="chapter-01---getting-ready-with-python">
Chapter 01 - Getting Ready with Python
</h2>
<h3 id="installing-python-3-and-launching-python-shell">
Installing Python 3, And Launching Python Shell
</h3>
<p>This video should help you get up and running with Python 3</p>
<ul>
<li>
<a href="https://www.youtube.com/watch?v=Ji1WW4Suaww"
>Installing Python 3 and Launch Python Shell</a
>
</li>
</ul>
<p>
Installing Python is really a cakewalk. Search for “Python download” on
www.google.com. Download the installable and install it.
</p>
<p>A quick word of caution on Windows</p>
<ul>
<li>
Make sure that you have the check-box “Add Python 3.6 to PATH”, checked.
</li>
</ul>
<p>Once you have installed Python, you can launch the Python Shell.</p>
<ul>
<li>Windows - Launch cmd prompt by typing in ‘cmd’ command.</li>
<li>Mac or Linux - Launch up terminal.</li>
</ul>
<p>Command to launch Python 3 is different in Mac.</p>
<ul>
<li>In Mac, type in <code>python3</code></li>
<li>
In other operating systems, including windows, type <code>python</code>
</li>
</ul>
<p>You can type code in python shell and code as well!</p>
<p>You can use <code>print(5*4)</code>, and it shows <code>20</code>.</p>
<p>
You can execute the code, and the shell would immediately give you output.
</p>
<p>Using the the Python Shell is an awesome way to learn Python.</p>
<h2 id="chapter-02---introduction-to-python-programming">
Chapter 02 - Introduction To Python Programming
</h2>
<p>
Most programmers find programming a lot of fun, and besides, it also gets
their work done.
</p>
<p>
Programming mainly involves <em>problem solving</em>, where one makes use
of a computer to solve a real world problem.
</p>
<p>
During our journey here, we will approach programming in a very different
way. We will not only introduce you to the Python language, but also help
you pick up essential problem solving skills.
</p>
<p>
As a programmer, you need to be able to look at a problem, and identify
the important programming concepts relevant to solving it. Finally, you
need to be able to use the language features and syntax, to express your
solution on the computer. While all this looks complex, we want to make it
easy for you. Together, we will tackle a variety of programming
challenges, using these same steps. We will start with simple challenges
(such as a Multiplication Table), and gradually increase the difficulty
level over the duration of this book.
</p>
<p>
Learning to program is a lot like learning to ride a bicycle. The first
few steps are the most challenging ones.
</p>
<p>
Once you get over these initial steps, your experience will become more
and more enjoyable.
</p>
<p>
Are you ready for your first programming challenge? Let’s get going now!
We wish you all the best.
</p>
<h4 id="summary">Summary</h4>
<p>In this step, we:</p>
<ul>
<li>Were introduced to the concept of problem solving</li>
<li>Understood how good programmers approach problem solving</li>
</ul>
<h3 id="step-01-our-first-programming-challenge">
Step 01: Our First Programming Challenge
</h3>
<p>
Our first <em>programming challenge</em> aims to do, what every kid does
in math class: read out a multiplication table. We now want to give this
task to the computer. Here is the statement of our problem:
</p>
<h4 id="the-print-multiplication-table-challenge-pmt-challenge">
The <strong>Print Multiplication Table Challenge (PMT-Challenge)</strong>
</h4>
<ol type="1">
<li>
Compute the multiplication table for <code>5</code>, with entries from
<code>1</code> to <code>10</code>.
</li>
<li>Display this table.</li>
</ol>
<p>The display needs to be:</p>
<p>
<strong><em>5 * 1 = 5</em></strong>
</p>
<p>
<strong><em>5 * 2 = 10</em></strong>
</p>
<p>
<strong><em>5 * 3 = 15</em></strong>
</p>
<p>
<strong><em>5 * 4 = 20</em></strong>
</p>
<p>
<strong><em>5 * 5 = 25</em></strong>
</p>
<p>
<strong><em>5 * 6 = 30</em></strong>
</p>
<p>
<strong><em>5 * 7 = 35</em></strong>
</p>
<p>
<strong><em>5 * 8 = 40</em></strong>
</p>
<p>
<strong><em>5 * 9 = 45</em></strong>
</p>
<p>
<strong><em>5 * 10 = 50</em></strong>
</p>
<p>
This is the challenge. For convenience, let’s give it a label, say
<em>PMT-Challenge</em>. What would be the important concepts we need to
learn, to solve this challenge? The following list of concepts would be a
good starting point:
</p>
<ul>
<li><strong>Statements</strong></li>
<li><strong>Expressions</strong></li>
<li><strong>Variables</strong></li>
<li><strong>Literals</strong></li>
<li><strong>Conditionals</strong></li>
<li><strong>Loops</strong></li>
<li><strong>Methods</strong></li>
</ul>
<p>
In the rest of this chapter, we will introduce these concepts to you,
one-by-one. We will also show you how learning each concept, takes us
closer to a solution to <em>PMT-Challenge</em>.
</p>
<h4 id="summary-1">Summary</h4>
<p>In this step, we:</p>
<ul>
<li>Stated our first programming challenge</li>
<li>
Identified what programming concepts we need to learn, to solve this
challenge
</li>
</ul>
<h3 id="step-02-breaking-down-pmt-challenge">
Step 02: Breaking Down <em>PMT-Challenge</em>
</h3>
<p>
Typically when we do programming, we have problems. Solving the problem
typically need a step-by -step approach. Common sense tells us that to
solve a complex problem, we break it into smaller parts, and solve each
part one by one. Here is how any good programmer worth her salt, would
solve a problem:
</p>
<ul>
<li>Simplify the problem, by breaking it into sub-problems</li>
<li>
Solve the sub-problems in stages (in some order), using the language
</li>
<li>Combine these solutions to get a final solution</li>
</ul>
<p>
The <em>PMT-Challenge</em> is no different! Now how do we break it down,
and where do we really start? Once again, your common sense will reveal a
solution. As a first step, we could get the computer to calculate say,
<code>5 * 3</code>. The second thing we can do, is to try and print the
calculated value, in a manner similar to <code>5 * 3 = 15</code>. Then, we
could repeat what we just did, to print out all the entries of the
<code>5</code> multiplication table. Let’s put it down a little more
formally:
</p>
<p>Here is how our draft steps look like</p>
<ul>
<li>Calculate <code>5 * 3</code> and print result as <code>15</code></li>
<li>
Print <code>5 * 3 = 15</code> (<code>15</code> is result of previous
calculation)
</li>
<li>
Do this ten times, once for each table entry (going from
<code>1</code> to <code>10</code>)
</li>
</ul>
<p>Let’s start with that kind of a game plan, and see where it takes us.</p>
<h4 id="summary-2">Summary</h4>
<p>In this step, we:</p>
<ul>
<li>
Learned that breaking down a problem into sub-problems is a great help
</li>
<li>Found a way to break down the <em>PMT-Challenge</em> problem</li>
</ul>
<h3 id="step-03-introducing-operators-and-expressions">
Step 03: Introducing Operators And Expressions
</h3>
<p>
Let’s focus on solving the first sub-problem of <em>PMT-Challenge</em>,
the numeric computation. We want the computer to calculate
<code>5 * 5</code> for example, and print <code>25</code> for us. How do
we get it to do that? That’s what we would be looking at in this step.
</p>
<h4 id="snippet-01-introducing-operators">
Snippet-01: Introducing Operators
</h4>
<p>
Launch up Python shell. We want to calculate <code>5 * 5</code>. How do we
do that?
</p>
<p>Using our knowledge of school math, let’s try <code>5 X 5</code>.</p>
<pre><code> >>> 5 X 5
File "< stdin >", line 1
5 X 5
^
SyntaxError: invalid syntax</code></pre>
<p>
The Python Shell hits back at us, saying “<em>invalid syntax</em>”. This
is how Python complains, when it doesn’t fully understand the code you
type in. Here, it says our code has a “<strong>SyntaxError</strong>”.
</p>
<p>
The reason why it complains, is because ‘<code>X</code>’ is not a valid
<strong>operator</strong> in Python.
</p>
<p>
The way you can do multiplication is by using the ‘<code>*</code>’
<em>operator</em> .
</p>
<p>
“<em>5 into 5</em>” is achieved by the code <code>5 * 5</code>, and you
can see the result <code>25</code> being printed. Similarly,
<code>5 * 6</code> gives us <code>30</code>.
</p>
<pre><code> >>> 5 * 6
30</code></pre>
<p>There are a wide range of other operators in Python:</p>
<ul>
<li><code>5 + 6</code> gives a result of <code>11</code>.</li>
<li>
<p><code>5 - 6</code> leads to <code>-1</code>.</p>
<pre><code>>>> 5 + 6
11
>>> 5 - 6
-1</code></pre>
</li>
</ul>
<p>
<code>10 / 2</code>, gives an output of <code>5.0</code> . There is one
interesting operator, <code>**</code>. Let’s try <code>10 ** 3</code>. We
ran this code, and the result we get is <code>1000</code>. Yes you guessed
right, the operator performs “to the power of”. “<code>10</code> to the
power of <code>3</code>” is <code>10 * 10 * 10</code>, or
<code>1000</code>.
</p>
<pre><code> >>> 10 / 2
5.0
>>> 10 ** 3
1000</code></pre>
<p>
Another interesting operator is <code>%</code>, called “<em>modulo</em>”,
which computes the remainder on integer division. If we do
<code>10 % 3</code>, what is the remainder when <code>10</code> is divided
by <code>3</code>? <code>3 * 3</code> is <code>9</code>, and
<code>10 - 9</code> is <code>1</code>, which is what
<code>%</code> returns in this case.
</p>
<p>Let’s look at some terminology:</p>
<ul>
<li>
Whatever pieces of code we gave Python shell to run, are called
<strong>expressions</strong>. So, <code>5 * 5</code>,
<code>5 * 6</code> and <code>5 - 6</code> are all <em>expressions</em>.
An expression is composed of <em>operators</em> and
<strong>operands</strong>.
</li>