-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorials-python.html
1342 lines (1070 loc) · 44.2 KB
/
tutorials-python.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 lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>PrograMix</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<style>
body {
padding-top: 70px;
/* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
font-size: 15px;
font-style: verdana;
}
li{
color: #33AAFF;
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><img src="images/logo-trans-134px.png" style="margin-top: -10px"></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<!-- languages nav and dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Programming Languages<span class="caret"></span></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="programming-language-java.html">Java</a></li>
<li><a class="dropdown-item" href="programming-language-python.html">Python</a></li>
<li><a class="dropdown-item" href="programming-language-haskell.html">Haskell</a></li>
<li><a class="dropdown-item" href="programming-language-php.html">PHP</a></li>
<li><a class="dropdown-item" href="programming-language-c++.html">C++</a></li>
<li><a class="dropdown-item" href="programming-language-c_sharp.html">C#</a></li>
<li><a class="dropdown-item" href="programming-language-ios.html">IOS - Objective-C</a></li>
<li><a class="dropdown-item" href="programming-language-SQL.html">SQL</a></li>
<li><a class="dropdown-item" href="programming-language-Perl.html">Perl</a></li>
<li><a class="dropdown-item" href="programming-language-JavaScript.html">JavaScript</a></li>
</ul>
</div>
</li><!--end of languages dropdown-->
<!--tutorials in nav and dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Tutorials<span class="caret"></span></a>
<!-- Tutorials dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="tutorials-java.html">Java</a></li>
<li><a class="dropdown-item" href="tutorials-python.html">Python</a></li>
<li><a class="dropdown-item" href="tutorials-haskell.html">Haskell</a></li>
<li><a class="dropdown-item" href="tutorials-php.html">PHP</a></li>
<li><a class="dropdown-item" href="tutorials-c++.html">C++</a></li>
<li><a class="dropdown-item" href="tutorials-c_shar.html">C#</a></li>
<li><a class="dropdown-item" href="tutorials-ios.html">IOS - Objective-C</a></li>
<li><a class="dropdown-item" href="tutorials-sql.html">SQL</a></li>
<li><a class="dropdown-item" href="tutorials-perl.html">Perl</a></li>
<li><a class="dropdown-item" href="tutorials-js.html">JavaScript</a></li>
</ul>
</div>
</li><!-- end of tutorials -->
<!--programming software in navbar with dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Programming Softwares<span class="caret"></span></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="prosoft-nb.html">NetBeans</a></li>
<li><a class="dropdown-item" href="prosoft-jc.html">JCreator</a></li>
<li><a class="dropdown-item" href="prosoft-st.html">Sublime Text</a></li>
<li><a class="dropdown-item" href="prosoft-eclipse.html">Eclipse</a></li>
<li><a class="dropdown-item" href="prosoft-xcode.html">Xcode</a></li>
</ul>
</div>
</li><!-- end of programming softwares -->
<!-- search bar icon w/ dropdown searchbox-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false"><img src="images/search-icon.png"></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<form method="get" action="/search" id="search">
<input name="q" type="text" size="40" placeholder="Search..." />
</form>
</div>
</li><!-- end of search bar-->
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Content w/ container-->
<div class="container">
<div class="row">
<div class="col-lg-12 text-left">
<div class="jumbotron jumbotron-fluid">
<div class="container" style="text-align: center">
<h1 class="display-3">Python</h1>
<p class="lead"> </p>
</div>
</div>
<h1>Python Keywords and Identifier</h1>
<h2>Python Keywords</h2>
<p style="text-indent: 40px">Keywords are the reserved words in Python.
<p style="text-indent: 40px">We cannot use a keyword as variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.
<p style="text-indent: 40px">In Python, keywords are case sensitive.
<p style="text-indent: 40px">There are 33 keywords in Python 3.3. This number can vary slightly in course of time.
<p style="text-indent: 40px">All the keywords except <i>True</i>, <i>False</i> and <i>None</i> are in lowercase and they must be written as it is. The list of all the keywords are given below.
<b>Keywords in Python programming language</b>
<table class="table">
<tbody>
<tr>
<td>False</td>
<td>class</td>
<td>finally</td>
<td>is</td>
<td>return</td>
</tr>
<tr>
<td>None</td>
<td>continue</td>
<td>for</td>
<td>lambda</td>
<td>try</td>
</tr>
<tr>
<td>True</td>
<td>def</td>
<td>from</td>
<td>nonlocal</td>
<td>while</td>
</tr>
<tr>
<td>and</td>
<td>del</td>
<td>global</td>
<td>not</td>
<td>with</td>
</tr>
<tr>
<td>as</td>
<td>elif</td>
<td>if</td>
<td>or</td>
<td>yield</td>
</tr>
<tr>
<td>assert</td>
<td>else</td>
<td>import</td>
<td>pass</td>
</tr>
<tr>
<td>break</td>
<td>except</td>
<td>in</td>
<td>raise</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">Looking at all the keywords at once and trying to figure out what they mean might be overwhelming.</p>
<p style="text-indent: 40px">If you want to have an overview, here is the complete list of all the keywords with examples.</p>
<h2>Python Identifiers</h2>
<p style="text-indent: 40px">Identifier is the name given to entities like class, functions, variables etc. in Python. It helps differentiating one entity from another.</p>
<h2>Rules for writing identifiers</h2>
<ol>
<li style="color: black">Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). Names like <i>myClass</i>, <i>var_1</i> and <i>print_this_to_screen</i>, all are valid example.</li></li>
<br>
<li style="color: black">An identifier cannot start with a digit. <i>1variable</i> is invalid, but <i>variable1</i> is perfectly fine.</li></li>
<li style="color: black">Keywords cannot be used as identifiers.</li></li>
<div class="alert alert-info" role="alert">
>>> global = 1<br>
File "<interactive input>", line 1<br>
global = 1<br>
^<br>
SyntaxError: invalid syntax<br>
</div>
<li style="color: black"> We cannot use special symbols like !, @, #, $, % etc. in our identifier.</li></li>
<div class="alert alert-info" role="alert">
>>> a@ = 0<br>
File "<interactive input>", line 1<br>
a@ = 0<br>
^<br>
SyntaxError: invalid syntax<br>
</div>
<li style="color: black"> Identifier can be of any length.</li></li>
</ol>
<h2>Things to care about</h2>
<p style="text-indent: 40px">Python is a case-sensitive language. This means, <i>Variable</i> and <i>variable</i> are not the same. Always name identifiers that make sense.</p>
<p style="text-indent: 40px">While, <i>c = 10</i> is valid. Writing <i>count = 10</i> would make more sense and it would be easier to figure out what it does even when you look at your code after a long gap.</p>
<p style="text-indent: 40px">Multiple words can be separated using an underscore, this_is_a_long_variable.</p>
<p style="text-indent: 40px">We can also use camel-case style of writing, i.e., capitalize every first letter of the word except the initial word without any spaces. For example: <i>camelCaseExample</i></p>
<h1>Python Statement, Indentation and Comments</h1>
<h2>Python Statement</h2>
<p style="text-indent: 40px">Instructions that a Python interpreter can execute are called statements. For example, <i>a = 1</i> is an assignment statement. <i>if</i> statement, <i>for</i> statement, <i>while</i> statement etc. are other kinds of statements which will be discussed later.</p>
<h2>Multi-line statement</h2>
<p style="text-indent: 40px">In Python, end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\). For example:</p>
<div class="alert alert-info" role="alert">
a = 1 + 2 + 3 + \<br>
4 + 5 + 6 + \<br>
7 + 8 + 9<br>
</div>
<p style="text-indent: 40px">This is explicit line continuation. In Python, line continuation is implied inside parentheses ( ), brackets [ ] and braces { }. For instance, we can implement the above multi-line statement as</p>
<div class="alert alert-info" role="alert">
a = (1 + 2 + 3 +<br>
4 + 5 + 6 +<br>
7 + 8 + 9)<br>
</div>
<p style="text-indent: 40px">Here, the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [ ] and { }. For example:</p>
<div class="alert alert-info" role="alert">
colors = ['red',<br>
'blue',<br>
'green']<br>
</div>
<p style="text-indent: 40px">We could also put multiple statements in a single line using semicolons, as follows</p>
<div class="alert alert-info" role="alert">
a = 1; b = 2; c = 3
</div>
<h2>Python Indentation</h2>
<p style="text-indent: 40px">Most of the programming languages like C, C++, Java use braces { } to define a block of code. Python uses indentation.</p>
<p style="text-indent: 40px">A code block (body of a function, loop etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be consistent throughout that block.</p>
<p style="text-indent: 40px">Generally four whitespaces are used for indentation and is preferred over tabs. Here is an example.</p>
<div class="alert alert-info" role="alert">
for i in range(1,11):<br>
print(i)<br>
if i == 5:<br>
break<br>
</div>
<p style="text-indent: 40px">The enforcement of indentation in Python makes the code look neat and clean. This results into Python programs that look similar and consistent.</p>
<p style="text-indent: 40px">Indentation can be ignored <br>in line continuation. But it's a good idea to always indent. It makes the code more readable. For example:</p>
<div class="alert alert-info" role="alert">
if True:<br>
print('Hello')<br>
a = 5<br>
</div>
<p style="text-indent: 40px">and</p>
<div class="alert alert-info" role="alert">
if True: print('Hello'); a = 5
</div>
<p style="text-indent: 40px">both are valid and do the same thing. But the former style is clearer.</p>
<p style="text-indent: 40px">Incorrect indentation will result into <i>IndentationError</i>.</p>
<h2>Python Comments</h2>
<p style="text-indent: 40px">Comments are very important while writing a program. It describes what's going on inside a program so that a person looking at the source code does not have a hard time figuring it out. You might forget the key details of the program you just wrote in a month's time. So taking time to explain these concepts in form of comments is always fruitful.</p>
<p style="text-indent: 40px">In Python, we use the hash (#) symbol to start writing a comment.</p>
<p style="text-indent: 40px">It extends up to the newline character. Comments are for programmers for better understanding of a program. Python Interpreter ignores comment.</p>
<div class="alert alert-info" role="alert">
#This is a comment<br>
#print out Hello<br>
print('Hello')<br>
</div>
<h2>Multi-line comments</h2>
<p style="text-indent: 40px">If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning of each line. For example:</p>
<div class="alert alert-info" role="alert">
#This is a long comment<br>
#and it extends<br>
#to multiple lines<br>
Another way of doing this is to use triple quotes, either <i>'''</i> or <i>"""</i>.
</div>
<p style="text-indent: 40px">These triple quotes are generally used for multi-line strings. But they can be used as multi-line comment as well. Unless they are not docstrings, they do not generate any extra code.</p>
<div class="alert alert-info" role="alert">
"""This is also a<br>
perfect example of<br>
multi-line comments"""
</div>
<h2>Docstring in Python</h2>
<p style="text-indent: 40px">Docstring is short for documentation string.
<p style="text-indent: 40px">It is a string that occurs as the first statement in a module, function, class, or method definition. We must write what a function/class does in the docstring.</p>
<p style="text-indent: 40px">Triple quotes are used while writing docstrings. For example:</p>
<div class="alert alert-info" role="alert">
def double(num):<br>
"""Function to double the value"""<br>
return 2*num
</div>
<p style="text-indent: 40px"> Docstring is available to us as the attribute <i>__doc__</i> of the function. Issue the following code in shell once you run the above program.</p>
<div class="alert alert-info" role="alert">
>>> print(double.__doc__)<br>
Function to double the value
</div>
<h1>Python Variables and Data Types</h1>
<h2>Python Variables</h2>
<p style="text-indent: 40px">A variable is a location in memory used to store some data (value).</p>
<p style="text-indent: 40px">They are given unique names to differentiate between different memory locations. The rules for writing a variable name is same as the rules for writing identifiers in Python.</p>
<p style="text-indent: 40px">We don't need to declare a variable before using it. In Python, we simply assign a value to a variable and it will exist. We don't even have to declare the type of the variable. This is handled internally according to the type of value we assign to the variable.</p>
<h2>Variable assignment</h2>
<p style="text-indent: 40px">We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned to any valid variable.</p>
<div class="alert alert-info" role="alert">
a = 5<br>
b = 3.2<br>
c = "Hello"
</div>
<p style="text-indent: 40px">Here, we have three assignment statements. <i>5</i> is an integer assigned to the variable <i>a</i>.</p>
<p style="text-indent: 40px">Similarly, <i>3.2</i> is a floating point number and <i>"Hello"</i> is a string (sequence of characters) assigned to the variables <i>b</i> and <i>c</i> respectively.</p>
<h2>Multiple assignments</h2>
<p style="text-indent: 40px">In Python, multiple assignments can be made in a single statement as follows:</p>
<div class="alert alert-info" role="alert">
a, b, c = 5, 3.2, "Hello"
</div>
<p style="text-indent: 40px">If we want to assign the same value to multiple variables at once, we can do this as</p>
<div class="alert alert-info" role="alert">
x = y = z = "same"
</div>
<p style="text-indent: 40px">This assigns the "same" string to all the three variables.</p>
<h1>Data types in Python</h1>
<p style="text-indent: 40px">Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.</p>
<p style="text-indent: 40px">There are various data types in Python. Some of the important types are listed below.</p>
<h2>Python Numbers</h2>
<p style="text-indent: 40px">Integers, floating point numbers and complex numbers falls under Python numbers category. They are defined as <i>int</i>, <i>float</i> and <i>complex</i> class in Python.</p>
<p style="text-indent: 40px">We can use the <i>type()</i> function to know which class a variable or a value belongs to and the <i>isinstance()</i> function to check if an object belongs to a particular class.</p>
<div class="alert alert-info" role="alert">
a = 5<br>
print(a, "is of type", type(a))<br>
<br>
a = 2.0<br>
print(a, "is of type", type(a))<br>
<br>
a = 1+2j<br>
print(a, "is complex number?", isinstance(1+2j,complex))
</div>
<p style="text-indent: 40px">Integers can be of any length, it is only limited by the memory available.</p>
<p style="text-indent: 40px">A floating point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. <i>1</i> is integer, <i>1.0</i> is floating point number.</p>
<p style="text-indent: 40px">Complex numbers are written in the form, <i>x + yj</i>, where <i>x </i>is the real part and <i>y</i> is the imaginary part. <br>Here are some examples.</p>
<div class="alert alert-info" role="alert">
>>> a = 1234567890123456789<br>
>>> a<br>
1234567890123456789<br>
>>> b = 0.1234567890123456789<br>
>>> b<br>
0.12345678901234568<br>
>>> c = 1+2j<br>
>>> c<br>
(1+2j)
</div>
<p style="text-indent: 40px">Notice that the <i>float</i> variable b got truncated.</p>
<h2>Python List</h2>
<p style="text-indent: 40px">List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.</p>
<p style="text-indent: 40px">Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].</p>
<div class="alert alert-info" role="alert">
>>> a = [1, 2.2, 'python']
</div>
<p style="text-indent: 40px">We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.</p>
<div class="alert alert-info" role="alert">
a = [5,10,15,20,25,30,35,40]<br>
<br>
# a[2] = 15<br>
print("a[2] = ", a[2])<br>
<br>
# a[0:3] = [5, 10, 15]<br>
print("a[0:3] = ", a[0:3])<br>
<br>
# a[5:] = [30, 35, 40]<br>
print("a[5:] = ", a[5:])
</div>
<p style="text-indent: 40px">Lists are mutable, meaning, value of elements of a list can be altered.</p>
<div class="alert alert-info" role="alert">
>>> a = [1,2,3]<br>
>>> a[2]=4<br>
>>> a<br>
[1, 2, 4]<br>
</div>
<h2>Python Tuple</h2>
<p style="text-indent: 40px">Tuple is an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once created cannot be modified.</p>
<p style="text-indent: 40px">Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.</p>
<p style="text-indent: 40px">It is defined within parentheses () where items are separated by commas.</p>
<div class="alert alert-info" role="alert">
>>> t = (5,'program', 1+3j)
</div>
<p style="text-indent: 40px">We can use the slicing operator [] to extract items but we cannot change its value.</p>
<div class="alert alert-info" role="alert">
t = (5,'program', 1+3j)<br>
<br>
# t[1] = 'program'<br>
print("t[1] = ", t[1])<br>
<br>
# t[0:3] = (5, 'program', (1+3j))<br>
print("t[0:3] = ", t[0:3])<br>
<br>
# Generates error<br>
# Tuples are immutable<br>
t[0] = 10
</div>
<h1>Python Strings</h1>
<p style="text-indent: 40px">String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, <i>'''</i> or <i>"""</i>.</p>
<div class="alert alert-info" role="alert">
>>> s = "This is a string"
>>> s = '''a multiline
</div>
<p style="text-indent: 40px">Like list and tuple, slicing operator [ ] can be used with string. Strings are immutable.</p>
<div class="alert alert-info" role="alert">
s = 'Hello world!'<br>
<br>
# s[4] = 'o'<br>
print("s[4] = ", s[4])<br>
<br>
# s[6:11] = 'world'<br>
print("s[6:11] = ", s[6:11])<br>
<br>
# Generates error<br>
# Strings are immutable in Python<br>
s[5] ='d'
</div>
<h2>Python Set</h2>
<p style="text-indent: 40px">Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.</p>
<div class="alert alert-info" role="alert">
a = {5,2,3,1,4}<br>
<br>
# printing set variable<br>
print("a = ", a)<br>
<br>
# data type of variable a<br>
print(type(a))<br>
</div>
<p style="text-indent: 40px">We can perform set operations like union, intersection on two sets. Set have unique values. They eliminate duplicates.</p>
<div class="alert alert-info" role="alert">
>>> a = {1,2,2,3,3,3}<br>
>>> a<br>
{1, 2, 3}<br>
</div>
<p style="text-indent: 40px">Since, set are unordered collection, indexing has no meaning. Hence the slicing operator [] does not work.</p>
<div class="alert alert-info" role="alert">
>>> a = {1,2,3}<br>
>>> a[1]<br>
Traceback (most recent call last):<br>
File "<string>", line 301, in runcode<br>
File "<interactive input>", line 1, in <module><br>
TypeError: 'set' object does not support indexing
</div>
<h2>Python Dictionary</h2>
<p style="text-indent: 40px">Dictionary is an unordered collection of key-value pairs.</p>
<p style="text-indent: 40px">It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.</p>
<p style="text-indent: 40px">In Python, dictionaries are defined within braces {} with each item being a pair in the form <i>key:value</i>. Key and value can be of any type.</p>
<div class="alert alert-info" role="alert">
>>> d = {1:'value','key':2}<br>
>>> type(d)<br>
<class 'dict'>
</div>
<p style="text-indent: 40px">We use key to retrieve the respective value. But not the other way around.</p>
<div class="alert alert-info" role="alert">
d = {1:'value','key':2}<br>
print(type(d))<br>
<br>
print("d[1] = ", d[1]);<br>
<br>
print("d['key'] = ", d['key']);<br>
<br>
# Generates error<br>
print("d[2] = ", d[2]);
</div>
<h2>Conversion between data types</h2>
<p style="text-indent: 40px">We can convert between different data types by using different type conversion functions like int(), float(), str() etc.</p>
<div class="alert alert-info" role="alert">
>>> float(5)<br>
5.0
</div>
<p style="text-indent: 40px">Conversion from float to int will truncate the value (make it closer to zero).</p>
<div class="alert alert-info" role="alert">
>>> int(10.6)<br>
10<br>
>>> int(-10.6)<br>
-10
</div>
<p style="text-indent: 40px">Conversion to and from string must contain compatible values.</p>
<div class="alert alert-info" role="alert">
>>> float('2.5')<br>
2.5<br>
>>> str(25)<br>
'25'<br>
>>> int('1p')<br>
Traceback (most recent call last):<br>
File "<string>", line 301, in runcode<br>
File "<interactive input>", line 1, in <module><br>
ValueError: invalid literal for int() with base 10: '1p'
</div>
<p style="text-indent: 40px">We can even convert one sequence to another.</p>
<div class="alert alert-info" role="alert">
>>> set([1,2,3])<br>
{1, 2, 3}<br>
>>> tuple({5,6,7})<br>
(5, 6, 7)<br>
>>> list('hello')<br>
['h', 'e', 'l', 'l', 'o']
</div>
<p style="text-indent: 40px">To convert to dictionary, each element must be a pair</p>
<div class="alert alert-info" role="alert">
>>> dict([[1,2],[3,4]])<br>
{1: 2, 3: 4}<br>
>>> dict([(3,26),(4,44)])<br>
{3: 26, 4: 44}
</div>
<h1>Python Input, Output and Import</h1>
<p style="text-indent: 40px">Python provides numerous built-in functions that are readily available to us at the Python prompt.</p>
<p style="text-indent: 40px">Some of the functions like <i>input()</i> and <i>print()</i> are widely used for standard input and output operations respectively. Let us see the output section first.</p>
<h1>Python Output Using print() function</h1>
<p style="text-indent: 40px">We use the <i>print()</i> function to output data to the standard output device (screen).</p>
<p style="text-indent: 40px">We can also output data to a file, but this will be discussed later. An example use is given below.</p>
<div class="alert alert-info" role="alert">
print('This sentence is output to the screen')<br>
# Output: This sentence is output to the screen<br>
<br>
a = 5<br>
<br>
print('The value of a is', a)<br>
# Output: The value of a is 5
</div>
<p style="text-indent: 40px">In the second print() statement, we can notice that a space was added between the string and the value of variable <i>a</i>.This is by default, but we can change it.</p>
<p style="text-indent: 40px">The actual syntax of the <i>print()</i> function is</p>
<div class="alert alert-info" role="alert">
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
</div>
<p style="text-indent: 40px">Here, <i>objects</i> is the value(s) to be printed.</p>
<p style="text-indent: 40px">The <i>sep</i> separator is used between the values. It defaults into a space character.</p>
<p style="text-indent: 40px">After all values are printed, <i>end</i> is printed. It defaults into a new line.</p>
<p style="text-indent: 40px"><i>The file</i> is the object where the values are printed and its default value is <i>sys.stdout</i> (screen). Here are an example to illustrate this.</p>
<div class="alert alert-info" role="alert">
print(1,2,3,4)<br>
# Output: 1 2 3 4<br>
<br>
print(1,2,3,4,sep='*')<br>
# Output: 1*2*3*4<br>
<br>
print(1,2,3,4,sep='#',end='&')<br>
# Output: 1#2#3#4&
</div>
<h2>Output formatting</h2>
<p style="text-indent: 40px">Sometimes we would like to format our output to make it look attractive. This can be done by using the <i>str.format()</i> method. This method is visible to any string object.</p>
<div class="alert alert-info" role="alert">
>>> x = 5; y = 10<br>
>>> print('The value of x is {} and y is {}'.format(x,y))<br>
The value of x is 5 and y is 10
</div>
<p style="text-indent: 40px">Here the curly braces {} are used as placeholders. We can specify the order in which it is printed by using numbers (tuple index).</p>
<div class="alert alert-info" role="alert">
print('I love {0} and {1}'.format('bread','butter'))<br>
# Output: I love bread and butter<br>
<br>
print('I love {1} and {0}'.format('bread','butter'))<br>
# Output: I love butter and bread
</div>
<p style="text-indent: 40px">We can even use keyword arguments to format the string.</p>
<div class="alert alert-info" role="alert">
>>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John'))<br>
Hello John, Goodmorning
</div>
<p style="text-indent: 40px">We can even format strings like the old <i>sprintf()</i> style used in C programming language. We use the <i>%</i> operator to accomplish this.</p>
<div class="alert alert-info" role="alert">
>>> x = 12.3456789<br>
>>> print('The value of x is %3.2f' %x)<br>
The value of x is 12.35<br>
>>> print('The value of x is %3.4f' %x)<br>
The value of x is 12.3457
</div>
<h1>Python Input</h1>
<p style="text-indent: 40px">Up till now, our programs were static. The value of variables were defined or hard coded into the source code.</p>
<p style="text-indent: 40px">To allow flexibility we might want to take the input from the user. In Python, we have the <i>input()</i> function to allow this. The syntax for <i>input()</i> is</p>
<div class="alert alert-info" role="alert">
input([prompt])
</div>
<p style="text-indent: 40px">where <i>prompt</i> is the string we wish to display on the screen. It is optional.</p>
<div class="alert alert-info" role="alert">
>>> num = input('Enter a number: ')<br>
Enter a number: 10<br>
>>> num<br>
'10'
</div>
<p style="text-indent: 40px">Here, we can see that the entered value <i>10</i> is a string, not a number. To convert this into a number we can use <i>int()</i> or <i>float()</i> functions.</p>
<div class="alert alert-info" role="alert">
>>> int('10')<br>
10<br>
>>> float('10')<br>
10.0
</div>
<p style="text-indent: 40px">This same operation can be performed using the <i>eval()</i> function. But it takes it further. It can evaluate even expressions, provided the input is a string</p>
<div class="alert alert-info" role="alert">
>>> int('2+3')<br>
Traceback (most recent call last):<br>
File "<string>", line 301, in runcode<br>
File "<interactive input>", line 1, in <module><br>
ValueError: invalid literal for int() with base 10: '2+3'<br>
>>> eval('2+3')<br>
5
</div>
<h1>Python Import</h1>
<p style="text-indent: 40px">When our program grows bigger, it is a good idea to break it into different modules.</p>
<p style="text-indent: 40px">A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension <i>.py</i>.</p>
<p style="text-indent: 40px">Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the <i>import</i> keyword to do this.</p>
<p style="text-indent: 40px">For example, we can import the <i>math</i> module by typing in <i>import math</i>.</p>
<div class="alert alert-info" role="alert">
import math<br>
print(math.pi)
</div>
<p style="text-indent: 40px">Now all the definitions inside <i>math</i> module are available in our scope. We can also import some specific attributes and functions only, using the <i>from</i> keyword. For example:</p>
<div class="alert alert-info" role="alert">
>>> from math import pi<br>
>>> pi<br>
3.141592653589793
</div>
<p style="text-indent: 40px">While importing a module, Python looks at several places defined in <i>sys.path</i>. It is a list of directory locations.</p>
<div class="alert alert-info" role="alert">
>>> import sys<br>
>>> sys.path<br>
['', <br>
'C:\\Python33\\Lib\\idlelib', <br>
'C:\\Windows\\system32\\python33.zip',<br>
'C:\\Python33\\DLLs', <br>
'C:\\Python33\\lib', <br>
'C:\\Python33', <br>
'C:\\Python33\\lib\\site-packages']
</div>
<p style="text-indent: 40px">We can add our own location to this list as well.</p>
<h1>Python Operators</h1>
<p style="text-indent: 40px">Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.</p>
<p style="text-indent: 40px">For example:</p>
<div class="alert alert-info" role="alert">
>>> 2+3
5
</div>
<p style="text-indent: 40px">Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.</p>
<p style="text-indent: 40px">Python has a number of operators which are classified below.</p>
<b>Type of operators in Python</b>
<table class="table table-bordered" style="border-color: black">
<tbody>
<td>Arithmetic operators</td>
<td>Comparison (Relational) operators</td>
<td>Logical (Boolean) operators</td>
<td>Bitwise operators</td>
<td>Assignment operators</td>
<td>Special operators</td>
</tbody>
</table>
<h1>Arithmetic operators</h1>
<p style="text-indent: 40px">Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.</p>
<b>Arithmetic operators in Python</b>
<table class="table">
<thead>
<th>Operator</th>
<th>Meaning</th>
<th>Example</th>
</thead>
<tbody>
<tr>
<td>+</td>
<td>Add two operands or unary plus</td>
<td>x + y<br>+2</td>
</tr>
<tr>
<td>-</td>
<td>Subtract right operand from the left or unary minus</td>
<td>x - y<br>-2</td>
</tr>
<tr>
<td>*</td>
<td>Multiply two operands</td>
<td>x * y</td>
</tr>
<tr>
<td>/</td>
<td>Divide left operand by the right one (always results into float)</td>
<td>x / y</td>
</tr>
<tr>
<td>%</td>
<td>Modulus - remainder of the division of left operand by the right</td>
<td>x % y (remainder of x/y)</td>
</tr>
<tr>
<td>//</td>
<td>Floor division - division that results into whole number adjusted to the left in the number line</td>
<td>x // y</td>
</tr>
<tr>
<td>**</td>
<td>Exponent - left operand raised to the power of right</td>
<td>x**y (x to the power y)</td>
</tr>
</tbody>
</table>
<h2>Example #1: Arithmetic operators in Python</h2>
<div class="alert alert-info" role="alert">
x = 15<br>
y = 4<br>
<br>
# Output: x + y = 19<br>
print('x + y =',x+y)<br>
<br>
# Output: x - y = 11<br>
print('x - y =',x-y)<br>
<br>
# Output: x * y = 60<br>
print('x * y =',x*y)<br>
<br>
# Output: x / y = 3.75<br>
print('x / y =',x/y)<br>
<br>
# Output: x // y = 3<br>
print('x // y =',x//y)<br>
<br>
# Output: x ** y = 50625<br>
print('x ** y =',x**y)
</div>
<p style="text-indent: 40px">When you run the program, the output will be:</p>
<div class="alert alert-info" role="alert">
x + y = 19<br>
x - y = 11<br>
x * y = 60<br>
x / y = 3.75<br>
x // y = 3<br>
x ** y = 50625<br>
</div>
<h1>Comparison operators</h1>
<p style="text-indent: 40px">Comparison operators are used to compare values. It either returns True or False according to the condition.</p>
<b>Comparision operators in Python</b>
<table class="table">
<thead>
<th>Operator</th>
<th>Meaning</th>
<th>Example</th>
</thead>
<tbody>
<tr>
<td>></td>
<td>Greater that - True if left operand is greater than the right</td>
<td>x > y</td>
</tr>
<tr>
<td><</td>
<td>Less that - True if left operand is less than the right</td>
<td>x < y</td>
</tr>
<tr>
<td>==</td>
<td>Equal to - True if both operands are equal</td>
<td>x == y</td>
</tr>
<tr>
<td>!=</td>
<td>Not equal to - True if operands are not equal</td>
<td>x >= y</td>
</tr>
<tr>
<td>>=</td>
<td>Greater than or equal to - True if left operand is greater than or equal to the right</td>
<td>x != y</td>
</tr>
<tr>
<td><=</td>
<td>Less than or equal to - True if left operand is less than or equal to the right</td>
<td>x <= y</td>
</tr>
</tbody>
</table>
<br>
<b>Example #2: Comparison operators in Python</b><br>
<br>