-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.json
More file actions
1607 lines (1587 loc) · 57.8 KB
/
data.json
File metadata and controls
1607 lines (1587 loc) · 57.8 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
{
"topics": [
{
"title": "What is Python?",
"url": "https://www.youtube.com/watch?v=71DMPhmxOS4",
"content": [
{
"subsection": "Introduction to Python",
"description": "Python is a high-level scripting language used for text processing, system administration, and internet-related tasks. It has a small core language that's easy to master, with extensive module support for various tasks. Python is object-oriented and available on multiple platforms."
},
{"image": "memes/programming.jpg"},
{
"subsection": "Basic Principles of Python",
"description": "Python incorporates features found in more complex languages from its inception, making it beginner-friendly yet powerful for advanced users.",
"features": [
"High-level scripting language",
"True object-oriented",
"Extensible with modules"
]
}
]
},
{
"title": "Basic Code Examples",
"content": [
{
"subsection": "Printing 'Hello, World!'",
"code": {
"language": "python",
"content": [
"print('hello, world')"
]
}
},
{
"subsection": "Calculating Tax",
"description": "Calculating tax based on cost and tax rate.",
"code": {
"language": "python",
"content": [
"cost = 27.00",
"taxrate = 0.075",
"print(cost * taxrate)"
]
}
},
{
"subsection": "Basic Arithmetic",
"description": "Performing basic arithmetic operations.",
"code": {
"language": "python",
"content": [
"print(16 + 25 + 92 * 3)"
]
}
} , { "image":"memes/helloworld.png"}
],
"url": "https://www.youtube.com/watch?v=SFkXX6vgs3k"
},
{
"title": "Data Types",
"url": "https://www.youtube.com/watch?v=LKFrQXaoSMQ",
"content": [
{
"subsection": "Introduction to Data Types",
"description": "Python has several built-in data types that are used to define the operations that can be done on them and the storage method for each of them. The basic data types include:",
"list": [
"`int`: Integer, a whole number without decimals.",
"`float`: Floating-point number, a number that has both an integer and fractional part, separated by a decimal point.",
"`complex`: Complex number, represented as `a + bj`, where `a` and `b` are real numbers and `j` is the imaginary unit.",
"`str`: String, a sequence of characters enclosed in quotes (`'` or `\"`).",
"`bool`: Boolean, represents `True` or `False` values.",
"`list`: List, a collection of items that are ordered and mutable.",
"`tuple`: Tuple, a collection of items that are ordered and immutable.",
"`set`: Set, a collection of unordered and unindexed unique items.",
"`dict`: Dictionary, a collection of key-value pairs that are unordered, mutable, and indexed."
]
},
{
"subsection": "Numeric Data Types",
"description": "Python supports three numerical data types:",
"list": [
"`int`: Integer, positive or negative whole numbers (without a fractional part).",
"`float`: Floating-point numbers, used for real numbers with a decimal point.",
"`complex`: Complex numbers, represented as `a + bj`, where `a` and `b` are real numbers and `j` is the imaginary unit."
]
},
{"image": "memes/datatypes.png"},
{
"subsection": "Text Data Type",
"description": "The `str` type in Python represents text and is enclosed in either single (`'`) or double (`\"`) quotes.",
"code": {
"language": "python",
"content": [
"message = 'Hello, World!'",
"print(message)"
]
}
},
{
"subsection": "Boolean Data Type",
"description": "`bool` is a built-in data type in Python used to represent boolean values, `True` or `False`.",
"code": {
"language": "python",
"content": [
"is_valid = True",
"print(is_valid)"
]
}
},
{
"subsection": "Sequence Data Types",
"description": "Python supports three sequence data types:",
"list": [
"`list`: Ordered and mutable collection of items.",
"`tuple`: Ordered and immutable collection of items.",
"`range`: Immutable sequence of numbers generated by `range()` function."
]
},
{
"subsection": "Mapping Data Type",
"description": "The `dict` type in Python represents a collection of key-value pairs.",
"code": {
"language": "python",
"content": [
"person = {",
" 'name': 'John',",
" 'age': 30",
"}",
"print(person)"
]
}
},
{
"subsection": "Set Data Type",
"description": "A `set` is an unordered and unindexed collection of unique items.",
"code": {
"language": "python",
"content": [
"fruits = {'apple', 'banana', 'cherry'}",
"print(fruits)"
]
}
},
{
"subsection": "Type Conversion",
"description": "Python allows conversion between different data types using constructors like `int()`, `float()`, `str()`, etc.",
"code": {
"language": "python",
"content": [
"x = int(3.5)",
"y = float(5)",
"z = str(10)",
"print(x, y, z)"
]
}
},
{
"subsection": "MCQs",
"description": "",
"mcq": [
{
"question": "Which of these is not a core data type?",
"options": [
"Lists",
"Dictionary",
"Tuples",
"Class"
],
"answer": "Class"
},
{
"question": "Given a function that does not return any value, what value is thrown by default when executed in the shell?",
"options": [
"int",
"bool",
"void",
"None"
],
"answer": "None"
},
{
"question": "What is the return type of the function `id`?",
"options": [
"int",
"float",
"bool",
"dict"
],
"answer": "int"
},
{
"question": "What data type is the object below? `L = [1, 23, 'hello', 1]`",
"options": [
"list",
"dictionary",
"array",
"tuple"
],
"answer": "list"
},
{
"question": "Which data type in Python is used to store a sequence of characters?",
"options": [
"Integer",
"Float",
"String",
"Boolean"
],
"answer": "String"
}
]
}, { "image": "memes/integer.webp"}
]
},
{
"title": "Variables",
"url": "https://www.youtube.com/watch?v=dHcCbxTfwoU",
"content": [
{
"subsection": "Introduction to Variables",
"description": "In programming, variables are used to store data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.",
"code": {
"language": "python",
"content": [
"x = 5",
"y = 'Hello, World!'",
"print(x)",
"print(y)"
]
}
},
{
"subsection": "Variable Names",
"image": "memes/variable.jpg",
"description": "A variable name must start with a letter or the underscore character. \nA variable name cannot start with a number. Variable names are case-sensitive (age, Age, and AGE are three different variables).",
"code": {
"language": "python",
"content": [
"myvar = 'John'",
"my_var = 'John'",
"_my_var = 'John'",
"myVar = 'John'",
"MYVAR = 'John'",
"myvar2 = 'John'"
]
}
},
{},
{
"subsection": "Variable Types",
"description": "Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:",
"list": [
"Text Type: `str`",
"Numeric Types: `int`, `float`, `complex`",
"Sequence Types: `list`, `tuple`, `range`",
"Mapping Type: `dict`",
"Set Types: `set`, `frozenset`",
"Boolean Type: `bool`",
"Binary Types: `bytes`, `bytearray`, `memoryview`"
]
},
{
"subsection": "Global Variables",
"description": "Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.",
"code": {
"language": "python",
"content": [
"x = 'global'",
"",
"def myfunc():",
" print('Python is ' + x)",
"",
"myfunc()",
"",
"print('Python is ' + x)"
]
}
},
{
"subsection": "Local Variables",
"description": "Variables that are created inside a function are known as local variables. Local variables can only be used inside the function where they were created.",
"code": {
"language": "python",
"content": [
"def myfunc():",
" y = 'local'",
" print('Python is ' + y)",
"",
"myfunc()",
"",
"# This will cause an error:",
"# print('Python is ' + y)"
]
}
},
{
"subsection": "MCQs",
"description": "",
"mcq": [
{
"question": "What is a variable in Python?",
"options": [
"A reserved word",
"A data type",
"A location in memory to store data",
"A function"
],
"answer": "A location in memory to store data"
},
{
"question": "How do you declare a variable in Python?",
"options": [
"var x",
"x = variable",
"declare x",
"x = 4"
],
"answer": "x = 4"
},
{
"question": "What is the correct way to comment a single line in Python?",
"options": [
"// This is a comment",
"# This is a comment",
"/* This is a comment */",
"-- This is a comment"
],
"answer": "# This is a comment"
},
{
"question": "Which of the following is a valid variable name in Python?",
"options": [
"1variable",
"my_variable",
"global",
"variable-1"
],
"answer": "my_variable"
},
{
"question": "How do you swap the values of two variables in Python without using a third variable?",
"options": [
"x = y; y = x",
"x, y = y, x",
"temp = x; x = y; y = temp",
"x + y; y = x; x = y"
],
"answer": "x, y = y, x"
}
]
}
]
},
{
"title": "Booleans",
"content": [
{
"subsection": "Introduction to Booleans",
"description": "In programming you often need to know if an expression is `True` or `False`. You can evaluate any expression in Python, and get one of two answers, `True` or `False`.",
"code": {
"language": "python",
"content": [
"print(10 > 9)",
"print(10 == 9)",
"print(10 < 9)"
]
}
},
{"image": "memes/boolean.webp"},
{
"subsection": "Boolean Values",
"description": "In Python, `True` and `False` are Boolean values (note the uppercase T and F).",
"code": {
"language": "python",
"content": [
"print(bool('Hello'))",
"print(bool(15))"
]
}
},
{
"subsection": "Evaluate Values and Variables",
"description": "Almost any value is evaluated to `True` if it has some sort of content. Any string is `True`, except empty strings.",
"code": {
"language": "python",
"content": [
"bool('abc')",
"bool(123)",
"bool(['apple', 'cherry', 'banana'])"
]
}
},
{
"subsection": "Some Values are `False`",
"description": "In Python, empty values like `''`, `0`, `None`, and empty lists (`[]` and `{}`) are evaluated as `False`.",
"code": {
"language": "python",
"content": [
"bool(False)",
"bool(None)",
"bool(0)",
"bool('')",
"bool([])",
"bool({})"
]
}
},
{
"subsection": "MCQs",
"description": "",
"mcq": [
{
"question": "What will be the output of the following Python code snippet?\n\nbool('False')\nbool()",
"options": [
"True\nTrue",
"False\nTrue",
"False\nFalse",
"True\nFalse"
],
"answer": "True\nFalse"
},
{
"question": "What will be the output of the following Python code snippet?\n\n['hello', 'morning'][bool('')]",
"options": [
"error",
"no output",
"hello",
"morning"
],
"answer": "morning"
},
{
"question": "What will be the output of the following Python code snippet?\n\nnot(3>4)\nnot(1&1)",
"options": [
"True\nTrue",
"True\nFalse",
"False\nTrue",
"False\nFalse"
],
"answer": "True\nFalse"
},
{
"question": "What will be the output of the following Python code?\n\n['f', 't'][bool('spam')]",
"options": [
"t",
"f",
"No output",
"Error"
],
"answer": "t"
},
{
"question": "What will be the output of the following Python code?\n\nl=[1, 0, 2, 0, 'hello', '', []]\nlist(filter(bool, l))",
"options": [
"Error",
"[1, 0, 2, 0, 'hello', '', []]",
"[1, 0, 2, 'hello', '', []]",
"[1, 2, 'hello']"
],
"answer": "[1, 2, 'hello']"
}
]
}
]
},
{
"title": "Input Function",
"url": "https://www.youtube.com/watch?v=DB9Cq6TSTuQ",
"content": [
{
"subsection": "Introduction to Input Function",
"description": "The `input()` function allows user input. Always returns a string.",
"code": {
"language": "python",
"content": [
"username = input('Enter username:')",
"print('Username is: ' + username)"
]
}
},
{
"subsection": "Prompting User for Input",
"description": "You can use the `input()` function with a prompt message to guide the user on what to input.",
"code": {
"language": "python",
"content": [
"name = input('Enter your name: ')",
"print('Hello, ' + name)"
]
}
},
{
"subsection": "Converting Input to Integer or Float",
"description": "To use the input as an integer or float, you must convert it explicitly using `int()` or `float()`.",
"code": {
"language": "python",
"content": [
"age = int(input('Enter your age: '))",
"print('You are', age, 'years old.')"
]
}
},
{
"subsection": "Handling User Input",
"description": "User input can be stored in variables and used throughout your program.",
"code": {
"language": "python",
"content": [
"name = input('Enter your name: ')",
"age = int(input('Enter your age: '))",
"print('Hello, ' + name + '! You are', age, 'years old.')"
]
}
}
]
},
{
"title": "Comments",
"content": [
{
"subsection": "Single Lined Comments",
"description": "Single-line comments start with a hash symbol (#) and extend to the end of the line. For example:",
"code": {
"language": "python",
"content": [
"# this is a single-lined comment"
]
}
},
{
"subsection": "Multi-Lined Comments",
"description": "Multi-line comments start with three double quotes (\"\"\") and end with three double quotes (\"\"\"). For example:",
"code": {
"language": "python",
"content": [
"\"\"\"\nThis is a multi-line comment.\n\nIt can span multiple lines.\n\"\"\""
]
}
},
{"image": "memes/comments.webp"},
{
"subsection": "MCQs",
"description": "",
"mcq": [
{
"question": "How much space is occupied after the compilation of a Python script by comments?",
"options": [
"1 Byte",
"1 KB",
"1 MB",
"Zero Byte"
],
"answer": "Zero Byte"
},
{
"question": "In a Python script, where can you write a comment line?",
"options": [
"Start of code",
"End of code",
"In between",
"Anywhere"
],
"answer": "Anywhere"
},
{
"question": "Which of the following is a use of comments in Python?",
"options": [
"Readability",
"Descriptive",
"Program Information",
"All of these"
],
"answer": "All of these"
},
{
"question": "Non-executable statements in Python are called:",
"options": [
"Directives",
"Functions",
"Comments",
"Control Structures"
],
"answer": "Comments"
},
{
"question": "In Python programming, which of the following symbols is used for writing comments?",
"options": [
"//",
"/",
"$",
"#"
],
"answer": "#"
}
]
}, {"image": "memes/comments.jpg"}
]
},
{
"title": "Strings",
"url": "https://www.youtube.com/watch?v=crw3rVFNwIM",
"content": [
{
"subsection": "Creating Strings",
"description": "Strings can be created by enclosing characters in single quotes (`'`), double quotes (`\"`), triple single quotes (`'''`), or triple double quotes (`\"\"\"`),.",
"code": {
"language": "python",
"content": [
"single_quoted_string = 'Hello, World!'",
"double_quoted_string = \"Hello, World!\"",
"triple_single_quoted_string = '''Hello,\nWorld!'''",
"triple_double_quoted_string = \"\"\"Hello,\nWorld!\"\"\""
]
}
},
{
"subsection": "Accessing Characters in a String",
"description": "You can access individual characters in a string using indexing, where the index starts at 0.",
"code": {
"language": "python",
"content": [
"greeting = \"Hello, World!\"",
"print(greeting[0]) # Output: H",
"print(greeting[7]) # Output: W"
]
}
},
{
"subsection": "Slicing Strings",
"description": "Slicing allows you to obtain a substring by specifying a range of indices. The syntax for slicing is `string[start:end]`.",
"code": {
"language": "python",
"content": [
"greeting = \"Hello, World!\"",
"print(greeting[0:5]) # Output: Hello",
"print(greeting[7:12]) # Output: World"
]
}
},
{
"subsection": "String Methods",
"description": "Python provides several built-in methods to manipulate strings. Here are some commonly used methods:",
"list": [
"lower(): Converts all characters to lowercase.",
"upper(): Converts all characters to uppercase.",
"strip(): Removes leading and trailing whitespace.",
"replace(old, new): Replaces all occurrences of a substring with another substring.",
"split(delimiter): Splits the string into a list of substrings based on the specified delimiter."
],
"code": {
"language": "python",
"content": [
"text = \" Hello, World! \"",
"# Convert to lowercase",
"print(text.lower()) # Output: hello, world!",
"# Convert to uppercase",
"print(text.upper()) # Output: HELLO, WORLD!",
"# Strip whitespace",
"print(text.strip()) # Output: Hello, World!",
"# Replace a substring",
"print(text.replace(\"World\", \"Python\")) # Output: Hello, Python!",
"# Split the string",
"print(text.split(\",\")) # Output: [' Hello', ' World! ']"
]
}
},
{"image": "memes/string.webp"},
{
"subsection": "String Concatenation",
"description": "You can concatenate strings using the `+` operator or by using the `join()` method.",
"code": {
"language": "python",
"content": [
"# Using the + operator",
"greeting = \"Hello\" + \", \" + \"World!\"",
"print(greeting) # Output: Hello, World!",
"# Using the join() method",
"words = [\"Hello\", \"World\"]",
"greeting = \", \".join(words)",
"print(greeting) # Output: Hello, World"
]
}
},
{
"subsection": "String Formatting",
"description": "Python provides several ways to format strings, making it easy to create and manage strings that include variables. Common methods include the `format()` method and f-strings (formatted string literals).",
"code": {
"language": "python",
"content": [
"name = \"Alice\"",
"age = 25",
"# Using the format() method",
"greeting = \"Hello, my name is {} and I am {} years old.\".format(name, age)",
"print(greeting) # Output: Hello, my name is Alice and I am 25 years old.",
"# Using f-strings (available in Python 3.6+)",
"greeting = f\"Hello, my name is {name} and I am {age} years old.\"",
"print(greeting) # Output: Hello, my name is Alice and I am 25 years old."
]
}
},
{
"subsection": "Multi-line Strings",
"description": "Multi-line strings are created using triple quotes (`'''` or `\"\"\"`). They are often used for documentation (docstrings) or to include large blocks of text in your code.",
"code": {
"language": "python",
"content": [
"multi_line_string = \"\"\"This is a multi-line string.\n\nIt can span multiple lines.\n\nIt is very useful for large blocks of text.\"\"\"",
"print(multi_line_string)"
]
}
},
{
"subsection": "MCQs",
"description": "",
"mcq": [
{
"question": "What does the following Python code snippet do?\n\nstring = \"Hello, World!\"\nprint(string.upper())",
"options": [
"a) Prints “Hello, World!” in lowercase",
"b) Converts “Hello, World!” to uppercase and prints it",
"c) Reverses the string “Hello, World!”",
"d) Removes all whitespace characters from the string “Hello, World!”"
],
"answer": "b"
},
{
"question": "How can you concatenate two strings in Python?",
"options": [
"a) Using the concat() method",
"b) Using the join() method",
"c) Using the & operator",
"d) Using the + operator"
],
"answer": "d"
},
{
"question": "What is the output of the following Python code snippet?\n\nstring = \"Hello, World!\"\nprint(string[3:7])",
"options": [
"a) “Hello”",
"b) “lo, “",
"c) “lo, W”",
"d) “lo, World”"
],
"answer": "b"
},
{
"question": "Which method is used to split a string into a list of substrings based on a delimiter in Python?",
"options": [
"a) split()",
"b) substring()",
"c) separate()",
"d) divide()"
],
"answer": "a"
},
{
"question": "What does the strip() method do in Python string manipulation?",
"options": [
"a) Removes all whitespace characters from both ends of the string",
"b) Removes all characters except alphabets from the string",
"c) Converts the string to uppercase",
"d) Converts the string to lowercase"
],
"answer": "a"
}
]
}
]
},
{
"title": "Lists",
"url": "https://www.youtube.com/watch?v=gOMW_n2-2Mw",
"content": [
{
"subsection": "Creating Lists",
"description": "Lists are created by enclosing comma-separated values within square brackets (`[]`).",
"code": {
"language": "python",
"content": [
"empty_list = []",
"numbers = [1, 2, 3, 4, 5]",
"fruits = ['apple', 'banana', 'cherry']",
"mixed_list = [1, 'hello', 3.5, True]"
]
}
},
{
"subsection": "Accessing Elements in a List",
"description": "You can access individual elements in a list using indexing, where the index starts at 0.",
"code": {
"language": "python",
"content": [
"fruits = ['apple', 'banana', 'cherry']",
"print(fruits[0]) # Output: apple",
"print(fruits[2]) # Output: cherry"
]
}
},
{
"subsection": "Slicing Lists",
"description": "Slicing allows you to obtain a sublist by specifying a range of indices. The syntax for slicing is `list[start:end]`.",
"code": {
"language": "python",
"content": [
"numbers = [1, 2, 3, 4, 5]",
"print(numbers[1:4]) # Output: [2, 3, 4]"
]
}
},
{
"subsection": "List Methods",
"description": "Python provides several built-in methods to manipulate lists. Here are some commonly used methods:",
"list": [
"append(item): Adds an item to the end of the list.",
"extend(iterable): Extends the list by appending all elements from the iterable.",
"insert(index, item): Inserts an item at a specified position.",
"remove(item): Removes the first occurrence of the item from the list.",
"pop(index): Removes and returns the item at the specified index.",
"index(item): Returns the index of the first occurrence of the item.",
"count(item): Returns the number of occurrences of the item in the list.",
"sort(): Sorts the list in ascending order.",
"reverse(): Reverses the elements of the list."
],
"code": {
"language": "python",
"content": [
"numbers = [3, 1, 4, 1, 5, 9, 2]",
"",
"# Append an item",
"numbers.append(6)",
"print(numbers) # Output: [3, 1, 4, 1, 5, 9, 2, 6]",
"",
"# Remove an item",
"numbers.remove(1)",
"print(numbers) # Output: [3, 4, 1, 5, 9, 2, 6]",
"",
"# Sort the list",
"numbers.sort()",
"print(numbers) # Output: [1, 2, 3, 4, 5, 6, 9]",
""
]
}
},
{
"subsection": "List Comprehensions",
"description": "List comprehensions provide a concise way to create lists. They consist of an expression followed by a `for` clause, optionally followed by `if` clauses.",
"code": {
"language": "python",
"content": [
"numbers = [1, 2, 3, 4, 5]",
"squared_numbers = [x**2 for x in numbers if x % 2 == 0]",
"print(squared_numbers) # Output: [4, 16]"
]
}
},
{
"subsection": "Nested Lists",
"description": "Lists can contain other lists as elements, allowing for the creation of nested data structures.",
"code": {
"language": "python",
"content": [
"matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]",
"print(matrix[1][1]) # Output: 5"
]
}
},
{
"subsection": "List Mutability and Immutability",
"description": "While lists themselves are mutable (elements can be changed), if a list contains immutable elements (like tuples), those elements cannot be changed.",
"code": {
"language": "python",
"content": [
"tuple_in_list = [(1, 2), (3, 4)]",
"tuple_in_list[0] = (5, 6) # Valid operation",
"tuple_in_list[0][0] = 7 # Invalid operation (tuple elements are immutable)"
]
}
},
{
"subsection": "List vs Tuple",
"description": "Lists and tuples are both used to store collections of items, but tuples are immutable while lists are mutable.",
"code": {
"language": "python",
"content": [
"my_list = [1, 2, 3]",
"my_tuple = (1, 2, 3)",
"# Mutating a list",
"my_list[0] = 4",
"print(my_list) # Output: [4, 2, 3]",
"# Attempting to mutate a tuple",
"my_tuple[0] = 4 # This will cause an error"
]
}
},
{
"subsection": "List Aliasing and Copying",
"description": "Assigning one list to another creates an alias, meaning both variables refer to the same list. To create a separate copy, you can use slicing or the `copy()` method.",
"code": {
"language": "python",
"content": [
"list1 = [1, 2, 3]",
"list2 = list1 # list2 is an alias of list1",
"list1[0] = 4",
"print(list2) # Output: [4, 2, 3]",
"# Creating a copy",
"list3 = list1.copy()",
"list1[1] = 5",
"print(list1) # Output: [4, 5, 3]",
"print(list3) # Output: [4, 2, 3]"
]
}
},
{"image": "memes/concatinate_list.jpg"},
{
"subsection": "List Iteration Techniques",
"description": "Python offers multiple techniques to iterate over lists, including `for` loops, `enumerate()`, `zip()`, and list comprehensions.",
"code": {
"language": "python",
"content": [
"numbers = [1, 2, 3, 4, 5]",
"# Using a for loop",
"for num in numbers:",
" print(num)",
"# Using enumerate()",
"for index, num in enumerate(numbers):",
" print(index, num)",
"# Using zip()",
"fruits = ['apple', 'banana', 'cherry']",
"for fruit, num in zip(fruits, numbers):",
" print(fruit, num)"
]
}
},
{
"subsection": "List Memory Management",
"description": "Understanding how Python manages memory for lists, especially when dealing with large lists or appending elements, is crucial for optimizing performance.",
"code": {
"language": "python",
"content": [
"import sys",
"numbers = list(range(1000000))",
"print(sys.getsizeof(numbers)) # Output: size in bytes"
]
}
}
]
},
{
"title": "Tuples",
"url": "https://www.youtube.com/watch?v=w6hL_dszMxk",
"content": [
{
"subsection": "Creating Tuples",
"description": "Tuples are created by enclosing comma-separated values within parentheses (`()`).",
"code": {
"language": "python",
"content": [
"mytuple = ('apple', 'banana', 'cherry')",
"print(mytuple)"
]
}
},
{
"subsection": "Tuple Characteristics",
"description": "Tuple items are ordered, unchangeable, and allow duplicate values. They are indexed, and the first item has index [0], the second item has index [1], and so on.",
"code": {
"language": "python",
"content": [
"thistuple = ('apple', 'banana', 'cherry', 'apple', 'cherry')",
"print(thistuple)"
]
}
},
{
"subsection": "Tuple Length",
"description": "To determine how many items a tuple has, use the `len()` function.",
"code": {
"language": "python",
"content": [
"thistuple = ('apple', 'banana', 'cherry')",
"print(len(thistuple))"
]
}
},
{
"subsection": "Create Tuple With One Item",
"description": "To create a tuple with only one item, add a comma after the item.",