01-PythonIntroductionFin.ipynb 31.1 KB
Newer Older
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introducing Python\n",
    "\n",
    "Python is an interpreted, high-level, object-oriented, dynamically-typed, scripting language. \n",
    "What does each of these terms mean?\n",
    "\n",
    "An interpreted language is one where the instructions are compiled into machine code beforehand, \n",
    "and then are executed at run-time. This compares with a compiled language such as C or Fortran\n",
    "where the instructions are compiled into machine language and then run.\n",
    "\n",
    "A high-level language is one which is written in code which can be easily transferred from machine\n",
    "to machine. This is in contrast to low-level languages which deals with specific details of the\n",
    "computer architecture and must be optimized for each machine.\n",
    "\n",
    "An object-oriented language is one where the objects, e.g., variables, are the focus of the \n",
    "language. For example, objects can then have specific functions associated with them.\n",
    "\n",
    "A dynamically-typed language is one where the form of a variable is defined as you assign it, rather \n",
    "than having specific rules for variables or having to define the type of each variable at the \n",
    "beginning of your program.\n",
    "\n",
    "A scripting language is one where instructions can be typed and interpreted without having to write\n",
    "formal programs.\n",
    "\n",
    "### Why Python?\n",
    "\n",
    "* Python is the most popular language due to the fact that it’s easier to code and understand.\n",
    "* Python is an object-oriented programming language and can be used to write functional code too.\n",
    "* It is a suitable language that bridges the gaps between business and developers.\n",
    "* It takes less time to bring a Python program to market compared to other languages such as C#/Java.\n",
    "* There are a vast number of Python machine learning and analytical packages.\n",
    "* A large number of communities and books are available to support Python developers.\n",
    "* Nearly all types of applications, can be implemented in Python.\n",
    "* There is no need to declare variable types. Thus it is quicker to implement a Python application.\n",
    "\n",
    "### Why Not Python?\n",
    "\n",
    "* Python is slower than C++, C#, Java. This is due to the lack of Just In Time optimisers in Python.\n",
    "* Python syntactical white-space constraint makes it slightly difficult to implement for new coders.\n",
    "* Python does not offer advanced statistical features as R does.\n",
    "* Python is not suitable for low-level systems and hardware interaction."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables\n",
    "\n",
    "* Variables store information that can be used and/or changed in your program or script. This information can be an integer, text, collection, etc.\n",
    "* Variables are used to hold user inputs, local states of your program, etc.\n",
    "* Variables have a name so that they can be referenced in the code.\n",
    "* The fundamental concept to understand is that everything is an object in Python.\n",
    "\n",
    "__The standard data types Python supports are numbers, strings, sets, lists, tuples, and dictionaries. We will explain some of these in detail in this lesson.__\n",
    "\n",
    "### Declare and Assign Value to a Variable\n",
    "\n",
    "Assignment sets a value to a variable.\n",
    "\n",
    "To assign variable a value, use the equals sign (=)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello You 2\n"
     ]
    }
   ],
   "source": [
    "myFirstVariable = 1 # defines an integer variable\n",
    "mySecondVariable = 2\n",
    "myFirstVariable = \"Hello You\" # defines a string\n",
    "print(myFirstVariable,mySecondVariable) # print arguments to standard output or screen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that comments can be included in code by inserting a #. Everything after this on the line is ignored.\n",
    "\n",
    "Assigning a value is known as __binding__ in Python. In the example above, we have assigned the value of 2 to mySecondVariable.\n",
    "\n",
    "Note how we assigned an integer value of 1 and then a string value of “Hello You” to the same myFirstVariable variable. This is possible due to the fact that the data types are dynamically typed in python. This is why Python is known as a dynamically typed programming language.\n",
    "\n",
    "If you want to assign the same value to more than one variables then you can use the chained assignment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "myFirstVariable = mySecondVariable = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numeric\n",
    "\n",
    "Integers and floats (floating point numbers) are supported."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An example of an integer is 1\n",
      "An example of a float is 1.2\n"
     ]
    }
   ],
   "source": [
    "value1 = 1 # integer\n",
    "value2 = 1.2 # float with a floating point\n",
    "print('An example of an integer is',value1)\n",
    "print('An example of a float is',value2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Strings\n",
    "\n",
    "* Textual information. Not limited to letters.\n",
    "* A string is an list of characters.\n",
    "* A string value is enclosed in quotation marks: single, double or triple quotes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'jonathon'\n",
    "name = \"jonathon\"\n",
    "name = \"\"\"jonathon\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Strings are immutable. Once they are created, they cannot be changed, e.g., if we try to change the second character of the string (array notation will be explained later), we will get an error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-2f3bea8f29c6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'me'\u001b[0m \u001b[0;31m# Updating it will fail:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'y'\u001b[0m \u001b[0;31m# Will throw a Type Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "a = 'me' # Updating it will fail: \n",
    "a[1] = 'y' # Will throw a Type Error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* When string variables are assigned a new value then Python creates a new object to store the value. Therefore a reference (pointer) to an object is created. This pointer is then assigned to the variable and as a result, the variable can be used.\n",
    "\n",
    "* We can also assign one variable to another variable. All it does is that a new pointer is created which points to the same object:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "beta alpha\n"
     ]
    }
   ],
   "source": [
    "a = 'alpha' \n",
    "b = a \n",
    "a = 'beta'\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Finding Variable Type\n",
    "\n",
    "If you want to find type of a variable, you can use the`type` function:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type('jonathon') # Returns <type 'str'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Operations\n",
    "\n",
    "* These allow us to perform computation on variables\n",
    "\n",
    "### Numeric Operations\n",
    "\n",
    "* Python supports the standard algebraic operations *, /, +, -\n",
    "* Python also supports floor division // (division rounded __down__ to the next integer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0.3333333333333333\n"
     ]
    }
   ],
   "source": [
    "print(1//3)  #returns 0\n",
    "print(1/3) #returns 0.333 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: the return type of division is always float as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a = 10/5\n",
    "print(type(a)) # prints float"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also, Python supports exponentiation via `**` operator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "print(2**3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and the Modulus (remainder) operator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(7%2) # (7/2-7//2)*2 = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The common operators also support augmented expressions. For example, `a+1` can be written as"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "a = 2\n",
    "a += 1 # shorthand for a = a+1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Methods\n",
    "\n",
    "We have already seen examples of functions such as `print()` and `type()`.\n",
    "\n",
    "Each class or type of variable has a set of functions that are built in to their implementation. These are called *methods* and are invoked by first calling the variable and then following that with a period and then the function name. \n",
    "\n",
    "For example, there are number of methods for changing the case of strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n",
      "HELLO WORLD!\n",
      "hEllO wORLd!\n"
     ]
    }
   ],
   "source": [
    "myString = \"HeLLo WorlD!\"\n",
    "print(myString.lower()) # convert string to lower case\n",
    "print(myString.upper()) # convert string to upper case\n",
    "print(myString.swapcase()) # swap the case of all the letters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### String Operations\n",
    "\n",
    "Some other string operations include, concatenating strings, i.e., adding them together to create a new string:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AB\n"
     ]
    }
   ],
   "source": [
    "print('A' + 'B')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember a string is an immutable data type, therefore, concatenating strings creates a new string object.\n",
    "\n",
    "Repeating strings, e.g., ‘A’*3 will repeat A three times:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AAA\n"
     ]
    }
   ],
   "source": [
    "print('A'*3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Slicing:\n",
    "\n",
    "Each element in a string or list gets two indexes:\n",
    "* From left to right, the index starts at 0 and increments by 1\n",
    "* From right to left, the index starts at -1 and decrements by 1\n",
    "* Therefore,  `y[0]` and `y[-len(y)]` both will return the same value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "y = 'abc'\n",
    "print(y[0])\n",
    "print(y[-len(y)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can access a substring of a string (or a list) using:\n",
    "\n",
    "`string[start:end:step]`\n",
    "\n",
    "This returns the characters in the string starting at `start` and up to, but not including, `end`, in steps of `step`. Each of the arguments, and the last colon, are optional. The default values are `start=0`, `end=len(string)` and `step=1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ac\n",
      "bc\n",
      "Ab\n"
     ]
    }
   ],
   "source": [
    "y = 'Abc'\n",
    "print(y[0::2]) # print every second character up to the end\n",
    "print(y[1:]) # print from the second character to the end\n",
    "print(y[:2]) # print up to the second character"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Reversing\n",
    "\n",
    "Using this slicing with negative steps we can reverse a string:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cba\n"
     ]
    }
   ],
   "source": [
    "x = 'abc'\n",
    "print(x[::-1]) # since the step is negative, start at the end and increment to the start"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Negative Index\n",
    "\n",
    "If you want to start from the last character then use negative index."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cb\n"
     ]
    }
   ],
   "source": [
    "y = 'abc'\n",
    "print(y[-1:0:-1]) # will return cb, same as print(y[-1:-3:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Finding Index\n",
    "\n",
    "To find the occurence of a character in a string we can use the find method. The second and third arguments, which are optional, tell where to start and end the search. \n",
    "\n",
    "Remember indices start at 0, and this just tells you the first occurence of the character. For finding multiple characters, it tells where the character starts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "9\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "name = \"rumpelstiltskin\"\n",
    "print(name.find('t')) \n",
    "print(name.find('l',7,10)) # finds index of l, starting at name[7] and ending at name[10]\n",
    "print(name.find('skin')) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "#### Casting\n",
    "\n",
    "To __cast__ (convert between types cast) we can use the functions:\n",
    "\n",
    "* `str(x)`: convert to string\n",
    "* `int(x)`: convert to integer\n",
    "* `float(x)`: convert to floats\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comments and Line Continuation\n",
    "\n",
    "Rule number of one of coding or programming is that your code should always be commented to explain what it does.\n",
    "\n",
    "### Single Line Comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is a single line comment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Multiple Line Comments\n",
    "\n",
    "For long comments we can use triple quotes. In the output \\n represents the carriage return character."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'this is a multi\\nline\\ncomment'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''this is a multi\n",
    "line\n",
    "comment'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Line Continuation\n",
    "\n",
    "Long expressions can be continued over multiple lines by enclosing them in parentheses:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-197.0\n"
     ]
    }
   ],
   "source": [
    "a = (25*10+6/2\n",
    "     -450)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or using a backslash: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-197.0\n"
     ]
    }
   ],
   "source": [
    "# no characters can appear after the backslash\n",
    "a = 25*10+6/2 \\\n",
    "    -450\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Boolean Expressions\n",
    "\n",
    "Expressions can perform Boolean operations such as:\n",
    "* Equality: `==`\n",
    "* Not Equal: `!=`\n",
    "* Greater Than: `>`\n",
    "* Less Than: `<`\n",
    "* Greater Than Or Equal: `>=`\n",
    "* Less Than Or Equal: `<=`\n",
    "\n",
    "We can compare numbers and assign Boolean variables (True or False):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "c =  False\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = 2\n",
    "print(a==b) # test whether a is equal to b\n",
    "print(a<=b) # test whether a is less than or equal to b\n",
    "c = a>b     # c is a Boolean variable\n",
    "print('c = ',c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conditions\n",
    "\n",
    "To write a process based on tests of the values of variables, we can use the if then else construct. The construct is created by including a colon at the conclusion of each test and indenting the statements to be performed by four characters immediately below the test. Once you enter the colon, Jupyter will automatically indent the following lines until the user chooses otherwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is greater than b\n",
      "5 4\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "b = 4\n",
    "if a == b:\n",
    "    print('a is b')\n",
    "elif a < b:\n",
    "    print('a is less than b')\n",
    "elif a > b:\n",
    "    print('a is greater than b') \n",
    "    print(a,b)\n",
    "else:\n",
    "    print('a is different') \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also add conditional logic in each part of the if then else construct. This is known as a nested condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "within else of else\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "b = 4\n",
    "if a == b:\n",
    "    print('a is b')\n",
    "#let's write conditions within else\n",
    "else:\n",
    "    if a == 2:\n",
    "        print('within if of else')\n",
    "    else:\n",
    "        print('within else of else')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loops\n",
    "\n",
    "To repeat a process we can use while or for loops. Again these use indentation and colons to define the loop process.\n",
    "\n",
    "### While\n",
    "\n",
    "Here we provide a condition and run the loop until the condition is met:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "input=2\n",
    "while (input > 0): # test whether the input is negative\n",
    "    print(input)\n",
    "    input = input-1 # update the variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### For\n",
    "\n",
    "To loop over some statements a number of times, we can use for loops. The object `[0,1,2,3,4]` is an array or list and what we are telling Python to do is let `i` be each element in that array, and stop when we get to the end of the array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in [0,1,2,3,4]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can use the range function:\n",
    "\n",
    "`range(start, stop, step):`\n",
    "\n",
    "This generates numerical values that commence at `start` (inclusive), finish at `stop` (exclusive) with the steps `step`. For example, to generate and view\n",
    "odd numbers from 1 to 9, do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,10,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can loop over items or characters of a string, since strings are just arrays of characters. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "for letter in 'hello':\n",
    "    print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Combine For with If\n",
    "\n",
    "Let’s do a simple exercise to find if a character is in two words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "o\n",
      "n\n",
      "e\n",
      "n\n",
      "a\n",
      "m\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "name = 'onename'\n",
    "anothername = 'onenameonename'\n",
    "for character in name: # loop through each character in the string name\n",
    "    if character in anothername: # check if the character is in the string anothername\n",
    "        print(character) # print the character if true"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Break\n",
    "\n",
    "If you want to end a for or while loop we can use the break construct."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,10):\n",
    "    print(i)\n",
    "    if (i==5):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "while True: # do this while loop regardless, since it is always true\n",
    "    x -= 1 # decrease x by one\n",
    "    print(x)\n",
    "    if (x==1): # stop when x = 1\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercises\n",
    "\n",
    "Please enter your solution in the cell below each exercise."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1\n",
    "\n",
    "For the numbers 13 and 22 write a script which tests whether the number is even or odd, and then prints out\n",
    "a statement such as:\n",
    "\n",
    "The number 13 is odd. (The print statement should use a variable, rather than an actual number.)\n",
    "\n",
    "Your solution should involve a for loop, an array, the % operator and an if-else statement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The number  13 is odd\n",
      "The number  22 is even\n"
     ]
    }
   ],
   "source": [
    "for number in [13,22]:\n",
    "    if (number%2 == 0):\n",
    "        print('The number ',number,'is even')\n",
    "    else:\n",
    "        print('The number ',number,'is odd')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2\n",
    "\n",
    "Use a for loop to calculate the factorial of 9 and print out the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9! =  362880\n"
     ]
    }
   ],
   "source": [
    "fact = 1\n",
    "for i in range(2,10):\n",
    "    fact = fact*i\n",
    "print('9! = ',fact)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3\n",
    "\n",
    "For the string 'Supercalifragilisticexpialidocious', print the string in reverse order, print strings consisting of the characters in the odd and even positions, and find the number of occurences of the vowels in the string (you should loop over the vowels and use the `count()` method). Your answers should be explained and formatted using print statements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "String backwards is suoicodilaipxecitsiligarfilacrepuS\n",
      "Characters in odd positions are Sprairglsiepaioiu\n",
      "Characters in even positions are ueclfaiitcxildcos\n",
      "a occurs 3 times in the string\n",
      "e occurs 2 times in the string\n",
      "i occurs 7 times in the string\n",
      "o occurs 2 times in the string\n",
      "u occurs 2 times in the string\n"
     ]
    }
   ],
   "source": [
    "string = 'Supercalifragilisticexpialidocious'\n",
    "print('String backwards is',string[::-1])\n",
    "print('Characters in odd positions are',string[0::2])\n",
    "print('Characters in even positions are',string[1::2])\n",
    "for vowel in 'aeiou': # loop through each vowel\n",
    "    print(vowel,'occurs',string.count(vowel),'times in the string')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4\n",
    "\n",
    "Use a for loop to print the 10 by 10 multiplication table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 6 7 8 9 10 \n",
      "\n",
      "2 4 6 8 10 12 14 16 18 20 \n",
      "\n",
      "3 6 9 12 15 18 21 24 27 30 \n",
      "\n",
      "4 8 12 16 20 24 28 32 36 40 \n",
      "\n",
      "5 10 15 20 25 30 35 40 45 50 \n",
      "\n",
      "6 12 18 24 30 36 42 48 54 60 \n",
      "\n",
      "7 14 21 28 35 42 49 56 63 70 \n",
      "\n",
      "8 16 24 32 40 48 56 64 72 80 \n",
      "\n",
      "9 18 27 36 45 54 63 72 81 90 \n",
      "\n",
      "10 20 30 40 50 60 70 80 90 100 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# this may be a little difficult, as I didn't tell them to add end=\" \"\n",
    "a = range(1,11)\n",
    "for b in a:\n",
    "    for c in a:\n",
    "        print(b*c, end=\" \")\n",
    "    print('\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5\n",
    "\n",
    "Given an integer 260221 reverse the order and print the output, i.e., 122062."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "122062\n"
     ]
    }
   ],
   "source": [
    "number = 260221\n",
    "string = str(number)\n",
    "print(int(string[::-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}