-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython.html
8548 lines (8459 loc) · 239 KB
/
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>
<head>
<!-- ad sense tag-->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1733919132650092"
crossorigin="anonymous"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FBJL1SB5QM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-FBJL1SB5QM");
</script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="title" content="Coding-nation" />
<meta name="description"
content="CodingNation is a comprehensive code tutorial website, providing beginner to advanced coding tutorials, coding exercises, and coding challenges to help you learn to code and build your skills." />
<meta name="keywords"
content="Coding-nation, CodeNation, HTML tutorials, python tutorials, css tutorials, c tutorials,React js tutorials, javascript tutorials,java tutorials, php tutorials, code editor, online code editor, Codebot,chatbot,quizzes" />
<meta name="robots" content="index, follow" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="English" />
<meta name="author" content="coding-nation.com" />
<title>Python Course | coding-nation</title>
<link rel="icon" type="image/png" sizes="32x32" href="../images/favicon.png" />
<!-- Latest compiled and minified CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet" />
<link rel="stylesheet" href="../Styles/Notes.css" />
<script>
function changeTitle(newTitle) {
document.title = newTitle;
}
</script>
</head>
<body>
<!--for whole page-->
<div class="contain">
<!--navigation bar-->
<header class="fixed-navbar">
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="../index.html">CodingNation</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto" id="navbarNavLinks">
<li class="nav-item">
<a class="nav-link" href="../index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">Courses</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../Python.html">Python</a>
</li>
<li>
<a class="dropdown-item" href="../React.html">React JS</a>
</li>
<li>
<a class="dropdown-item" href="../html.html">HTML</a>
</li>
<li><a class="dropdown-item" href="../css.html">CSS</a></li>
<li>
<a class="dropdown-item" href="../jascri.html">JavaScript</a>
</li>
<li><a class="dropdown-item" href="../cpro.html">C</a></li>
<li>
<a class="dropdown-item" href="../java.html">Java</a>
</li>
<li><a class="dropdown-item" href="../php.html">PHP</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">Practice</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLQuiz.html" target="_blank">HTML
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CSSQuiz.html" target="_blank">CSS
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JSQuiz.html" target="_blank">JavaScript
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CQuiz.html" target="_blank">C Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAQuiz.html" target="_blank">Java
Quiz</a>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button"
data-bs-toggle="dropdown">Exercises</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLExer.html" target="_blank">HTML Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CSSExer.html" target="_blank">CSS Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JsExer.html" target="_blank">JavaScript
Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CExer.html" target="_blank">C Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAExer.html" target="_blank">Java Exercise</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="../codebot.html" target="_blank">CodeBot</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../Editor.html" target="_blank">Editor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../About.html">About</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<!--sidebar-->
<button class="topics" onclick="toggleSidebar()">Topics▼</button>
<aside class="fixed-sidebar" id="myNav">
<div class="menu">
<h2>Introduction</h2>
<button class="single" data-index="0" onclick="changeTitle('Python Overview | CodingNation')">
Python Overview
</button>
<button class="single" data-index="1" onclick="changeTitle('Installation & Getting Started | CodingNation')">
Installation & <br />
Getting Started
</button>
<button class="single" data-index="2" onclick="changeTitle('What is Syntax? | CodingNation')">
What is Syntax?
</button>
<button class="single" data-index="3" onclick="changeTitle('Python Comments | CodingNation')">
Python Comments
</button>
<button class="single" data-index="4" onclick="changeTitle('Python Variables | CodingNation')">
Python Variables
</button>
<h2>Python<br />DataTypes& Operators</h2>
<button class="single" data-index="5" onclick="changeTitle('Data Types | CodingNation')">
Data Types
</button>
<button class="single" data-index="6" onclick="changeTitle('Python Numbers | CodingNation')">
Python Numbers
</button>
<button class="single" data-index="7" onclick="changeTitle('Data Conversion | CodingNation')">
Data Conversion
</button>
<button class="single" data-index="8" onclick="changeTitle('Type Casting | CodingNation')">
Type Casting
</button>
<button class="single" data-index="9" onclick="changeTitle('Python Operators | CodingNation')">
Python Operators
</button>
<button class="single" data-index="10" onclick="changeTitle('Python Booleans | CodingNation')">
Python Booleans
</button>
<h2>Python Strings</h2>
<button class="single" data-index="11" onclick="changeTitle('Python Strings | CodingNation')">
Python Strings
</button>
<button class="single" data-index="12" onclick="changeTitle('Operation on Strings | CodingNation')">
Operation on Strings
</button>
<button class="single" data-index="13" onclick="changeTitle('String Methods | CodingNation')">
String Methods
</button>
<button class="single" data-index="14" onclick="changeTitle('Format Strings | CodingNation')">
Format Strings
</button>
<button class="single" data-index="15" onclick="changeTitle('Escape Characters | CodingNation')">
Escape Characters
</button>
<h2>Python Lists</h2>
<button class="single" data-index="16" onclick="changeTitle('Python Lists | CodingNation')">
Python Lists
</button>
<button class="single" data-index="17" onclick="changeTitle('List Indexes | CodingNation')">
List Indexes
</button>
<button class="single" data-index="18" onclick="changeTitle('Add List Items | CodingNation')">
Add List Items
</button>
<button class="single" data-index="19" onclick="changeTitle('Remove List Items | CodingNation')">
Remove List Items
</button>
<button class="single" data-index="20" onclick="changeTitle('Change List Items | CodingNation')">
Change List Items
</button>
<button class="single" data-index="21" onclick="changeTitle('List Comprehension | CodingNation')">
List Comprehension
</button>
<button class="single" data-index="22" onclick="changeTitle('List Methods | CodingNation')">
List Methods
</button>
<h2>Python Tuples</h2>
<button class="single" data-index="23" onclick="changeTitle('Python Tuples | CodingNation')">
Python Tuples
</button>
<button class="single" data-index="24" onclick="changeTitle('Tuple Indexes | CodingNation')">
Tuple Indexes
</button>
<button class="single" data-index="25" onclick="changeTitle('Manipulating Tuples | CodingNation')">
Manipulating Tuples
</button>
<button class="single" data-index="26" onclick="changeTitle('Unpack Tuples | CodingNation')">
Unpack Tuples
</button>
<h2>Python Sets</h2>
<button class="single" data-index="27" onclick="changeTitle('Python Sets | CodingNation')">
Python Sets
</button>
<button class="single" data-index="28" onclick="changeTitle('Add/Remove Set Items | CodingNation')">
Add/Remove Set Items
</button>
<button class="single" data-index="29" onclick="changeTitle('Join Sets | CodingNation')">
Join Sets
</button>
<button class="single" data-index="30" onclick="changeTitle('Set Methods | CodingNation')">
Set Methods
</button>
<h2>Python Dictionaries</h2>
<button class="single" data-index="31" onclick="changeTitle('Python Dictionaries | CodingNation')">
Python Dictionaries
</button>
<button class="single" data-index="32" onclick="changeTitle('Access Items | CodingNation')">
Access Items
</button>
<button class="single" data-index="33" onclick="changeTitle('Add/Remove Items | CodingNation')">
Add/Remove Items
</button>
<button class="single" data-index="34" onclick="changeTitle('Copy Dictionaries | CodingNation')">
Copy Dictionaries
</button>
<h2>
<h2>Conditional Statements</h2>
</h2>
<button class="single" data-index="35" onclick="changeTitle('if Statement | CodingNation')">
if Statement
</button>
<button class="single" data-index="36" onclick="changeTitle('if-else Statement | CodingNation')">
if-else Statement
</button>
<button class="single" data-index="37" onclick="changeTitle('elif Statement | CodingNation')">
elif Statement
</button>
<button class="single" data-index="38" onclick="changeTitle('Nested if Statement | CodingNation')">
Nested if Statement
</button>
<h2>Python Loops</h2>
<button class="single" data-index="39" onclick="changeTitle('Python for Loop | CodingNation')">
Python for Loop
</button>
<button class="single" data-index="40" onclick="changeTitle('Python while Loop | CodingNation')">
Python while Loop
</button>
<button class="single" data-index="41" onclick="changeTitle('Nested Loop | CodingNation')">
Nested Loop
</button>
<button class="single" data-index="42" onclick="changeTitle('Control Statements | CodingNation')">
Control Statements
</button>
<h2>Python Functions</h2>
<button class="single" data-index="43" onclick="changeTitle('Python Functions | CodingNation')">
Python Functions
</button>
<button class="single" data-index="44" onclick="changeTitle('Function Arguments | CodingNation')">
Function Arguments
</button>
<button class="single" data-index="45" onclick="changeTitle('Return Statements | CodingNation')">
Return Statements
</button>
<button class="single" data-index="46" onclick="changeTitle('Python Recursion | CodingNation')">
Python Recursion
</button>
<h2>Python Modules</h2>
<button class="single" data-index="47" onclick="changeTitle('Python Modules | CodingNation')">
Python Modules
</button>
<button class="single" data-index="48" onclick="changeTitle('Python Packages | CodingNation')">
Python Packages
</button>
<h2>Python OOPS</h2>
<button class="single" data-index="49" onclick="changeTitle('Python OOPS | CodingNation')">
Python OOPS
</button>
<button class="single" data-index="50" onclick="changeTitle('self method | CodingNation')">
self method
</button>
<button class="single" data-index="51" onclick="changeTitle('__init__ method | CodingNation')">
__init__ method
</button>
<h2>Advanced Topics</h2>
<button class="single" data-index="52" onclick="changeTitle('Python Iterators | CodingNation')">
Python Iterators
</button>
<button class="single" data-index="53" onclick="changeTitle('JSON | CodingNation')">
JSON
</button>
<button class="single" data-index="54" onclick="changeTitle('Python try...except | CodingNation')">
Python try...except
</button>
<button class="single" data-index="55" onclick="changeTitle('Python PIP | CodingNation')">
Python PIP
</button>
<button class="single" data-index="56" onclick="changeTitle('Data & Time | CodingNation')">
Data & Time
</button>
<h2>File Handling</h2>
<button class="single" data-index="57" onclick="changeTitle('Python File Handling | CodingNation')">
Python File Handling
</button>
<button class="single" data-index="58" onclick="changeTitle('Read/Write Files | CodingNation')">
Read/Write Files
</button>
</div>
</aside>
<!--for content side-->
<main class="main-content">
<div class="content">
<!--previous and next buttons-->
<button id="previous">Previous</button>
<button id="next">Next</button>
<!--Notes-->
<div class="notes" id="div1">
<h2>Python Overview</h2>
<br />
<h3>What is Python</h3>
<br />
<li>
Python is a dynamically typed, General Purpose Programming
Language that supports an object-oriented programming approach as
well as a functional programming approach.
</li>
<li>
Python is also an interpreted and high-level programming language.
</li>
<li>It was created by Guido Van Rossum in 1989.</li>
<br /><br />
<h3>Features of Python</h3>
<br />
<li>Python is simple and easy to understand.</li>
<li>
It is Interpreted and platform-independent which makes debugging
very easy.
</li>
<li>Python is an open-source programming language.</li>
<li>
Python provides very big library support. Some of the popular
libraries include NumPy, Tensorflow, Selenium, OpenCV, etc.
</li>
<li>
It is possible to integrate other programming languages within
python.
</li>
<br />
<br />
<h3>What is Python used for</h3>
<br />
<li>
Python is used in Data Visualization to create plots and graphical
representations.
</li>
<li>
Python helps in Data Analytics to analyze and understand raw data
for insights and trends.
</li>
<li>
It is used in AI and Machine Learning to simulate human behavior
and to learn from past data without hard coding.
</li>
<li>It is used to create web applications.</li>
<li>It can be used to handle databases.</li>
<li>
It is used in business and accounting to perform complex
mathematical operations along with quantitative and qualitative
analysis.
</li>
<br />
<br />
</div>
<div class="notes" id="div2" style="display: none">
<h2>Installation & Getting Started</h2>
<br />
<h3>Steps to Install Python:</h3>
<br />
<li>
Visit the official python website:
<a href="https://www.python.org/" target="_blank">https://www.python.org/</a>
</li>
<li>
Download the executable file based on your Operating System and
version specifications.
</li>
<li>
Run the executable file and complete the installation process.
</li>
<br />
<h3>Version:</h3>
<br />
<p>
After installation check the version of python by typing following
command: ‘python --version’.
</p>
<br />
<br />
<h3>Starting Python:</h3>
<br />
<p>
Open Python IDE or any other text editor of your preferred choice.
Let’s understand python code execution with the simplest print
statement. Type the following in the IDE:
</p>
<br />
<pre class="highlight">
<code class="language-js">
print("Hello World !!!")
</code>
</pre>
<br />
<br />
<p>
Now save the file with a .py extension and Run it. You will get
the following output:
</p>
<br />
<pre class="highlight">
<code class="language-js">
Hello World !!!
</code>
</pre>
<br />
<br />
<h3>Installing Packages:</h3>
<br />
<p>
To install packages in Python, we use the pip command. <br />
e.g. pip install "Package Name"
<br />
Following command installs pandas package in Python
</p>
<br />
<pre class="highlight">
<code class="language-js">
print("Hello World !!!")
</code>
</pre>
<br />
<br />
<p>
We will learn more about the pip command in the chapter dedicated
to pip.
</p>
<br />
<br />
</div>
<div class="notes" id="div3" style="display: none">
<h2>What is Syntax?</h2>
<br />
<p>
In simplest words, Syntax is the arrangement of words and phrases
to create well-formed sentences in a language. In the case of a
computer language, the syntax is the structural arrangement of
comments, variables, numbers, operators, statements, loops,
functions, classes, objects, etc. which helps us understand the
meaning or semantics of a computer language.
</p>
<br />
<br />
<p>
For E.g. a ‘comment’ is used to explain the functioning of a block
of code. It starts with a ‘#’.
<br />
More on comments in the comments chapter.
</p>
<br />
<br />
<p>
For E.g. a block of code is identified by an ‘indentation’. Have a
look at the following code, here print(i) is said to be indented
with respect to the link above it. In simple words, indentation is
the addition of spaces before the line "print(i)"
</p>
<br />
<pre class="highlight">
<code class="language-js">
for i in range(5):
print(i)
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div4" style="display: none">
<h2>Python Comments</h2>
<br />
<p>
A comment is a part of the coding file that the programmer does
not want to execute, rather the programmer uses it to either
explain a block of code or to avoid the execution of a specific
part of code while testing.
</p>
<br />
<br />
<h3>Single-Line Comments:</h3>
<br />
<p>To write a comment just add a ‘#’ at the start of the line.</p>
<br />
<h4>Example 1:</h4>
<br />
<pre class="highlight">
<code class="language-js">
#This is a 'Single-Line Comment'
print("This is a print statement.")
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
This is a print statement.
</code>
</pre>
<br />
<br />
<h4>Example 2:</h4>
<br />
<pre class="highlight">
<code class="language-js">
print("Hello World !!!") #Printing Hello World
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
Hello World !!!
</code>
</pre>
<br />
<br />
<h4>Example 3:</h4>
<br />
<pre class="highlight">
<code class="language-js">
print("Python Program")
#print("Python Program")
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
Python Program
</code>
</pre>
<br />
<br />
<h3>Multi-Line Comments:</h3>
<br />
<p>
To write multi-line comments you can use ‘#’ at each line or you
can use the multiline string.
</p>
<br />
<p><b>Example 1:</b> The use of ‘#’.</p>
<br />
<pre class="highlight">
<code class="language-js">
#It will execute a block of code if a specified condition is true.
#If the codition is false than it will execute another block of code.
p = 7
if (p > 5):
print("p is greater than 5.")
else:
print("p is not greater than 5.")
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
p is greater than 5.
</code>
</pre>
<br />
<br />
<p><b>Example 2:</b> The use of multiline string.</p>
<br />
<pre class="highlight">
<code class="language-js">
"""This is an if-else statement.
It will execute a block of code if a specified condition is true.
If the condition is false then it will execute another block of code."""
p = 7
if (p > 5):
print("p is greater than 5.")
else:
print("p is not greater than 5.")
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
p is greater than 5.
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div5" style="display: none">
<h2>Python Variables</h2>
<br />
<p>
Variables are containers that store information that can be
manipulated and referenced later by the programmer within the
code.
<br />
In python, the programmer does not need to declare the variable
type explicitly, we just need to assign the value to the variable.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
name = "Abhishek" #type str
age = 20 #type int
passed = True #type bool
</code>
</pre>
<br />
<br />
<p>
It is always advisable to keep variable names descriptive and to
follow a set of conventions while creating variables:
</p>
<br />
<li>
Variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
</li>
<li>
Variable name must start with a letter or the underscore
character.
</li>
<li>Variables are case sensitive.</li>
<li>Variable name cannot start with a number.</li>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
Color = "yellow" #valid variable name
cOlor = "red" #valid variable name
_color = "blue" #valid variable name
5color = "green" #invalid variable name
$color = "orange" #invalid variable name
</code>
</pre>
<br />
<br />
<p>
Sometimes, a multi-word variable name can be difficult to read by
the reader. To make it more readable, the programmer can do the
following:
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
NameOfCity = "Mumbai" #Pascal case
nameOfCity = "Berlin" #Camel case
name_of_city = "Moscow" #snake case
</code>
</pre>
<br />
<br />
<h3>Scope of variable:</h3>
<br />
<p>
The scope of the variable is the area within which the variable
has been created. Based on this a variable can either have a local
scope or a global scope.
</p>
<br />
<br />
<h3>Local Variable:</h3>
<br />
<p>
A local variable is created within a function and can be only used
inside that function. Such a variable has a local scope.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
icecream = "Vanilla" #global variable
def foods():
vegetable = "Potato" #local variable
fruit = "Lichi" #local variable
print(vegetable + " is a local variable value.")
print(icecream + " is a global variable value.")
print(fruit + " is a local variable value.")
foods()
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
Potato is a local variable value.
Vanilla is a global variable value.
Lichi is a local variable value.
</code>
</pre>
<br />
<br />
<h3>Global Variable:</h3>
<br />
<p>
A global variable is created in the main body of the code and can
be used anywhere within the code. Such a variable has a global
scope.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
icecream = "Vanilla" #global variable
def foods():
vegetable = "Potato" #local variable
fruit = "Lichi" #local variable
print(vegetable + " is a local variable value.")
foods()
print(icecream + " is a global variable value.")
print(fruit + " is a local variable value.")
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
Potato is a local variable value.
Vanilla is a global variable value.
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div6" style="display: none">
<h2>Data Types</h2>
<br />
<p>
Data type specifies the type of value a variable requires to do
various operations without causing an error. By default, python
provides the following built-in data types:
</p>
<br />
<h3>Numeric data: int, float, complex</h3>
<br />
<p>
int: 3, -8, 0
<br />
float: 7.349, -9.0, 0.0000001
<br />
complex: 6 + 2i
<br />
more on numeric data types in the number chapter.
</p>
<br />
<br />
<h3>Text data: str</h3>
<br />
<p>str: “Hello World!!!”, “Python Programming”</p>
<br />
<br />
<h3>Boolean data:</h3>
<br />
<p>Boolean data consists of values True or False.</p>
<br /><br />
<h3>Sequenced data: list, tuple, range</h3>
<br />
<p>
<b>List:</b> A list is an ordered collection of data with elements
separated by a comma and enclosed within square brackets. Lists
are mutable and can be modified after creation.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
list1 = [8, 2.3, [-4, 5], ["apple", "banana"]]
print(list1)
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
[8, 2.3, [-4, 5], ['apple', 'banana']]
</code>
</pre>
<br />
<br />
<p>
<b>tuple:</b>A tuple is an ordered collection of data with
elements separated by a comma and enclosed within parentheses.
Tuples are immutable and can not be modified after creation.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
tuple1 = (("parrot", "sparrow"), ("Lion", "Tiger"))
print(tuple1)
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
(('parrot', 'sparrow'), ('Lion', 'Tiger'))
</code>
</pre>
<br />
<br />
<p>
<b>range:</b>returns a sequence of numbers as specified by the
user. If not specified by the user then it starts from 0 by
default and increments by 1.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
sequence1 = range(4,14,2)
for i in sequence1:
print(i)
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
4
6
8
10
12
</code>
</pre>
<br />
<br />
<h3>Mapped data: dict</h3>
<br />
<p>
<b>dict:</b> a dictionary is an unordered collection of data
containing a key:value pair. The key:value pairs are enclosed
within curly brackets.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
dict1 = {"name":"sweety", "age":20, "canVote":True}
print(dict1)
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
{'name': 'sweety', 'age': 20, 'canvote': True}
</code>
</pre>
<br />
<br />
<h3>Binary data: bytes, bytearray, memoryview</h3>
<br />
<p>
<b>bytes:</b>bytes() function is used to convert objects into byte
objects, or create empty bytes object of the specified size.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
#Converting string to bytes
str1 = "This is a string"
arr1 = bytes(str1, 'utf-8')
print(arr1)
arr2 = bytes(str1, 'utf-16')
print(arr2)
#Creating bytes of given size
bytestr = bytes(4)
print(bytestr)
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-js">
b'This is a string'
b'\xff\xfeT\x00h\x00i\x00s\x00 \x00i\x00s\x00 \x00a\x00 \x00s\x00t\x00r\x00i\x00n\x00g\x00'
b'\x00\x00\x00\x00'
</code>
</pre>
<br />
<br />
<p>
<b>bytearray:</b> bytearray() function is used to convert objects
into bytearray objects, or create empty bytearray object of the
specified size.
</p>
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-js">
#Converting string to bytes
str1 = "This is a string"
arr1 = bytearray(str1, 'utf-8')
print(arr1)
arr2 = bytearray(str1, 'utf-16')
print(arr2)