-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata-types.html
More file actions
1013 lines (893 loc) · 48.3 KB
/
data-types.html
File metadata and controls
1013 lines (893 loc) · 48.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Meta tags for search engines-->
<meta name="keywords"
content="python, cheatsheet, cheat, sheet, functions, args, kwargs, sets, dictionary, list, string, regular, expression, formatting, comprehension">
<meta name="description"
content="Explore Python effortlessly with Python Cheat Sheet. Your quick reference for essential Python concepts. Clear explanations, intuitive design, and useful links for efficient learning. Perfect for both beginners and experienced Python developers.">
<!-- Favicons -->
<link type="image/png" sizes="16x16" rel="icon" href="./assets/images/favicons/icons8-python-gradient-16.png">
<link type="image/png" sizes="32x32" rel="icon" href="./assets/images/favicons/icons8-python-gradient-32.png">
<link type="image/png" sizes="96x96" rel="icon" href="./assets/images/favicons/icons8-python-gradient-96.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180"
href="./assets/images/favicons/icons8-python-gradient-180.png">
<!-- Title -->
<title>Variables and data types</title>
<!-- Highlight.js -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css"
integrity="sha512-rO+olRTkcf304DQBxSWxln8JXCzTHlKnIdnMUwYvQa9/Jd4cQaNkItIUj6Z4nvW1dqK0SKXLbn9h4KwZTNtAyw=="
crossorigin="anonymous" referrerpolicy="no-referrer">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<!-- Stylesheet -->
<link rel="stylesheet" href="./assets/css/style.css">
<!-- Fontawesome -->
<script src="https://kit.fontawesome.com/e212569926.js" crossorigin="anonymous"></script>
</head>
<body>
<header>
<input type="radio" id="nav-topics-show" name="nav-topics-toggle">
<label class="font-aw-icon" for="nav-topics-show">
<i class="fa-solid fa-bars font-aw-icon"></i>
</label>
<h1>
<a href="index.html">
<img id="logo" src="./assets/images/icons8-python.svg" alt="logo">
</a>
</h1>
<div id="feedback-icon"><a href="feedback.html" aria-label="Visit feedback page"><i class="fa-solid fa-comment font-aw-icon"></i></a></div>
<nav id="nav-site">
<ul class="bold-text">
<li><a href="./index.html">Home</a></li>
<li class="selected-page"><a href="./variables.html">Get Started</a></li>
<li><a href="./feedback.html">Feedback</a></li>
</ul>
</nav>
<ul id="outer-sites">
<li>
<a href="https://www.linkedin.com/in/dima-bulavenko-07a162239/" target="_blank" rel="noopener"
aria-label="Visit my LinkedIn page (open in new tab)">
<i class="fa-brands fa-linkedin font-aw-icon"></i>
</a>
</li>
<li>
<a href="https://github.com/Dima-Bulavenko/python-cheat-sheet" target="_blank" rel="noopener"
aria-label="Visit GitHub repository of this project (open in new tab)">
<i class="fa-brands fa-square-github font-aw-icon"></i>
</a>
</li>
</ul>
</header>
<input type="radio" id="nav-topics-hide" name="nav-topics-toggle" checked>
<div id="nav-topics-container">
<div id="nav-bar-topics">
<label for="nav-topics-hide"><i class="fa-solid fa-xmark font-aw-icon"></i></label>
<nav id="nav-topics">
<ul>
<li>
<h2><a href="./variables.html">Variables</a></h2>
<ul>
<li>
<h3><a href="./variables.html#assignments">Assignments</a></h3>
</li>
<li>
<h3><a href="./variables.html#multiple-assignments">Multiple Assignments</a></h3>
</li>
<li>
<h3><a href="./variables.html#variable-naming-rules">Variable Naming Rules</a></h3>
</li>
<li>
<h3><a href="./variables.html#dynamic-typing">Dynamic Typing</a></h3>
</li>
<li>
<h3><a href="./variables.html#variable-reassignment">Variable Reassignment</a></h3>
</li>
</ul>
</li>
<li>
<h2><a href="./operators.html">Operators</a></h2>
<ul>
<li>
<h3><a href="./operators.html#arithmetic-operators">Arithmetic Operators</a></h3>
</li>
<li>
<h3><a href="./operators.html#assignment-operators">Assignment Operators</a></h3>
</li>
<li>
<h3><a href="./operators.html#comparison-operators">Comparison Operators</a></h3>
</li>
<li>
<h3><a href="./operators.html#logical-operators">Logical Operators</a></h3>
</li>
<li>
<h3><a href="./operators.html#identity-operators">Identity Operators</a></h3>
</li>
<li>
<h3><a href="./operators.html#membership-operators">Membership Operators</a></h3>
</li>
<li>
<h3><a href="./operators.html#operator-precedence">Operator Precedence</a></h3>
</li>
</ul>
</li>
<li>
<h2 class="selected-page"><a href="./data-types.html">Data Types</a></h2>
<ul>
<li>
<h3><a href="./data-types.html#numeric-types">Numeric Types</a></h3>
</li>
<li>
<h3><a href="./data-types.html#string">String</a></h3>
</li>
<li>
<h3><a href="./data-types.html#list">List</a></h3>
</li>
<li>
<h3><a href="./data-types.html#tuple">Tuple</a></h3>
</li>
<li>
<h3><a href="./data-types.html#dictionary">Dictionary</a></h3>
</li>
<li>
<h3><a href="./data-types.html#set">Set</a></h3>
</li>
<li>
<h3><a href="./data-types.html#boolean">Boolean</a></h3>
</li>
<li>
<h3><a href="./data-types.html#none">None</a></h3>
</li>
</ul>
</li>
<li>
<h2><a href="./control-flow.html">Control Flow</a></h2>
<ul>
<li>
<h3><a href="./control-flow.html#conditional-statements">Conditional Statements</a></h3>
</li>
<li>
<h3><a href="./control-flow.html#loops">Loops</a></h3>
</li>
<li>
<h3><a href="./control-flow.html#break-and-continue-statements">Break and Continue
Statements</a></h3>
</li>
</ul>
</li>
<li>
<h2><a href="./functions.html">Functions</a></h2>
<ul>
<li>
<h3><a href="./functions.html#defining-functions">Defining Functions</a></h3>
</li>
<li>
<h3><a href="./functions.html#function-parameters">Function Parameters</a></h3>
</li>
<li>
<h3><a href="./functions.html#lambda-functions">Lambda Functions</a></h3>
</li>
<li>
<h3><a href="./functions.html#recursion">Recursion</a></h3>
</li>
</ul>
</li>
<li>
<h2><a href="./modules-and-packages.html">Modules and Packages</a></h2>
<ul>
<li>
<h3><a href="./modules-and-packages.html#importing-modules">Importing Modules</a></h3>
</li>
<li>
<h3><a href="./modules-and-packages.html#creating-and-using-packages">Creating and Using
Packages</a></h3>
</li>
</ul>
</li>
<li>
<h2><a href="./error-handling.html">Error Handling</a></h2>
<ul>
<li>
<h3><a href="./error-handling.html#exceptions-and-errors">Exceptions and Errors</a></h3>
</li>
<li>
<h3><a href="./error-handling.html#try-except-blocks">Try, Except Blocks</a></h3>
</li>
<li>
<h3><a href="./error-handling.html#handling-types-of-exceptions">Handling Types of
Exceptions</a></h3>
</li>
</ul>
</li>
<li>
<h2><a href="./file-handling.html">File Handling</a></h2>
<ul>
<li>
<h3><a href="./file-handling.html#reading-from-files">Reading from Files</a></h3>
</li>
<li>
<h3><a href="./file-handling.html#writing-to-files">Writing to Files</a></h3>
</li>
<li>
<h3><a href="./file-handling.html#working-with-file-paths">Working with File Paths</a>
</h3>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
<main>
<section class="topic-section" id="data-types">
<h2>Data Types</h2>
<p>In Python, data types categorize the types of values that variables can hold. They define how the values
can be stored, manipulated, and operated upon in a program. Python is dynamically typed, meaning you
don't need to explicitly declare the data type of a variable; the interpreter determines it based on the
assigned value.</p>
<p class="highlight-block">Common data types include integers, floating-point numbers, strings, lists,
tuples, dictionaries, sets</p>
<section id="numeric-types">
<h3>Numeric Types <a
href="https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex"
target="_blank" rel="noopener"
aria-label="Get detailed information about Numeric Types in python official documentation (open in new tab)"><i
class="fa-solid fa-circle-info"></i></a></h3>
<p>Numeric types in Python represent numerical values and are essential for performing mathematical
operations. The main numeric types are
<span class="highlight-text">int</span> (integer), <span class="highlight-text">float</span>
(floating-point), and
<span class="highlight-text">complex</span> (complex numbers).
</p>
<h4>Integer</h4>
<p>Represents whole numbers without any decimal points.</p>
<p>Can be positive or negative.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">num_int = 42
negative_int = -10
</code></pre>
<h4>Floating-Point</h4>
<p>Represents numbers with decimal points or in exponential form.</p>
<p>Can be used to represent real numbers.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">num_float = 3.14
</code></pre>
<h4>Complex Numbers</h4>
<p>Represents numbers in the form of a + bj, where "a" and "b" are real numbers, and "j" is the
imaginary
unit.</p>
<p>Useful in mathematical and engineering applications.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">num_complex = 2 + 3j
</code></pre>
<h4>Operations on Numeric Types</h4>
<p>Python supports various arithmetic operations on numeric types:</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python"># Examples:
a = 10
b = 3
# Addition
sum_result = a + b # Result: 13
# Subtraction
diff_result = a - b # Result: 7
# Multiplication
prod_result = a * b # Result: 30
# Division
div_result = a / b # Result: 3.3333 (floating-point division)
# Floor Division (returns the floor value)
floor_div_result = a // b # Result: 3
# Modulus (returns the remainder)
mod_result = a % b # Result: 1
# Exponentiation
exp_result = a ** b # Result: 1000
</code></pre>
</section>
<section id="string">
<h3>String <a href="https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str"
target="_blank" rel="noopener"
aria-label="Get detailed information about String type in python official documentation (open in new tab)"><i
class="fa-solid fa-circle-info"></i></a></h3>
<p>Strings are sequences of characters, and they can be defined using either single quotes (') or double
quotes (").</p>
<p><span class="highlight-text">Strings are immutable</span>, meaning you cannot change the characters
of an existing string directly.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
</code></pre>
<h4>Accessing Characters</h4>
<p>You can access individual characters in a string using indexing.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
first_char = my_string[0] # Result: 'H'
</code></pre>
<h4>String Slicing</h4>
<p>Slicing allows you to extract a portion of the string.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python"># Initial string
my_string = "Python Programming"
# String Slicing
substring_1 = my_string[0:6] # Result: 'Python'
substring_2 = my_string[7:18] # Result: 'Programming'
substring_3 = my_string[:6] # Result: 'Python' (omitting start index defaults to 0)
substring_4 = my_string[7:] # Result: 'Programming' (omitting end index defaults to the end)
# Slicing with a step
substring_5 = my_string[0:10:2] # Result: 'Pto rg' (every second character from index 0 to 9)
# Reverse the string using slicing
reversed_string = my_string[::-1] # Result: 'gnimmargorP nohtyP'
</code></pre>
<h4>Useful String Methods</h4>
<div class="method">
<p><span class="highlight-text">len()</span> - returns the length (number of
characters) of a string.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
length = len(my_string) # Result: 13
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">lower() and upper()</span> - converts all characters in
a string to lowercase and
uppercase appropriately</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
lowercase_str = my_string.lower() # Result: 'hello, python!'
uppercase_str = my_string.upper() # Result: 'HELLO, PYTHON!'
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">strip()</span> - removes leading and trailing whitespaces from a
string.</p>
<p><span class="highlight-text">lstrip()</span> - removes leading whitespaces.</p>
<p><span class="highlight-text">rstrip()</span> - removes trailing whitespaces</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">padded_str = " Python "
stripped_str = padded_str.strip() # Result: 'Python'
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">replace()</span> - replaces a specified substring with another
substring.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
new_str = my_string.replace("Python", "World") # Result: 'Hello, World!'
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">find()</span> - returns the index of the first occurrence of a
substring (or -1 if not found).</p>
<p><span class="highlight-text">count()</span> - returns the number of occurrences of a substring.
</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
index = my_string.find("Python") # Result: 7
occurrences = my_string.count("o") # Result: 2
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">split()</span> - splits a string into a list of substrings based on
a specified delimiter.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
words = my_string.split(",") # Result: ['Hello', ' Python!']
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">join()</span> - joins elements of a sequence (like a list) into a
string using the specified separator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">word_list = ["Hello", "Python!"]
joined_str = ",".join(word_list) # Result: 'Hello,Python!'
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">startswith() and endswith()</span> - checks if a string starts or
ends with a specified substring.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_string = "Hello, Python!"
starts_with = my_string.startswith("Hello") # Result: True
ends_with = my_string.endswith("Python") # Result: False
</code></pre>
</div>
<p class="highlight-block"><a href="https://docs.python.org/3/library/stdtypes.html#string-methods"
target="_blank" rel="noopener"
aria-label="Check Check the entire list of strings' methods in python documentation (open in new tab)">Check
the entire list of strings' methods</a></p>
<h4>String Format</h4>
<p>The most common and versatile method for string formatting is using <span
class="highlight-text">f-strings</span>.</p>
<p>An f<span class="highlight-text">-string</span> is created by prefixing a string with the letter "f"
</p>
<p>You can embed expressions inside curly braces <span class="highlight-text">{}</span> within the
string.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">name = "Alice"
age = 30
formatted_str = f"My name is {name} and I am {age} years old."
</code></pre>
<p>You can include variables, expressions, and even function calls inside curly braces.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">x = 5
y = 10
result_str = f"The sum of {x} and {y} is {x + y}."
</code></pre>
<p class="highlight-block"><a
href="https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting"
target="_blank" rel="noopener"
aria-label="Check detailed information about strings format in python documentation (open in new tab)">Check
detailed information about strings format</a></p>
</section>
<section id="list">
<h3>List <a href="https://docs.python.org/3/library/stdtypes.html#list" target="_blank" rel="noopener"
aria-label="Get detailed information about List type in python documentation (open in new tab)"><i
class="fa-solid fa-circle-info"></i></a></h3>
<p>Lists are <span class="highlight-text">ordered</span>, <span class="highlight-text">mutable</span>
sequences used to store collections of items. They are defined using square brackets.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, "apple", "orange"]
</code></pre>
<h4>Access List Items</h4>
<p>You can access individual items in a list using indexing.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, "apple", "orange"]
# Access the first item
first_item = my_list[0] # Result: 1
# Access the third item
third_item = my_list[2] # Result: 3
# Access the last item
last_item = my_list[-1] # Result: 'orange'
# Access a range of items
subset = my_list[1:4] # Result: [2, 3, 'apple']</code></pre>
<h4>Change List Items</h4>
<p>You can change the value of a specific item in a list using indexing.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, "apple", "orange"]
my_list[3] = "banana"
# Result: [1, 2, 3, "banana", "orange"]
</code></pre>
<h4>Loop Lists</h4>
<p>You can iterate through a list using a <span class="highlight-text">for</span> loop.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, 4]
for item in my_list:
print(item)
# Output: 1
# 2
# 3
# 4
</code></pre>
<h4>List Comprehension</h4>
<p>List comprehension is a concise way to create lists.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">numbers = [1, 2, 3, 4]
squared_numbers = [x**2 for x in numbers]
# Result: [1, 4, 9, 16]
</code></pre>
<h4>Join Lists</h4>
<p>You can concatenate two lists using the <span class="highlight-text">+</span> operator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined_list = list1 + list2
# Result: [1, 2, 3, 4, 5, 6]
</code></pre>
<h4>List Methods</h4>
<div class="method">
<p><span class="highlight-text">append()</span> - adds an element to the end of the list.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3]
my_list.append(4)
# Result: [1, 2, 3, 4]
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">remove()</span> - removes the first occurrence of a specified
element from the list.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, 4]
my_list.remove(3)
# Result: [1, 2, 4]
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">pop()</span> - removes and returns the last element from the list
(or a specified index).</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, 4]
last_element = my_list.pop()
# Result: [1, 2, 3], last_element: 4
second_element = my_list.pop(1) # 1 is index of second element
# Result: [1, 3], last_element: 2
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">insert()</span> - inserts an element at a specified position in the
list.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3]
my_list.insert(1, 4) # 1 is index the position and 4 is the value that will be inserted
# Result: [1, 4, 2, 3]
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">sort()</span> - sorts the elements of the list in ascending order.
</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [3, 1, 4, 1, 5, 9, 2]
my_list.sort()
# Result: [1, 1, 2, 3, 4, 5, 9]
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">reverse()</span> - reverses the order of the elements in the list.
</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, 4]
my_list.reverse()
# Result: [4, 3, 2, 1]
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">count()</span> - returns the number of occurrences of a specified
element in the list.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 2, 3, 4, 2, 5]
count_of_2 = my_list.count(2)
# Result: 3
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">extend()</span> - extends the list by appending elements from
another list.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
# Result: [1, 2, 3, 4, 5, 6]
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">index()</span> - returns the index of the first occurrence of a
specified element in the list (or raises a ValueError if not found).</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_list = [1, 2, 3, 4]
index_of_3 = my_list.index(3)
# Result: 2
</code></pre>
</div>
</section>
<section id="tuple">
<h3>Tuple <a href="https://docs.python.org/3/library/stdtypes.html#tuple" target="_blank" rel="noopener"
aria-label="Get detailed information about Tuple type in python documentation (open in new tab)"><i
class="fa-solid fa-circle-info"></i></a></h3>
<p>Tuples are <span class="highlight-text">ordered</span>, <span class="highlight-text">immutable</span>
sequences used to store collections of items. They are defined using parentheses.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_tuple = (1, 2, 3, "apple", "orange")
</code></pre>
<h4>Access Tuple Items</h4>
<p>You can access individual items in a tuple using indexing (like a list).</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_tuple = (1, 2, 3, "apple", "orange")
first_item = my_tuple[0] # Result: 1
</code></pre>
<h4>Update Tuples</h4>
<p>Tuples are immutable, meaning you cannot change the values of items once they are set.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">original_tuple = (1, 2, 3)
new_tuple = original_tuple + (4, 5)
# Result: (1, 2, 3, 4, 5)
</code></pre>
<h4>Unpack Tuples</h4>
<p>You can assign the elements of a tuple to individual variables using unpacking (list has the same
behavior).</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_tuple = (1, 2, 3)
a, b, c = my_tuple
# Variables: a=1, b=2, c=3
</code></pre>
<h4>Loop Tuples</h4>
<p>You can iterate through a tuple using a <span class="highlight-text">for</span> loop.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_tuple = (1, 2, 3, 4)
for item in my_tuple:
print(item)
# Output: 1
# 2
# 3
# 4
</code></pre>
<h4>Join Tuples</h4>
<p>You can concatenate two tuples using the <span class="highlight-text">+</span> operator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
joined_tuple = tuple1 + tuple2
# Result: (1, 2, 3, 4, 5, 6)
</code></pre>
<h4>Tuple Methods</h4>
<div class="method">
<p><span class="highlight-text">count()</span> - returns the number of occurrences of a specified
element in the tuple.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_tuple = (1, 2, 2, 3, 4, 2, 5)
count_of_2 = my_tuple.count(2)
# Result: 3
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">index()</span> - returns the index of the first occurrence of a
specified element in the tuple (or raises a ValueError if not found).</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_tuple = (1, 2, 3, 4)
index_of_3 = my_tuple.index(3)
# Result: 2
</code></pre>
</div>
</section>
<section id="dictionary">
<h3>Dictionary <a href="https://docs.python.org/3/library/stdtypes.html#dict" target="_blank"
rel="noopener"
aria-label="Get detailed information about Dictionary type in python documentation (open in new tab)"><i
class="fa-solid fa-circle-info"></i></a></h3>
<p>Dictionaries are <span class="highlight-text">unordered</span>, <span
class="highlight-text">mutable</span> collections of items. They are defined using curly braces
and consist of key-value pairs.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
</code></pre>
<h4>Accessing Dictionary Items</h4>
<p>You can access the value associated with a specific key in a dictionary.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
name_value = my_dict["name"] # Result: "John"
</code></pre>
<h4>Change Dictionary Items</h4>
<p>You can change the value associated with a specific key in a dictionary.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
my_dict["age"] = 31
# Result: {"name": "John", "age": 31, "city": "New York"}
</code></pre>
<h4>Add Dictionary Items</h4>
<p>You can add new key-value pairs to a dictionary.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30}
my_dict["city"] = "New York"
# Result: {"name": "John", "age": 30, "city": "New York"}
</code></pre>
<h4>Nested Dictionaries</h4>
<p>Dictionaries can be nested inside other dictionaries.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">nested_dict = {"person": {"name": "John", "age": 30, "city": "New York"}}
# Accessing nested value: nested_dict["person"]["age"]
</code></pre>
<h4>Dictionary Methods</h4>
<div class="method">
<p><span class="highlight-text">keys()</span> - returns a list of all the keys in the dictionary.
</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
keys_view = my_dict.keys()
# Result: dict_keys(['name', 'age', 'city'])
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">values()</span> - returns a list of all the values in the
dictionary.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
values_view = my_dict.values()
# Result: dict_values(['John', 30, 'New York'])
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">items()</span> - returns a list of all key-value pairs in the
dictionary.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
items_view = my_dict.items()
# Result: dict_items([('name', 'John'), ('age', 30), ('city', 'New York')])
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">get()</span> - returns the value for the specified key. If the key
is not found, it returns a default value.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
age_value = my_dict.get("age", 0) # 0 is default value
# Result: 30
# Alternatively: age_value = my_dict["age"] if "age" in my_dict else 0
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">pop()</span> - removes the item with the specified key and returns
its value.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
age_value = my_dict.pop("age")
# Result: age_value: 30, my_dict: {'name': 'John', 'city': 'New York'}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">popitem()</span> - removes and returns the last inserted key-value
pair.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
last_item = my_dict.popitem()
# Result: last_item: ('city', 'New York'), my_dict: {'name': 'John', 'age': 30}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">update()</span> - updates the dictionary with elements from another
dictionary or from an iterable of key-value pairs.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30}
new_data = {"city": "New York", "gender": "Male"}
my_dict.update(new_data)
# Result: {'name': 'John', 'age': 30, 'city': 'New York', 'gender': 'Male'}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">clear()</span> - removes all items from the dictionary.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_dict = {"name": "John", "age": 30, "city": "New York"}
my_dict.clear()
# Result: {}
</code></pre>
</div>
</section>
<section id="set">
<h3>Set <a href="https://docs.python.org/3/library/stdtypes.html#set" target="_blank" rel="noopener"
aria-label="Get detailed information about Set type in python documentation (open in new tab)"><i
class="fa-solid fa-circle-info"></i></a></h3>
<p>Sets are <span class="highlight-text">unordered</span>, <span class="highlight-text">mutable</span>
collections of unique items. They are defined using curly braces.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_set = {1, 1, 2, 2, 3, 4, 5}
# Notice set doesn't have any duplicate items
# Result: {1, 2, 3, 4, 5}
</code></pre>
<h4>Access Set Items</h4>
<p>You can access items in a set, but sets are unordered, so there is no index. You can check for
membership.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_set = {1, 2, 3, 4, 5}
is_present = 3 in my_set # Result: True
</code></pre>
<h4>Loop Sets</h4>
<p>You can iterate through a set using a <span class="highlight-text">for</span> loop.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_set = {1, 2, 3, 4}
for item in my_set:
print(item)
# Output: 1
# 2
# 3
# 4
</code></pre>
<h4>Set Methods</h4>
<div class="method">
<p><span class="highlight-text">add()</span> - adds an element to the set.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_set = {1, 2, 3}
my_set.add(4)
# Result: {1, 2, 3, 4}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">remove()</span> - removes the specified element from the set.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_set = {1, 2, 3, 4}
my_set.remove(3)
# Result: {1, 2, 4}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">discard()</span> - removes the specified element from the set if
present.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_set = {1, 2, 3, 4}
my_set.discard(2)
# Result: {1, 3, 4}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">union()</span> - returns a new set containing the union of sets.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">set1 = {1, 2, 3}
set2 = {3, 4, 5}
joined_set = set1.union(set2)
# Result: {1, 2, 3, 4, 5}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">|</span> - returns a new set containing the union of sets using the
| operator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">set1 = {1, 2, 3}
set2 = {3, 4, 5}
joined_set = set1 | set2
# Result: {1, 2, 3, 4, 5}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">difference()</span> - returns a new set containing the difference
between two or more sets.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
diff_set = set1.difference(set2)
# Result: {1, 2}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">-</span> - returns a new set containing the difference between sets
using the - operator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
diff_set = set1 - set2
# Result: {1, 2}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">intersection()</span> - returns a new set containing the
intersection of two or more sets.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
# Result: {3, 4}
</code></pre>
</div>
<div class="method">
<p><span class="highlight-text">&</span> - returns a new set containing the intersection of sets
using the & operator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
intersection_set = set1 & set2
# Result: {3, 4}
</code></pre>
</div>
<p class="highlight-block"><a href="https://docs.python.org/3/library/stdtypes.html#set" target="_blank"
rel="noopener"
aria-label="Check the entire list of set methods in python documentation (open in new tab)">Check
the entire list of set methods</a></p>
</section>
<section id="boolean">
<h3>Boolean</h3>
<h4>Boolean Values</h4>
<p>Boolean values in Python are <span class="highlight-text">True</span> and <span
class="highlight-text">False</span>.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_boolean = True
another_boolean = False
</code></pre>
<h4>Evaluate Values and Variables</h4>
<p>Values and variables can be evaluated in Boolean context, resulting in either <span
class="highlight-text">True</span> or <span class="highlight-text">False</span>.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">x = 5
y = 10
is_greater = x > y # Result: False
</code></pre>
<h4>Most Values are True</h4>
<p>In a Boolean context, most values are considered <span class="highlight-text">True</span>. Examples
include non-zero numbers, non-empty strings, and non-empty lists.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">value = 42
is_true = bool(value) # Result: True
</code></pre>
<h4>Some Values are False</h4>
<p>Some values are considered <span class="highlight-text">False</span> in a Boolean context. Examples
include zero, empty strings, and empty lists.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">empty_list = []
is_false = bool(empty_list) # Result: False
</code></pre>
<h4>Functions can Return a Boolean</h4>
<p>Functions can return Boolean values, allowing for conditional checks based on their results.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">def is_even(number):
return number % 2 == 0
result = is_even(4) # Result: True
</code></pre>
</section>
<section id="none">
<h3>Python NoneType</h3>
<p>In Python, <span class="highlight-text">None</span> is a special constant representing the absence of
a value or a null value. It is often used to signify that a variable or a function does not have a
meaningful value or result.</p>
<h4>Assignment of None</h4>
<pre><span class="lang-name">Python</span><code class="hljs language-python">my_variable = None
</code></pre>
<h4>Function Return Values</h4>
<p>Functions in Python can explicitly return <span class="highlight-text">None</span> to indicate that
they don't produce any meaningful result.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">def do_something():
# code to do something
return None
</code></pre>
<h4>Default Function Arguments</h4>
<p><span class="highlight-text">None</span> is commonly used as a default value for function parameters
when the absence of a user-provided value is meaningful.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">def greet(name=None):
if name is None:
print("Hello, anonymous!")
else:
print(f"Hello, {name}!")
greet() # Output: Hello, anonymous!
greet("John") # Output: Hello, John!
</code></pre>
<h4>Checking for None</h4>
<p>You can check if a variable is <span class="highlight-text">None</span> using the is operator.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">if some_variable is None:
print("The variable is None.")
else:
print("The variable has a value.")
</code></pre>
<h4>Comparisons</h4>
<p><span class="highlight-text">None</span> is considered equal to itself and evaluates to <span
class="highlight-text">False</span> in a boolean context.</p>
<pre><span class="lang-name">Python</span><code class="hljs language-python">x = None
y = None
print(x == y) # Output: True
print(bool(None)) # Output: False
</code></pre>
</section>
</section>
</main>
<footer>
<ul id="useful-links">
<li>
<a href="https://docs.python.org/3/" target="_blank" rel="noopener"
aria-label="Visit visit python3 documentation (open in new tab)">
Python3 documentation
</a>
</li>
<li>