WalidAlHassan commited on
Commit
0a1fe39
·
1 Parent(s): d48316b
Functions_And_Dictionaries.ipynb → Dictionaries.ipynb RENAMED
@@ -2,16 +2,14 @@
2
  "cells": [
3
  {
4
  "cell_type": "markdown",
5
- "metadata": {
6
- "id": "winJJbiUiIIO"
7
- },
8
  "source": [
9
- "Data structures: Dictionaries"
10
  ]
11
  },
12
  {
13
  "cell_type": "code",
14
- "execution_count": 1,
15
  "metadata": {
16
  "id": "yih6CE4RiIrl"
17
  },
@@ -20,83 +18,36 @@
20
  "Dictionaries = {\"key1\":\"value1\"}"
21
  ]
22
  },
23
- {
24
- "cell_type": "markdown",
25
- "metadata": {
26
- "id": "56RkusxohJDa"
27
- },
28
- "source": [
29
- "Create Dictionaries"
30
- ]
31
- },
32
  {
33
  "cell_type": "code",
34
- "execution_count": 2,
35
  "metadata": {
36
  "id": "VLkxduR8hSQ_"
37
  },
38
- "outputs": [],
39
- "source": [
40
- "dic = {\n",
41
- " \"brand\": \"Ford\",\n",
42
- " \"model\": \"Mustang\",\n",
43
- " \"year\": 1964\n",
44
- "}"
45
- ]
46
- },
47
- {
48
- "cell_type": "code",
49
- "execution_count": 3,
50
- "metadata": {
51
- "colab": {
52
- "base_uri": "https://localhost:8080/"
53
- },
54
- "id": "Ihvoijply3U5",
55
- "outputId": "9f69778d-96a9-40b7-c1b9-b9feb4b0bc6d"
56
- },
57
- "outputs": [
58
- {
59
- "name": "stdout",
60
- "output_type": "stream",
61
- "text": [
62
- "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n"
63
- ]
64
- }
65
- ],
66
- "source": [
67
- "print(dic)"
68
- ]
69
- },
70
- {
71
- "cell_type": "code",
72
- "execution_count": 4,
73
- "metadata": {
74
- "colab": {
75
- "base_uri": "https://localhost:8080/"
76
- },
77
- "id": "dCW1nOWDiNRW",
78
- "outputId": "693b217a-6e52-4b7c-f054-9c34119daa31"
79
- },
80
  "outputs": [
81
  {
82
  "name": "stdout",
83
  "output_type": "stream",
84
  "text": [
85
  "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n",
86
- "3\n",
87
  "<class 'dict'>\n"
88
  ]
89
  }
90
  ],
91
  "source": [
 
 
 
 
 
 
92
  "print(dic)\n",
93
- "print(len(dic))\n",
94
  "print(type(dic))"
95
  ]
96
  },
97
  {
98
  "cell_type": "code",
99
- "execution_count": 5,
100
  "metadata": {
101
  "colab": {
102
  "base_uri": "https://localhost:8080/"
@@ -118,18 +69,9 @@
118
  "print(dic)"
119
  ]
120
  },
121
- {
122
- "cell_type": "markdown",
123
- "metadata": {
124
- "id": "k95vij2nkKLS"
125
- },
126
- "source": [
127
- "Accessing Items"
128
- ]
129
- },
130
  {
131
  "cell_type": "code",
132
- "execution_count": 6,
133
  "metadata": {
134
  "colab": {
135
  "base_uri": "https://localhost:8080/"
@@ -147,13 +89,14 @@
147
  }
148
  ],
149
  "source": [
 
150
  "x = dic[\"year\"]\n",
151
  "print(x)"
152
  ]
153
  },
154
  {
155
  "cell_type": "code",
156
- "execution_count": 7,
157
  "metadata": {
158
  "colab": {
159
  "base_uri": "https://localhost:8080/",
@@ -169,7 +112,7 @@
169
  "'Mustang'"
170
  ]
171
  },
172
- "execution_count": 7,
173
  "metadata": {},
174
  "output_type": "execute_result"
175
  }
@@ -179,18 +122,9 @@
179
  "x"
180
  ]
181
  },
182
- {
183
- "cell_type": "markdown",
184
- "metadata": {
185
- "id": "aKSZoGH-iYTm"
186
- },
187
- "source": [
188
- "keys()"
189
- ]
190
- },
191
  {
192
  "cell_type": "code",
193
- "execution_count": 8,
194
  "metadata": {
195
  "colab": {
196
  "base_uri": "https://localhost:8080/"
@@ -203,18 +137,21 @@
203
  "name": "stdout",
204
  "output_type": "stream",
205
  "text": [
206
- "dict_keys(['brand', 'model', 'year'])\n"
 
207
  ]
208
  }
209
  ],
210
  "source": [
 
211
  "x = dic.keys()\n",
212
- "print(x)"
 
213
  ]
214
  },
215
  {
216
  "cell_type": "code",
217
- "execution_count": 9,
218
  "metadata": {
219
  "colab": {
220
  "base_uri": "https://localhost:8080/"
@@ -224,24 +161,24 @@
224
  },
225
  "outputs": [
226
  {
227
- "data": {
228
- "text/plain": [
229
- "dict_values(['Ford', 'Mustang', 1964])"
230
- ]
231
- },
232
- "execution_count": 9,
233
- "metadata": {},
234
- "output_type": "execute_result"
235
  }
236
  ],
237
  "source": [
 
238
  "x = dic.values()\n",
239
- "x"
 
240
  ]
241
  },
242
  {
243
  "cell_type": "code",
244
- "execution_count": 10,
245
  "metadata": {
246
  "colab": {
247
  "base_uri": "https://localhost:8080/"
@@ -251,33 +188,24 @@
251
  },
252
  "outputs": [
253
  {
254
- "data": {
255
- "text/plain": [
256
- "dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])"
257
- ]
258
- },
259
- "execution_count": 10,
260
- "metadata": {},
261
- "output_type": "execute_result"
262
  }
263
  ],
264
  "source": [
 
265
  "x = dic.items()\n",
266
- "x"
267
- ]
268
- },
269
- {
270
- "cell_type": "markdown",
271
- "metadata": {
272
- "id": "QNjHr3FxhhrI"
273
- },
274
- "source": [
275
- "Add Items"
276
  ]
277
  },
278
  {
279
  "cell_type": "code",
280
- "execution_count": 11,
281
  "metadata": {
282
  "colab": {
283
  "base_uri": "https://localhost:8080/"
@@ -287,65 +215,35 @@
287
  },
288
  "outputs": [
289
  {
290
- "data": {
291
- "text/plain": [
292
- "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white'}"
293
- ]
294
- },
295
- "execution_count": 11,
296
- "metadata": {},
297
- "output_type": "execute_result"
298
  }
299
  ],
300
  "source": [
 
 
 
 
 
 
 
 
301
  "dic[\"color\"] = \"white\"\n",
302
- "dic"
303
- ]
304
- },
305
- {
306
- "cell_type": "code",
307
- "execution_count": 12,
308
- "metadata": {
309
- "colab": {
310
- "base_uri": "https://localhost:8080/"
311
- },
312
- "id": "df2cHHGInqBE",
313
- "outputId": "6c8a7c34-fad7-4075-ca3e-26fb94818e55"
314
- },
315
- "outputs": [
316
- {
317
- "data": {
318
- "text/plain": [
319
- "{'brand': 'Ford',\n",
320
- " 'model': 'Mustang',\n",
321
- " 'year': 1964,\n",
322
- " 'color': 'white',\n",
323
- " 'Power': '315 HP'}"
324
- ]
325
- },
326
- "execution_count": 12,
327
- "metadata": {},
328
- "output_type": "execute_result"
329
- }
330
- ],
331
- "source": [
332
- "d= {\"Power\":\"315 HP\"}\n",
333
  "dic.update(d)\n",
334
- "dic"
335
- ]
336
- },
337
- {
338
- "cell_type": "markdown",
339
- "metadata": {
340
- "id": "MQQ-FXNhi1sm"
341
- },
342
- "source": [
343
- "Modify"
344
  ]
345
  },
346
  {
347
  "cell_type": "code",
348
- "execution_count": 13,
349
  "metadata": {
350
  "colab": {
351
  "base_uri": "https://localhost:8080/"
@@ -358,50 +256,29 @@
358
  "name": "stdout",
359
  "output_type": "stream",
360
  "text": [
361
- "dict_values(['Ford', 'Mustang', 1964, 'white', '315 HP'])\n",
362
- "dict_values(['Ford', 'Mustang', 2020, 'white', '315 HP'])\n"
363
  ]
364
  }
365
  ],
366
  "source": [
 
 
 
 
 
 
 
 
 
367
  "x = dic.values()\n",
368
  "\n",
369
- "print(x) #before the change\n",
370
  "\n",
371
  "dic[\"year\"] = 2020\n",
 
372
  "\n",
373
- "print(x) #after the change"
374
- ]
375
- },
376
- {
377
- "cell_type": "code",
378
- "execution_count": 14,
379
- "metadata": {
380
- "colab": {
381
- "base_uri": "https://localhost:8080/"
382
- },
383
- "id": "uvPL_HxuoeYj",
384
- "outputId": "26100336-0df1-475e-fe1d-43aa96310b02"
385
- },
386
- "outputs": [
387
- {
388
- "data": {
389
- "text/plain": [
390
- "{'brand': 'Ford',\n",
391
- " 'model': 'Mustang',\n",
392
- " 'year': 2020,\n",
393
- " 'color': 'white',\n",
394
- " 'Power': '400 HP'}"
395
- ]
396
- },
397
- "execution_count": 14,
398
- "metadata": {},
399
- "output_type": "execute_result"
400
- }
401
- ],
402
- "source": [
403
- "dic.update({\"Power\":\"400 HP\"})\n",
404
- "dic"
405
  ]
406
  },
407
  {
@@ -415,7 +292,7 @@
415
  },
416
  {
417
  "cell_type": "code",
418
- "execution_count": 15,
419
  "metadata": {
420
  "colab": {
421
  "base_uri": "https://localhost:8080/"
@@ -448,7 +325,7 @@
448
  },
449
  {
450
  "cell_type": "code",
451
- "execution_count": 16,
452
  "metadata": {
453
  "colab": {
454
  "base_uri": "https://localhost:8080/"
@@ -461,7 +338,7 @@
461
  "name": "stdout",
462
  "output_type": "stream",
463
  "text": [
464
- "{'brand': 'Ford', 'year': 2020, 'color': 'white', 'Power': '400 HP'}\n"
465
  ]
466
  }
467
  ],
@@ -472,7 +349,7 @@
472
  },
473
  {
474
  "cell_type": "code",
475
- "execution_count": 17,
476
  "metadata": {
477
  "colab": {
478
  "base_uri": "https://localhost:8080/"
@@ -496,7 +373,7 @@
496
  },
497
  {
498
  "cell_type": "code",
499
- "execution_count": 18,
500
  "metadata": {
501
  "colab": {
502
  "base_uri": "https://localhost:8080/"
@@ -520,7 +397,7 @@
520
  },
521
  {
522
  "cell_type": "code",
523
- "execution_count": 19,
524
  "metadata": {
525
  "colab": {
526
  "base_uri": "https://localhost:8080/"
@@ -553,7 +430,7 @@
553
  },
554
  {
555
  "cell_type": "code",
556
- "execution_count": 20,
557
  "metadata": {
558
  "id": "sjuLUjk4lMMW"
559
  },
@@ -566,18 +443,9 @@
566
  "}"
567
  ]
568
  },
569
- {
570
- "cell_type": "markdown",
571
- "metadata": {
572
- "id": "SfuHtH52lWkM"
573
- },
574
- "source": [
575
- "for loop with keys"
576
- ]
577
- },
578
  {
579
  "cell_type": "code",
580
- "execution_count": 21,
581
  "metadata": {
582
  "colab": {
583
  "base_uri": "https://localhost:8080/"
@@ -597,13 +465,14 @@
597
  }
598
  ],
599
  "source": [
 
600
  "for x in dic:\n",
601
  " print(x)"
602
  ]
603
  },
604
  {
605
  "cell_type": "code",
606
- "execution_count": 22,
607
  "metadata": {
608
  "colab": {
609
  "base_uri": "https://localhost:8080/"
@@ -623,22 +492,14 @@
623
  }
624
  ],
625
  "source": [
 
626
  "for x in dic.keys():\n",
627
  " print(x)"
628
  ]
629
  },
630
- {
631
- "cell_type": "markdown",
632
- "metadata": {
633
- "id": "_CT7BsxjlsjZ"
634
- },
635
- "source": [
636
- "for loop with values"
637
- ]
638
- },
639
  {
640
  "cell_type": "code",
641
- "execution_count": 23,
642
  "metadata": {
643
  "colab": {
644
  "base_uri": "https://localhost:8080/"
@@ -658,13 +519,14 @@
658
  }
659
  ],
660
  "source": [
 
661
  "for x in dic:\n",
662
  " print(dic[x])"
663
  ]
664
  },
665
  {
666
  "cell_type": "code",
667
- "execution_count": 24,
668
  "metadata": {
669
  "colab": {
670
  "base_uri": "https://localhost:8080/"
@@ -684,22 +546,14 @@
684
  }
685
  ],
686
  "source": [
 
687
  "for x in dic.values():\n",
688
  " print(x)"
689
  ]
690
  },
691
- {
692
- "cell_type": "markdown",
693
- "metadata": {
694
- "id": "e8ttUHNPl2Ep"
695
- },
696
- "source": [
697
- "for loop with items"
698
- ]
699
- },
700
  {
701
  "cell_type": "code",
702
- "execution_count": 25,
703
  "metadata": {
704
  "colab": {
705
  "base_uri": "https://localhost:8080/"
@@ -719,27 +573,20 @@
719
  }
720
  ],
721
  "source": [
 
722
  "for x, y in dic.items():\n",
723
  " print(x,':', y)"
724
  ]
725
  },
726
- {
727
- "cell_type": "markdown",
728
- "metadata": {
729
- "id": "fq8j8EkwnbuQ"
730
- },
731
- "source": [
732
- "Advanced Operations of dictionaries"
733
- ]
734
- },
735
  {
736
  "cell_type": "code",
737
- "execution_count": 26,
738
  "metadata": {
739
  "id": "GjgwAYbZmtFC"
740
  },
741
  "outputs": [],
742
  "source": [
 
743
  "child1 = {\n",
744
  " \"name\" : \"Rohan\",\n",
745
  " \"year\" : 2004\n",
@@ -762,7 +609,7 @@
762
  },
763
  {
764
  "cell_type": "code",
765
- "execution_count": 27,
766
  "metadata": {
767
  "colab": {
768
  "base_uri": "https://localhost:8080/"
@@ -779,7 +626,7 @@
779
  " 'child3': {'name': 'Rana', 'year': 2011}}"
780
  ]
781
  },
782
- "execution_count": 27,
783
  "metadata": {},
784
  "output_type": "execute_result"
785
  }
@@ -787,414 +634,6 @@
787
  "source": [
788
  "myfamily"
789
  ]
790
- },
791
- {
792
- "cell_type": "code",
793
- "execution_count": 28,
794
- "metadata": {
795
- "colab": {
796
- "base_uri": "https://localhost:8080/"
797
- },
798
- "id": "k0_Fp2RrnHlY",
799
- "outputId": "e15c1f6b-25d8-42a9-fa78-409dfe895b25"
800
- },
801
- "outputs": [
802
- {
803
- "name": "stdout",
804
- "output_type": "stream",
805
- "text": [
806
- "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
807
- ]
808
- }
809
- ],
810
- "source": [
811
- "dic = {}\n",
812
- "for x in range(1, 6):\n",
813
- " dic[x] = x ** 2\n",
814
- " # dic.update({x: x ** 2})\n",
815
- "print(dic)"
816
- ]
817
- },
818
- {
819
- "cell_type": "code",
820
- "execution_count": 29,
821
- "metadata": {
822
- "colab": {
823
- "base_uri": "https://localhost:8080/"
824
- },
825
- "id": "MpLOphmoTesg",
826
- "outputId": "14fab957-1055-421e-8e2c-b8d18eca45bc"
827
- },
828
- "outputs": [
829
- {
830
- "name": "stdout",
831
- "output_type": "stream",
832
- "text": [
833
- "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
834
- ]
835
- }
836
- ],
837
- "source": [
838
- "squares = {x: x ** 2 for x in range(1, 6)}\n",
839
- "print(squares)"
840
- ]
841
- },
842
- {
843
- "cell_type": "code",
844
- "execution_count": 30,
845
- "metadata": {
846
- "colab": {
847
- "base_uri": "https://localhost:8080/"
848
- },
849
- "id": "w6EtOS4wookU",
850
- "outputId": "2a19cdd0-2f67-4b01-cbe6-28c5b1d193d5"
851
- },
852
- "outputs": [
853
- {
854
- "name": "stdout",
855
- "output_type": "stream",
856
- "text": [
857
- "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n"
858
- ]
859
- }
860
- ],
861
- "source": [
862
- "dic = {}\n",
863
- "for x in range(1, 11):\n",
864
- " if x % 2 == 0:\n",
865
- " dic[x] = x ** 2\n",
866
- " # dic.update({x: x ** 2})\n",
867
- "print(dic)"
868
- ]
869
- },
870
- {
871
- "cell_type": "code",
872
- "execution_count": 31,
873
- "metadata": {
874
- "colab": {
875
- "base_uri": "https://localhost:8080/"
876
- },
877
- "id": "dPjzWfdsau9g",
878
- "outputId": "9460ce1d-0cbc-4449-82b8-53e11183df28"
879
- },
880
- "outputs": [
881
- {
882
- "name": "stdout",
883
- "output_type": "stream",
884
- "text": [
885
- "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n"
886
- ]
887
- }
888
- ],
889
- "source": [
890
- "squares = {x: x ** 2 for x in range(1, 11) if x % 2 == 0}\n",
891
- "print(squares)"
892
- ]
893
- },
894
- {
895
- "cell_type": "code",
896
- "execution_count": 39,
897
- "metadata": {
898
- "id": "9OtTs6tC7Zcf"
899
- },
900
- "outputs": [],
901
- "source": [
902
- "users = [\n",
903
- " {\n",
904
- " \"_id\": \"u12345\",\n",
905
- " \"name\": \"Rahman\",\n",
906
- " \"email\": \"rahman@example.com\",\n",
907
- " \"age\": 28,\n",
908
- " \"isActive\": True,\n",
909
- " \"address\": {\n",
910
- " \"street\": \"123 Lakeview Road\",\n",
911
- " \"city\": \"Dhaka\",\n",
912
- " \"zip\": \"1207\"\n",
913
- " },\n",
914
- " \"hobbies\": [\"reading\", \"gaming\", \"traveling\"],\n",
915
- " \"orders\": [\n",
916
- " {\n",
917
- " \"order_id\": \"ord1001\",\n",
918
- " \"date\": \"2025-05-01\",\n",
919
- " \"items\": [\n",
920
- " {\"product\": \"Book\", \"price\": 12.99, \"quantity\": 1},\n",
921
- " {\"product\": \"Pen\", \"price\": 1.99, \"quantity\": 3}\n",
922
- " ],\n",
923
- " \"total\": 18.96\n",
924
- " },\n",
925
- " {\n",
926
- " \"order_id\": \"ord1002\",\n",
927
- " \"date\": \"2025-05-04\",\n",
928
- " \"items\": [\n",
929
- " {\"product\": \"Shoes\", \"price\": 45.00, \"quantity\": 1}\n",
930
- " ],\n",
931
- " \"total\": 45.00\n",
932
- " }\n",
933
- " ]\n",
934
- " },\n",
935
- " {\n",
936
- " \"_id\": \"u12346\",\n",
937
- " \"name\": \"Fatima\",\n",
938
- " \"email\": \"fatima@example.com\",\n",
939
- " \"age\": 32,\n",
940
- " \"isActive\": False,\n",
941
- " \"address\": {\n",
942
- " \"street\": \"78 Rose Lane\",\n",
943
- " \"city\": \"Chittagong\",\n",
944
- " \"zip\": \"4000\"\n",
945
- " },\n",
946
- " \"hobbies\": [\"painting\", \"yoga\"],\n",
947
- " \"orders\": [\n",
948
- " {\n",
949
- " \"order_id\": \"ord1003\",\n",
950
- " \"date\": \"2025-05-02\",\n",
951
- " \"items\": [\n",
952
- " {\"product\": \"Canvas\", \"price\": 25.50, \"quantity\": 2}\n",
953
- " ],\n",
954
- " \"total\": 51.00\n",
955
- " }\n",
956
- " ]\n",
957
- " },\n",
958
- " {\n",
959
- " \"_id\": \"u12347\",\n",
960
- " \"name\": \"Karim\",\n",
961
- " \"email\": \"karim@example.com\",\n",
962
- " \"age\": 40,\n",
963
- " \"isActive\": True,\n",
964
- " \"address\": {\n",
965
- " \"street\": \"10 Green Street\",\n",
966
- " \"city\": \"Khulna\",\n",
967
- " \"zip\": \"9100\"\n",
968
- " },\n",
969
- " \"hobbies\": [\"fishing\", \"cycling\", \"chess\"],\n",
970
- " \"orders\": []\n",
971
- " },\n",
972
- " {\n",
973
- " \"_id\": \"u12348\",\n",
974
- " \"name\": \"Ayesha\",\n",
975
- " \"email\": \"ayesha@example.com\",\n",
976
- " \"age\": 25,\n",
977
- " \"isActive\": True,\n",
978
- " \"address\": {\n",
979
- " \"street\": \"56 Mango Street\",\n",
980
- " \"city\": \"Sylhet\",\n",
981
- " \"zip\": \"3100\"\n",
982
- " },\n",
983
- " \"hobbies\": [\"dancing\", \"baking\"],\n",
984
- " \"orders\": [\n",
985
- " {\n",
986
- " \"order_id\": \"ord1004\",\n",
987
- " \"date\": \"2025-05-05\",\n",
988
- " \"items\": [\n",
989
- " {\"product\": \"Dress\", \"price\": 30.00, \"quantity\": 1},\n",
990
- " {\"product\": \"Handbag\", \"price\": 40.00, \"quantity\": 1}\n",
991
- " ],\n",
992
- " \"total\": 70.00\n",
993
- " }\n",
994
- " ]\n",
995
- " }\n",
996
- "]\n"
997
- ]
998
- },
999
- {
1000
- "cell_type": "code",
1001
- "execution_count": 46,
1002
- "metadata": {},
1003
- "outputs": [
1004
- {
1005
- "data": {
1006
- "text/plain": [
1007
- "[{'_id': 'u12345',\n",
1008
- " 'name': 'Rahman',\n",
1009
- " 'email': 'rahman@example.com',\n",
1010
- " 'age': 28,\n",
1011
- " 'isActive': True,\n",
1012
- " 'address': {'street': '123 Lakeview Road', 'city': 'Dhaka', 'zip': '1207'},\n",
1013
- " 'hobbies': ['reading', 'gaming', 'traveling'],\n",
1014
- " 'orders': [{'order_id': 'ord1001',\n",
1015
- " 'date': '2025-05-01',\n",
1016
- " 'items': [{'product': 'Book', 'price': 12.99, 'quantity': 1},\n",
1017
- " {'product': 'Pen', 'price': 1.99, 'quantity': 3}],\n",
1018
- " 'total': 18.96},\n",
1019
- " {'order_id': 'ord1002',\n",
1020
- " 'date': '2025-05-04',\n",
1021
- " 'items': [{'product': 'Shoes', 'price': 45.0, 'quantity': 1}],\n",
1022
- " 'total': 45.0}]},\n",
1023
- " {'_id': 'u12347',\n",
1024
- " 'name': 'Karim',\n",
1025
- " 'email': 'karim@example.com',\n",
1026
- " 'age': 40,\n",
1027
- " 'isActive': True,\n",
1028
- " 'address': {'street': '10 Green Street', 'city': 'Khulna', 'zip': '9100'},\n",
1029
- " 'hobbies': ['fishing', 'cycling', 'chess'],\n",
1030
- " 'orders': []},\n",
1031
- " {'_id': 'u12348',\n",
1032
- " 'name': 'Ayesha',\n",
1033
- " 'email': 'ayesha@example.com',\n",
1034
- " 'age': 25,\n",
1035
- " 'isActive': True,\n",
1036
- " 'address': {'street': '56 Mango Street', 'city': 'Sylhet', 'zip': '3100'},\n",
1037
- " 'hobbies': ['dancing', 'baking'],\n",
1038
- " 'orders': [{'order_id': 'ord1004',\n",
1039
- " 'date': '2025-05-05',\n",
1040
- " 'items': [{'product': 'Dress', 'price': 30.0, 'quantity': 1},\n",
1041
- " {'product': 'Handbag', 'price': 40.0, 'quantity': 1}],\n",
1042
- " 'total': 70.0}]}]"
1043
- ]
1044
- },
1045
- "execution_count": 46,
1046
- "metadata": {},
1047
- "output_type": "execute_result"
1048
- }
1049
- ],
1050
- "source": [
1051
- "active_users = [user for user in users if user[\"isActive\"]]\n",
1052
- "active_users"
1053
- ]
1054
- },
1055
- {
1056
- "cell_type": "code",
1057
- "execution_count": 47,
1058
- "metadata": {},
1059
- "outputs": [
1060
- {
1061
- "data": {
1062
- "text/plain": [
1063
- "[{'name': 'Rahman', 'email': 'rahman@example.com'},\n",
1064
- " {'name': 'Fatima', 'email': 'fatima@example.com'},\n",
1065
- " {'name': 'Ayesha', 'email': 'ayesha@example.com'}]"
1066
- ]
1067
- },
1068
- "execution_count": 47,
1069
- "metadata": {},
1070
- "output_type": "execute_result"
1071
- }
1072
- ],
1073
- "source": [
1074
- "users_with_orders = [\n",
1075
- " {\"name\": user[\"name\"], \"email\": user[\"email\"]}\n",
1076
- " for user in users if len(user[\"orders\"]) > 0\n",
1077
- "]\n",
1078
- "users_with_orders"
1079
- ]
1080
- },
1081
- {
1082
- "cell_type": "code",
1083
- "execution_count": 48,
1084
- "metadata": {},
1085
- "outputs": [
1086
- {
1087
- "data": {
1088
- "text/plain": [
1089
- "[{'name': 'Rahman', 'total_spent': 63.96},\n",
1090
- " {'name': 'Fatima', 'total_spent': 51.0},\n",
1091
- " {'name': 'Karim', 'total_spent': 0},\n",
1092
- " {'name': 'Ayesha', 'total_spent': 70.0}]"
1093
- ]
1094
- },
1095
- "execution_count": 48,
1096
- "metadata": {},
1097
- "output_type": "execute_result"
1098
- }
1099
- ],
1100
- "source": [
1101
- "user_spending = [\n",
1102
- " {\n",
1103
- " \"name\": user[\"name\"],\n",
1104
- " \"total_spent\": sum(order[\"total\"] for order in user[\"orders\"])\n",
1105
- " }\n",
1106
- " for user in users\n",
1107
- "]\n",
1108
- "user_spending"
1109
- ]
1110
- },
1111
- {
1112
- "cell_type": "code",
1113
- "execution_count": 53,
1114
- "metadata": {},
1115
- "outputs": [
1116
- {
1117
- "name": "stdout",
1118
- "output_type": "stream",
1119
- "text": [
1120
- "Book\n",
1121
- "Pen\n",
1122
- "Shoes\n"
1123
- ]
1124
- }
1125
- ],
1126
- "source": [
1127
- "for user in users:\n",
1128
- " if user['name'] == 'Rahman':\n",
1129
- " for i in user['orders']:\n",
1130
- " for j in i['items']:\n",
1131
- " print(j['product'])"
1132
- ]
1133
- },
1134
- {
1135
- "cell_type": "code",
1136
- "execution_count": 55,
1137
- "metadata": {},
1138
- "outputs": [
1139
- {
1140
- "data": {
1141
- "text/plain": [
1142
- "31.25"
1143
- ]
1144
- },
1145
- "execution_count": 55,
1146
- "metadata": {},
1147
- "output_type": "execute_result"
1148
- }
1149
- ],
1150
- "source": [
1151
- "avg_age = sum(user[\"age\"] for user in users) / len(users)\n",
1152
- "avg_age"
1153
- ]
1154
- },
1155
- {
1156
- "cell_type": "code",
1157
- "execution_count": 63,
1158
- "metadata": {},
1159
- "outputs": [
1160
- {
1161
- "ename": "SyntaxError",
1162
- "evalue": "invalid syntax (3403861915.py, line 5)",
1163
- "output_type": "error",
1164
- "traceback": [
1165
- " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[63]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mproduct_summary.update({[item[\"product\"]] += item[\"quantity\"]})\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n"
1166
- ]
1167
- }
1168
- ],
1169
- "source": [
1170
- "product_summary = {}\n",
1171
- "for user in users:\n",
1172
- " for order in user[\"orders\"]:\n",
1173
- " for item in order[\"items\"]:\n",
1174
- " product_summary.update({[item[\"product\"]] += item[\"quantity\"]})\n",
1175
- "product_summary"
1176
- ]
1177
- },
1178
- {
1179
- "cell_type": "code",
1180
- "execution_count": 65,
1181
- "metadata": {},
1182
- "outputs": [
1183
- {
1184
- "data": {
1185
- "text/plain": [
1186
- "1"
1187
- ]
1188
- },
1189
- "execution_count": 65,
1190
- "metadata": {},
1191
- "output_type": "execute_result"
1192
- }
1193
- ],
1194
- "source": [
1195
- "dhaka_count = sum(1 for user in users if user[\"address\"][\"city\"] == \"Dhaka\")\n",
1196
- "dhaka_count"
1197
- ]
1198
  }
1199
  ],
1200
  "metadata": {
 
2
  "cells": [
3
  {
4
  "cell_type": "markdown",
5
+ "metadata": {},
 
 
6
  "source": [
7
+ "# Dictionaries Operations"
8
  ]
9
  },
10
  {
11
  "cell_type": "code",
12
+ "execution_count": 72,
13
  "metadata": {
14
  "id": "yih6CE4RiIrl"
15
  },
 
18
  "Dictionaries = {\"key1\":\"value1\"}"
19
  ]
20
  },
 
 
 
 
 
 
 
 
 
21
  {
22
  "cell_type": "code",
23
+ "execution_count": 73,
24
  "metadata": {
25
  "id": "VLkxduR8hSQ_"
26
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  "outputs": [
28
  {
29
  "name": "stdout",
30
  "output_type": "stream",
31
  "text": [
32
  "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n",
 
33
  "<class 'dict'>\n"
34
  ]
35
  }
36
  ],
37
  "source": [
38
+ "# Create Dictionaries\n",
39
+ "dic = {\n",
40
+ " \"brand\": \"Ford\",\n",
41
+ " \"model\": \"Mustang\",\n",
42
+ " \"year\": 1964\n",
43
+ "}\n",
44
  "print(dic)\n",
 
45
  "print(type(dic))"
46
  ]
47
  },
48
  {
49
  "cell_type": "code",
50
+ "execution_count": 74,
51
  "metadata": {
52
  "colab": {
53
  "base_uri": "https://localhost:8080/"
 
69
  "print(dic)"
70
  ]
71
  },
 
 
 
 
 
 
 
 
 
72
  {
73
  "cell_type": "code",
74
+ "execution_count": 75,
75
  "metadata": {
76
  "colab": {
77
  "base_uri": "https://localhost:8080/"
 
89
  }
90
  ],
91
  "source": [
92
+ "# Accessing Items\n",
93
  "x = dic[\"year\"]\n",
94
  "print(x)"
95
  ]
96
  },
97
  {
98
  "cell_type": "code",
99
+ "execution_count": 76,
100
  "metadata": {
101
  "colab": {
102
  "base_uri": "https://localhost:8080/",
 
112
  "'Mustang'"
113
  ]
114
  },
115
+ "execution_count": 76,
116
  "metadata": {},
117
  "output_type": "execute_result"
118
  }
 
122
  "x"
123
  ]
124
  },
 
 
 
 
 
 
 
 
 
125
  {
126
  "cell_type": "code",
127
+ "execution_count": 77,
128
  "metadata": {
129
  "colab": {
130
  "base_uri": "https://localhost:8080/"
 
137
  "name": "stdout",
138
  "output_type": "stream",
139
  "text": [
140
+ "dict_keys(['brand', 'model', 'year'])\n",
141
+ "['brand', 'model', 'year']\n"
142
  ]
143
  }
144
  ],
145
  "source": [
146
+ "# keys()\n",
147
  "x = dic.keys()\n",
148
+ "print(x)\n",
149
+ "print(list(x))"
150
  ]
151
  },
152
  {
153
  "cell_type": "code",
154
+ "execution_count": 78,
155
  "metadata": {
156
  "colab": {
157
  "base_uri": "https://localhost:8080/"
 
161
  },
162
  "outputs": [
163
  {
164
+ "name": "stdout",
165
+ "output_type": "stream",
166
+ "text": [
167
+ "dict_values(['Ford', 'Mustang', 1964])\n",
168
+ "['Ford', 'Mustang', 1964]\n"
169
+ ]
 
 
170
  }
171
  ],
172
  "source": [
173
+ "# values()\n",
174
  "x = dic.values()\n",
175
+ "print(x)\n",
176
+ "print(list(x))"
177
  ]
178
  },
179
  {
180
  "cell_type": "code",
181
+ "execution_count": 79,
182
  "metadata": {
183
  "colab": {
184
  "base_uri": "https://localhost:8080/"
 
188
  },
189
  "outputs": [
190
  {
191
+ "name": "stdout",
192
+ "output_type": "stream",
193
+ "text": [
194
+ "dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])\n",
195
+ "[('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)]\n"
196
+ ]
 
 
197
  }
198
  ],
199
  "source": [
200
+ "# items()\n",
201
  "x = dic.items()\n",
202
+ "print(x)\n",
203
+ "print(list(x))"
 
 
 
 
 
 
 
 
204
  ]
205
  },
206
  {
207
  "cell_type": "code",
208
+ "execution_count": 80,
209
  "metadata": {
210
  "colab": {
211
  "base_uri": "https://localhost:8080/"
 
215
  },
216
  "outputs": [
217
  {
218
+ "name": "stdout",
219
+ "output_type": "stream",
220
+ "text": [
221
+ "Before add: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n",
222
+ "After add color: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white'}\n",
223
+ "After add power: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white', 'power': '315 HP'}\n"
224
+ ]
 
225
  }
226
  ],
227
  "source": [
228
+ "dic = {\n",
229
+ " \"brand\": \"Ford\",\n",
230
+ " \"model\": \"Mustang\",\n",
231
+ " \"year\": 1964\n",
232
+ "}\n",
233
+ "print('Before add:',dic)\n",
234
+ "\n",
235
+ "# Add Items\n",
236
  "dic[\"color\"] = \"white\"\n",
237
+ "print('After add color:',dic)\n",
238
+ "\n",
239
+ "d= {\"power\":\"315 HP\"}\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  "dic.update(d)\n",
241
+ "print('After add power:',dic)"
 
 
 
 
 
 
 
 
 
242
  ]
243
  },
244
  {
245
  "cell_type": "code",
246
+ "execution_count": 81,
247
  "metadata": {
248
  "colab": {
249
  "base_uri": "https://localhost:8080/"
 
256
  "name": "stdout",
257
  "output_type": "stream",
258
  "text": [
259
+ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white', 'power': '315 HP'}\n",
260
+ "{'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'white', 'power': '400 HP'}\n"
261
  ]
262
  }
263
  ],
264
  "source": [
265
+ "dic = {\n",
266
+ " 'brand': 'Ford',\n",
267
+ " 'model': 'Mustang',\n",
268
+ " 'year': 1964,\n",
269
+ " 'color': 'white',\n",
270
+ " 'power': '315 HP'\n",
271
+ "}\n",
272
+ "\n",
273
+ "# Modify\n",
274
  "x = dic.values()\n",
275
  "\n",
276
+ "print(dic) #before the change\n",
277
  "\n",
278
  "dic[\"year\"] = 2020\n",
279
+ "dic[\"power\"] = '400 HP'\n",
280
  "\n",
281
+ "print(dic) #after the change"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  ]
283
  },
284
  {
 
292
  },
293
  {
294
  "cell_type": "code",
295
+ "execution_count": 82,
296
  "metadata": {
297
  "colab": {
298
  "base_uri": "https://localhost:8080/"
 
325
  },
326
  {
327
  "cell_type": "code",
328
+ "execution_count": 83,
329
  "metadata": {
330
  "colab": {
331
  "base_uri": "https://localhost:8080/"
 
338
  "name": "stdout",
339
  "output_type": "stream",
340
  "text": [
341
+ "{'brand': 'Ford', 'year': 2020, 'color': 'white', 'power': '400 HP'}\n"
342
  ]
343
  }
344
  ],
 
349
  },
350
  {
351
  "cell_type": "code",
352
+ "execution_count": 84,
353
  "metadata": {
354
  "colab": {
355
  "base_uri": "https://localhost:8080/"
 
373
  },
374
  {
375
  "cell_type": "code",
376
+ "execution_count": 85,
377
  "metadata": {
378
  "colab": {
379
  "base_uri": "https://localhost:8080/"
 
397
  },
398
  {
399
  "cell_type": "code",
400
+ "execution_count": 86,
401
  "metadata": {
402
  "colab": {
403
  "base_uri": "https://localhost:8080/"
 
430
  },
431
  {
432
  "cell_type": "code",
433
+ "execution_count": 87,
434
  "metadata": {
435
  "id": "sjuLUjk4lMMW"
436
  },
 
443
  "}"
444
  ]
445
  },
 
 
 
 
 
 
 
 
 
446
  {
447
  "cell_type": "code",
448
+ "execution_count": 88,
449
  "metadata": {
450
  "colab": {
451
  "base_uri": "https://localhost:8080/"
 
465
  }
466
  ],
467
  "source": [
468
+ "# for loop with keys\n",
469
  "for x in dic:\n",
470
  " print(x)"
471
  ]
472
  },
473
  {
474
  "cell_type": "code",
475
+ "execution_count": 89,
476
  "metadata": {
477
  "colab": {
478
  "base_uri": "https://localhost:8080/"
 
492
  }
493
  ],
494
  "source": [
495
+ "# for loop with keys\n",
496
  "for x in dic.keys():\n",
497
  " print(x)"
498
  ]
499
  },
 
 
 
 
 
 
 
 
 
500
  {
501
  "cell_type": "code",
502
+ "execution_count": 90,
503
  "metadata": {
504
  "colab": {
505
  "base_uri": "https://localhost:8080/"
 
519
  }
520
  ],
521
  "source": [
522
+ "# for loop with values\n",
523
  "for x in dic:\n",
524
  " print(dic[x])"
525
  ]
526
  },
527
  {
528
  "cell_type": "code",
529
+ "execution_count": 91,
530
  "metadata": {
531
  "colab": {
532
  "base_uri": "https://localhost:8080/"
 
546
  }
547
  ],
548
  "source": [
549
+ "# for loop with values\n",
550
  "for x in dic.values():\n",
551
  " print(x)"
552
  ]
553
  },
 
 
 
 
 
 
 
 
 
554
  {
555
  "cell_type": "code",
556
+ "execution_count": 92,
557
  "metadata": {
558
  "colab": {
559
  "base_uri": "https://localhost:8080/"
 
573
  }
574
  ],
575
  "source": [
576
+ "# for loop with items\n",
577
  "for x, y in dic.items():\n",
578
  " print(x,':', y)"
579
  ]
580
  },
 
 
 
 
 
 
 
 
 
581
  {
582
  "cell_type": "code",
583
+ "execution_count": 93,
584
  "metadata": {
585
  "id": "GjgwAYbZmtFC"
586
  },
587
  "outputs": [],
588
  "source": [
589
+ "# Nested dictionaries\n",
590
  "child1 = {\n",
591
  " \"name\" : \"Rohan\",\n",
592
  " \"year\" : 2004\n",
 
609
  },
610
  {
611
  "cell_type": "code",
612
+ "execution_count": 94,
613
  "metadata": {
614
  "colab": {
615
  "base_uri": "https://localhost:8080/"
 
626
  " 'child3': {'name': 'Rana', 'year': 2011}}"
627
  ]
628
  },
629
+ "execution_count": 94,
630
  "metadata": {},
631
  "output_type": "execute_result"
632
  }
 
634
  "source": [
635
  "myfamily"
636
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
637
  }
638
  ],
639
  "metadata": {
List.ipynb ADDED
@@ -0,0 +1,488 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "5b1d21eb",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Lisi Operations"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 139,
14
+ "id": "b8a7504b",
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "# List\n",
19
+ "mylist = [\"apple\", \"banana\", \"cherry\"]"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 140,
25
+ "id": "22045eee",
26
+ "metadata": {},
27
+ "outputs": [
28
+ {
29
+ "name": "stdout",
30
+ "output_type": "stream",
31
+ "text": [
32
+ "['apple', 'banana', 'cherry', 'apple', 'cherry']\n"
33
+ ]
34
+ }
35
+ ],
36
+ "source": [
37
+ "# print the list\n",
38
+ "thislist = [\"apple\", \"banana\", \"cherry\", \"apple\", \"cherry\"]\n",
39
+ "print(thislist)"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": 141,
45
+ "id": "3de98cfc",
46
+ "metadata": {},
47
+ "outputs": [
48
+ {
49
+ "name": "stdout",
50
+ "output_type": "stream",
51
+ "text": [
52
+ "3\n"
53
+ ]
54
+ }
55
+ ],
56
+ "source": [
57
+ "# Length of list\n",
58
+ "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
59
+ "print(len(thislist))"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": 142,
65
+ "id": "1644c599",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "# different data types in list\n",
70
+ "list1 = [\"apple\", \"banana\", \"cherry\"]\n",
71
+ "list2 = [1, 5, 7, 9, 3]\n",
72
+ "list3 = [True, False, False]"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": 143,
78
+ "id": "ad86e453",
79
+ "metadata": {},
80
+ "outputs": [],
81
+ "source": [
82
+ "# different data types in a single list\n",
83
+ "list1 = [\"abc\", 34, True, 40, \"male\"]"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": 144,
89
+ "id": "0ef37cc4",
90
+ "metadata": {},
91
+ "outputs": [
92
+ {
93
+ "name": "stdout",
94
+ "output_type": "stream",
95
+ "text": [
96
+ "<class 'list'>\n"
97
+ ]
98
+ }
99
+ ],
100
+ "source": [
101
+ "mylist = [\"apple\", \"banana\", \"cherry\"]\n",
102
+ "print(type(mylist))"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 145,
108
+ "id": "0bd3956a",
109
+ "metadata": {},
110
+ "outputs": [
111
+ {
112
+ "name": "stdout",
113
+ "output_type": "stream",
114
+ "text": [
115
+ "['apple', 'banana', 'cherry']\n"
116
+ ]
117
+ }
118
+ ],
119
+ "source": [
120
+ "# Another way to create a list\n",
121
+ "thislist = list((\"apple\", \"banana\", \"cherry\"))\n",
122
+ "print(thislist)"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 146,
128
+ "id": "f51e0509",
129
+ "metadata": {},
130
+ "outputs": [
131
+ {
132
+ "name": "stdout",
133
+ "output_type": "stream",
134
+ "text": [
135
+ "['apple', 'banana', 'orange']\n"
136
+ ]
137
+ }
138
+ ],
139
+ "source": [
140
+ "# Add Items to a List\n",
141
+ "fruits = ['apple', 'banana']\n",
142
+ "fruits.append('orange')\n",
143
+ "print(fruits)"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": 147,
149
+ "id": "d62d99d7",
150
+ "metadata": {},
151
+ "outputs": [
152
+ {
153
+ "name": "stdout",
154
+ "output_type": "stream",
155
+ "text": [
156
+ "['apple', 'mango', 'banana', 'orange']\n"
157
+ ]
158
+ }
159
+ ],
160
+ "source": [
161
+ "# insert(index, item) – Insert item at a specific index\n",
162
+ "fruits.insert(1, 'mango')\n",
163
+ "print(fruits)"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": 148,
169
+ "id": "f947d134",
170
+ "metadata": {},
171
+ "outputs": [
172
+ {
173
+ "name": "stdout",
174
+ "output_type": "stream",
175
+ "text": [
176
+ "['apple', 'mango', 'banana', 'orange', 'grape', 'pineapple']\n"
177
+ ]
178
+ }
179
+ ],
180
+ "source": [
181
+ "# extend(list) – Add multiple items from another list\n",
182
+ "fruits.extend(['grape', 'pineapple'])\n",
183
+ "print(fruits)"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "execution_count": 149,
189
+ "id": "24d3ecad",
190
+ "metadata": {},
191
+ "outputs": [
192
+ {
193
+ "name": "stdout",
194
+ "output_type": "stream",
195
+ "text": [
196
+ "['strawberry', 'mango', 'banana', 'orange', 'grape', 'pineapple']\n"
197
+ ]
198
+ }
199
+ ],
200
+ "source": [
201
+ "# Update by index\n",
202
+ "fruits[0] = 'strawberry'\n",
203
+ "print(fruits)"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "code",
208
+ "execution_count": 150,
209
+ "id": "b820261e",
210
+ "metadata": {},
211
+ "outputs": [
212
+ {
213
+ "name": "stdout",
214
+ "output_type": "stream",
215
+ "text": [
216
+ "['strawberry', 'mango', 'banana', 'grape', 'pineapple']\n"
217
+ ]
218
+ }
219
+ ],
220
+ "source": [
221
+ "# remove(item) – Remove by value\n",
222
+ "fruits.remove('orange')\n",
223
+ "print(fruits)"
224
+ ]
225
+ },
226
+ {
227
+ "cell_type": "code",
228
+ "execution_count": 151,
229
+ "id": "e8bde013",
230
+ "metadata": {},
231
+ "outputs": [
232
+ {
233
+ "name": "stdout",
234
+ "output_type": "stream",
235
+ "text": [
236
+ "pineapple\n",
237
+ "['strawberry', 'mango', 'banana', 'grape']\n"
238
+ ]
239
+ }
240
+ ],
241
+ "source": [
242
+ "# pop(index) – Remove and return item by index (default is last)\n",
243
+ "last_item = fruits.pop()\n",
244
+ "print(last_item)\n",
245
+ "print(fruits)"
246
+ ]
247
+ },
248
+ {
249
+ "cell_type": "code",
250
+ "execution_count": 152,
251
+ "id": "1bd10b49",
252
+ "metadata": {},
253
+ "outputs": [
254
+ {
255
+ "name": "stdout",
256
+ "output_type": "stream",
257
+ "text": [
258
+ "['strawberry', 'banana', 'grape']\n"
259
+ ]
260
+ }
261
+ ],
262
+ "source": [
263
+ "# del – Delete by index or slice\n",
264
+ "del fruits[1]\n",
265
+ "print(fruits) "
266
+ ]
267
+ },
268
+ {
269
+ "cell_type": "code",
270
+ "execution_count": 153,
271
+ "id": "368b7abf",
272
+ "metadata": {},
273
+ "outputs": [
274
+ {
275
+ "name": "stdout",
276
+ "output_type": "stream",
277
+ "text": [
278
+ "[]\n"
279
+ ]
280
+ }
281
+ ],
282
+ "source": [
283
+ "# clear() – Remove all items\n",
284
+ "fruits.clear()\n",
285
+ "print(fruits)"
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "markdown",
290
+ "id": "21857eeb",
291
+ "metadata": {},
292
+ "source": [
293
+ "# Basic Practice problems"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": 154,
299
+ "id": "8f7069a3",
300
+ "metadata": {},
301
+ "outputs": [
302
+ {
303
+ "name": "stdout",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "['apple', 'banana', 'orange']\n"
307
+ ]
308
+ }
309
+ ],
310
+ "source": [
311
+ "# Given the list fruits = ['apple', 'banana'], add 'orange' at the end.\n",
312
+ "fruits = ['apple', 'banana']\n",
313
+ "fruits.append('orange')\n",
314
+ "print(fruits)"
315
+ ]
316
+ },
317
+ {
318
+ "cell_type": "code",
319
+ "execution_count": 155,
320
+ "id": "97680bab",
321
+ "metadata": {},
322
+ "outputs": [
323
+ {
324
+ "name": "stdout",
325
+ "output_type": "stream",
326
+ "text": [
327
+ "['apple', 'mango', 'banana']\n"
328
+ ]
329
+ }
330
+ ],
331
+ "source": [
332
+ "# Insert 'mango' between 'apple' and 'banana'.\n",
333
+ "fruits = ['apple', 'banana']\n",
334
+ "fruits.insert(1, 'mango')\n",
335
+ "print(fruits)"
336
+ ]
337
+ },
338
+ {
339
+ "cell_type": "code",
340
+ "execution_count": 156,
341
+ "id": "21d5422f",
342
+ "metadata": {},
343
+ "outputs": [
344
+ {
345
+ "name": "stdout",
346
+ "output_type": "stream",
347
+ "text": [
348
+ "['apple', 'banana', 'grape', 'pineapple']\n"
349
+ ]
350
+ }
351
+ ],
352
+ "source": [
353
+ "# Add ['grape', 'pineapple'] to the list fruits.\n",
354
+ "fruits = ['apple', 'banana']\n",
355
+ "fruits.extend(['grape', 'pineapple'])\n",
356
+ "print(fruits)"
357
+ ]
358
+ },
359
+ {
360
+ "cell_type": "code",
361
+ "execution_count": 163,
362
+ "id": "6470364e",
363
+ "metadata": {},
364
+ "outputs": [
365
+ {
366
+ "name": "stdout",
367
+ "output_type": "stream",
368
+ "text": [
369
+ "['strawberry', 'banana']\n"
370
+ ]
371
+ }
372
+ ],
373
+ "source": [
374
+ "# ange 'apple' to 'strawberry'.\n",
375
+ "fruits = ['apple', 'banana']\n",
376
+ "fruits[0] = 'strawberry'\n",
377
+ "print(fruits)"
378
+ ]
379
+ },
380
+ {
381
+ "cell_type": "code",
382
+ "execution_count": 169,
383
+ "id": "54de17a8",
384
+ "metadata": {},
385
+ "outputs": [
386
+ {
387
+ "name": "stdout",
388
+ "output_type": "stream",
389
+ "text": [
390
+ "['apple', 'orange']\n"
391
+ ]
392
+ }
393
+ ],
394
+ "source": [
395
+ "# Remove 'banana' from ['apple', 'banana', 'orange'].\n",
396
+ "fruits = ['apple', 'banana', 'orange']\n",
397
+ "fruits.remove('banana')\n",
398
+ "print(fruits)"
399
+ ]
400
+ },
401
+ {
402
+ "cell_type": "code",
403
+ "execution_count": 170,
404
+ "id": "6d81fe0f",
405
+ "metadata": {},
406
+ "outputs": [
407
+ {
408
+ "name": "stdout",
409
+ "output_type": "stream",
410
+ "text": [
411
+ "['apple', 'orange']\n"
412
+ ]
413
+ }
414
+ ],
415
+ "source": [
416
+ "# Delete the second item in the list.\n",
417
+ "fruits = ['apple', 'banana', 'orange']\n",
418
+ "del fruits[1]\n",
419
+ "print(fruits)"
420
+ ]
421
+ },
422
+ {
423
+ "cell_type": "code",
424
+ "execution_count": 171,
425
+ "id": "872276ff",
426
+ "metadata": {},
427
+ "outputs": [
428
+ {
429
+ "name": "stdout",
430
+ "output_type": "stream",
431
+ "text": [
432
+ "orange\n",
433
+ "['apple', 'banana']\n"
434
+ ]
435
+ }
436
+ ],
437
+ "source": [
438
+ "# Remove the last fruit from the list and store it in a variable.\n",
439
+ "fruits = ['apple', 'banana', 'orange']\n",
440
+ "last_fruit = fruits.pop()\n",
441
+ "print(last_fruit)\n",
442
+ "print(fruits)"
443
+ ]
444
+ },
445
+ {
446
+ "cell_type": "code",
447
+ "execution_count": 172,
448
+ "id": "5d30b05f",
449
+ "metadata": {},
450
+ "outputs": [
451
+ {
452
+ "name": "stdout",
453
+ "output_type": "stream",
454
+ "text": [
455
+ "[]\n"
456
+ ]
457
+ }
458
+ ],
459
+ "source": [
460
+ "# Remove all items from ['a', 'b', 'c'].\n",
461
+ "letters = ['a', 'b', 'c']\n",
462
+ "letters.clear()\n",
463
+ "print(letters)"
464
+ ]
465
+ }
466
+ ],
467
+ "metadata": {
468
+ "kernelspec": {
469
+ "display_name": "all",
470
+ "language": "python",
471
+ "name": "python3"
472
+ },
473
+ "language_info": {
474
+ "codemirror_mode": {
475
+ "name": "ipython",
476
+ "version": 3
477
+ },
478
+ "file_extension": ".py",
479
+ "mimetype": "text/x-python",
480
+ "name": "python",
481
+ "nbconvert_exporter": "python",
482
+ "pygments_lexer": "ipython3",
483
+ "version": "3.12.9"
484
+ }
485
+ },
486
+ "nbformat": 4,
487
+ "nbformat_minor": 5
488
+ }
Loop.ipynb ADDED
@@ -0,0 +1,586 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "c3e21f76",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Loop Operations"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 78,
14
+ "id": "6ccbd87a",
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "name": "stdout",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "b\n",
22
+ "a\n",
23
+ "n\n",
24
+ "a\n",
25
+ "n\n",
26
+ "a\n"
27
+ ]
28
+ }
29
+ ],
30
+ "source": [
31
+ "# Character iteration example\n",
32
+ "for x in \"banana\":\n",
33
+ " print(x)"
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": 79,
39
+ "id": "a2f8970f",
40
+ "metadata": {},
41
+ "outputs": [
42
+ {
43
+ "name": "stdout",
44
+ "output_type": "stream",
45
+ "text": [
46
+ "apple\n",
47
+ "banana\n",
48
+ "cherry\n"
49
+ ]
50
+ }
51
+ ],
52
+ "source": [
53
+ "# List iteration example\n",
54
+ "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
55
+ "for x in fruits:\n",
56
+ " print(x)"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 80,
62
+ "id": "4a666d26",
63
+ "metadata": {},
64
+ "outputs": [
65
+ {
66
+ "name": "stdout",
67
+ "output_type": "stream",
68
+ "text": [
69
+ "apple\n"
70
+ ]
71
+ }
72
+ ],
73
+ "source": [
74
+ "# List iteration with break example\n",
75
+ "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
76
+ "for x in fruits:\n",
77
+ " if x == \"banana\":\n",
78
+ " break\n",
79
+ " print(x)"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 81,
85
+ "id": "f07d007a",
86
+ "metadata": {},
87
+ "outputs": [
88
+ {
89
+ "name": "stdout",
90
+ "output_type": "stream",
91
+ "text": [
92
+ "apple\n",
93
+ "cherry\n"
94
+ ]
95
+ }
96
+ ],
97
+ "source": [
98
+ "# List iteration with continue example\n",
99
+ "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
100
+ "for x in fruits:\n",
101
+ " if x == \"banana\":\n",
102
+ " continue\n",
103
+ " print(x)"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": 82,
109
+ "id": "1f89c7d3",
110
+ "metadata": {},
111
+ "outputs": [
112
+ {
113
+ "name": "stdout",
114
+ "output_type": "stream",
115
+ "text": [
116
+ "0\n",
117
+ "1\n",
118
+ "2\n",
119
+ "3\n",
120
+ "4\n",
121
+ "5\n"
122
+ ]
123
+ }
124
+ ],
125
+ "source": [
126
+ "# Loop with range example\n",
127
+ "for x in range(6):\n",
128
+ " print(x)"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": 83,
134
+ "id": "cbf03e9b",
135
+ "metadata": {},
136
+ "outputs": [
137
+ {
138
+ "name": "stdout",
139
+ "output_type": "stream",
140
+ "text": [
141
+ "2\n",
142
+ "3\n",
143
+ "4\n",
144
+ "5\n"
145
+ ]
146
+ }
147
+ ],
148
+ "source": [
149
+ "# loop with range and start example\n",
150
+ "for x in range(2, 6):\n",
151
+ " print(x)"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": 84,
157
+ "id": "811e2ffc",
158
+ "metadata": {},
159
+ "outputs": [
160
+ {
161
+ "name": "stdout",
162
+ "output_type": "stream",
163
+ "text": [
164
+ "2\n",
165
+ "5\n",
166
+ "8\n",
167
+ "11\n",
168
+ "14\n",
169
+ "17\n",
170
+ "20\n",
171
+ "23\n",
172
+ "26\n",
173
+ "29\n"
174
+ ]
175
+ }
176
+ ],
177
+ "source": [
178
+ "# Loop with range example with step\n",
179
+ "for x in range(2, 30, 3):\n",
180
+ " print(x)"
181
+ ]
182
+ },
183
+ {
184
+ "cell_type": "code",
185
+ "execution_count": 85,
186
+ "id": "331755b3",
187
+ "metadata": {},
188
+ "outputs": [
189
+ {
190
+ "name": "stdout",
191
+ "output_type": "stream",
192
+ "text": [
193
+ "0\n",
194
+ "1\n",
195
+ "2\n",
196
+ "3\n",
197
+ "4\n",
198
+ "5\n",
199
+ "Finally finished!\n"
200
+ ]
201
+ }
202
+ ],
203
+ "source": [
204
+ "# Loop with else example\n",
205
+ "for x in range(6):\n",
206
+ " print(x)\n",
207
+ "else:\n",
208
+ " print(\"Finally finished!\")"
209
+ ]
210
+ },
211
+ {
212
+ "cell_type": "code",
213
+ "execution_count": 86,
214
+ "id": "8adec0ef",
215
+ "metadata": {},
216
+ "outputs": [
217
+ {
218
+ "name": "stdout",
219
+ "output_type": "stream",
220
+ "text": [
221
+ "red apple\n",
222
+ "red banana\n",
223
+ "red cherry\n",
224
+ "big apple\n",
225
+ "big banana\n",
226
+ "big cherry\n",
227
+ "tasty apple\n",
228
+ "tasty banana\n",
229
+ "tasty cherry\n"
230
+ ]
231
+ }
232
+ ],
233
+ "source": [
234
+ "# Nested loops example\n",
235
+ "adj = [\"red\", \"big\", \"tasty\"]\n",
236
+ "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
237
+ "\n",
238
+ "for x in adj:\n",
239
+ " for y in fruits:\n",
240
+ " print(x, y)"
241
+ ]
242
+ },
243
+ {
244
+ "cell_type": "code",
245
+ "execution_count": 87,
246
+ "id": "cdbd0d25",
247
+ "metadata": {},
248
+ "outputs": [],
249
+ "source": [
250
+ "# Loop with pass example\n",
251
+ "for x in [0, 1, 2]:\n",
252
+ " pass"
253
+ ]
254
+ },
255
+ {
256
+ "cell_type": "markdown",
257
+ "id": "df4de683",
258
+ "metadata": {},
259
+ "source": [
260
+ "# Basic Loop Exercise"
261
+ ]
262
+ },
263
+ {
264
+ "cell_type": "code",
265
+ "execution_count": 88,
266
+ "id": "a6718b53",
267
+ "metadata": {},
268
+ "outputs": [
269
+ {
270
+ "name": "stdout",
271
+ "output_type": "stream",
272
+ "text": [
273
+ "1\n",
274
+ "2\n",
275
+ "3\n",
276
+ "4\n",
277
+ "5\n",
278
+ "6\n",
279
+ "7\n",
280
+ "8\n",
281
+ "9\n",
282
+ "10\n"
283
+ ]
284
+ }
285
+ ],
286
+ "source": [
287
+ "# Print Numbers from 1 to 10\n",
288
+ "for i in range(1, 11):\n",
289
+ " print(i)"
290
+ ]
291
+ },
292
+ {
293
+ "cell_type": "code",
294
+ "execution_count": 89,
295
+ "id": "7dcb2102",
296
+ "metadata": {},
297
+ "outputs": [
298
+ {
299
+ "name": "stdout",
300
+ "output_type": "stream",
301
+ "text": [
302
+ "2\n",
303
+ "4\n",
304
+ "6\n",
305
+ "8\n",
306
+ "10\n",
307
+ "12\n",
308
+ "14\n",
309
+ "16\n",
310
+ "18\n",
311
+ "20\n",
312
+ "22\n",
313
+ "24\n",
314
+ "26\n",
315
+ "28\n",
316
+ "30\n",
317
+ "32\n",
318
+ "34\n",
319
+ "36\n",
320
+ "38\n",
321
+ "40\n",
322
+ "42\n",
323
+ "44\n",
324
+ "46\n",
325
+ "48\n",
326
+ "50\n"
327
+ ]
328
+ }
329
+ ],
330
+ "source": [
331
+ "# Print Even Numbers from 1 to 50\n",
332
+ "for i in range(2, 51, 2):\n",
333
+ " print(i)"
334
+ ]
335
+ },
336
+ {
337
+ "cell_type": "code",
338
+ "execution_count": 90,
339
+ "id": "4901d8e5",
340
+ "metadata": {},
341
+ "outputs": [
342
+ {
343
+ "name": "stdout",
344
+ "output_type": "stream",
345
+ "text": [
346
+ "1\n",
347
+ "4\n",
348
+ "9\n",
349
+ "16\n",
350
+ "25\n",
351
+ "36\n",
352
+ "49\n",
353
+ "64\n",
354
+ "81\n",
355
+ "100\n"
356
+ ]
357
+ }
358
+ ],
359
+ "source": [
360
+ "# Print Squares of Numbers 1 to 10\n",
361
+ "for i in range(1, 11):\n",
362
+ " print(i * i)"
363
+ ]
364
+ },
365
+ {
366
+ "cell_type": "code",
367
+ "execution_count": 91,
368
+ "id": "47f05e67",
369
+ "metadata": {},
370
+ "outputs": [
371
+ {
372
+ "name": "stdout",
373
+ "output_type": "stream",
374
+ "text": [
375
+ "5050\n"
376
+ ]
377
+ }
378
+ ],
379
+ "source": [
380
+ "# Sum of First N Numbers\n",
381
+ "n = 100\n",
382
+ "total = 0\n",
383
+ "for i in range(1, n + 1):\n",
384
+ " total += i\n",
385
+ "print(total)"
386
+ ]
387
+ },
388
+ {
389
+ "cell_type": "code",
390
+ "execution_count": 92,
391
+ "id": "2305a1ef",
392
+ "metadata": {},
393
+ "outputs": [
394
+ {
395
+ "name": "stdout",
396
+ "output_type": "stream",
397
+ "text": [
398
+ "5 x 1 = 5\n",
399
+ "5 x 2 = 10\n",
400
+ "5 x 3 = 15\n",
401
+ "5 x 4 = 20\n",
402
+ "5 x 5 = 25\n",
403
+ "5 x 6 = 30\n",
404
+ "5 x 7 = 35\n",
405
+ "5 x 8 = 40\n",
406
+ "5 x 9 = 45\n",
407
+ "5 x 10 = 50\n"
408
+ ]
409
+ }
410
+ ],
411
+ "source": [
412
+ "# Multiplication Table\n",
413
+ "for i in range(1, 11):\n",
414
+ " print(f\"5 x {i} = {5 * i}\")"
415
+ ]
416
+ },
417
+ {
418
+ "cell_type": "code",
419
+ "execution_count": 93,
420
+ "id": "7ea44b23",
421
+ "metadata": {},
422
+ "outputs": [
423
+ {
424
+ "name": "stdout",
425
+ "output_type": "stream",
426
+ "text": [
427
+ "* \n",
428
+ "* * \n",
429
+ "* * * \n",
430
+ "* * * * \n",
431
+ "* * * * * \n"
432
+ ]
433
+ }
434
+ ],
435
+ "source": [
436
+ "# Print a Pattern\n",
437
+ "# * \n",
438
+ "# * * \n",
439
+ "# * * * \n",
440
+ "# * * * * \n",
441
+ "# * * * * * \n",
442
+ "for i in range(1, 6):\n",
443
+ " print(\"* \" * i)"
444
+ ]
445
+ },
446
+ {
447
+ "cell_type": "code",
448
+ "execution_count": 94,
449
+ "id": "0d5cf55d",
450
+ "metadata": {},
451
+ "outputs": [
452
+ {
453
+ "name": "stdout",
454
+ "output_type": "stream",
455
+ "text": [
456
+ "120\n"
457
+ ]
458
+ }
459
+ ],
460
+ "source": [
461
+ "# Find the Factorial of a Number\n",
462
+ "n = 5\n",
463
+ "fact = 1\n",
464
+ "for i in range(1, n + 1):\n",
465
+ " fact *= i\n",
466
+ "print(fact)"
467
+ ]
468
+ },
469
+ {
470
+ "cell_type": "code",
471
+ "execution_count": 95,
472
+ "id": "cd78c599",
473
+ "metadata": {},
474
+ "outputs": [
475
+ {
476
+ "name": "stdout",
477
+ "output_type": "stream",
478
+ "text": [
479
+ "o\n",
480
+ "l\n",
481
+ "l\n",
482
+ "e\n",
483
+ "h\n"
484
+ ]
485
+ }
486
+ ],
487
+ "source": [
488
+ "# Reverse a String\n",
489
+ "word = \"hello\"\n",
490
+ "for char in reversed(word):\n",
491
+ " print(char)"
492
+ ]
493
+ },
494
+ {
495
+ "cell_type": "code",
496
+ "execution_count": 96,
497
+ "id": "d45a816d",
498
+ "metadata": {},
499
+ "outputs": [
500
+ {
501
+ "name": "stdout",
502
+ "output_type": "stream",
503
+ "text": [
504
+ "3\n"
505
+ ]
506
+ }
507
+ ],
508
+ "source": [
509
+ "# Count Vowels in a String\n",
510
+ "word = \"programming\"\n",
511
+ "vowels = \"aeiou\"\n",
512
+ "count = 0\n",
513
+ "for char in word:\n",
514
+ " if char in vowels:\n",
515
+ " count += 1\n",
516
+ "print(count)"
517
+ ]
518
+ },
519
+ {
520
+ "cell_type": "code",
521
+ "execution_count": 97,
522
+ "id": "dd03f6c7",
523
+ "metadata": {},
524
+ "outputs": [
525
+ {
526
+ "name": "stdout",
527
+ "output_type": "stream",
528
+ "text": [
529
+ "30\n"
530
+ ]
531
+ }
532
+ ],
533
+ "source": [
534
+ "# Sum of Elements in a List\n",
535
+ "numbers = [2, 4, 6, 8, 10]\n",
536
+ "total = 0\n",
537
+ "for num in numbers:\n",
538
+ " total += num\n",
539
+ "print(total)"
540
+ ]
541
+ },
542
+ {
543
+ "cell_type": "code",
544
+ "execution_count": 98,
545
+ "id": "8aeb306a",
546
+ "metadata": {},
547
+ "outputs": [
548
+ {
549
+ "name": "stdout",
550
+ "output_type": "stream",
551
+ "text": [
552
+ "[11, 12, 13]\n"
553
+ ]
554
+ }
555
+ ],
556
+ "source": [
557
+ "# Given nums = [1, 2, 3], add 10 to each number using a loop.\n",
558
+ "nums = [1, 2, 3]\n",
559
+ "for i in range(len(nums)):\n",
560
+ " nums[i] += 10\n",
561
+ "print(nums)"
562
+ ]
563
+ }
564
+ ],
565
+ "metadata": {
566
+ "kernelspec": {
567
+ "display_name": "all",
568
+ "language": "python",
569
+ "name": "python3"
570
+ },
571
+ "language_info": {
572
+ "codemirror_mode": {
573
+ "name": "ipython",
574
+ "version": 3
575
+ },
576
+ "file_extension": ".py",
577
+ "mimetype": "text/x-python",
578
+ "name": "python",
579
+ "nbconvert_exporter": "python",
580
+ "pygments_lexer": "ipython3",
581
+ "version": "3.12.9"
582
+ }
583
+ },
584
+ "nbformat": 4,
585
+ "nbformat_minor": 5
586
+ }
combind_dic_list_loops.ipynb ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "e545f9aa",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Combine Operations"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 1,
14
+ "id": "8debcab5",
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "name": "stdout",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
22
+ ]
23
+ }
24
+ ],
25
+ "source": [
26
+ "dic = {}\n",
27
+ "for x in range(1, 6):\n",
28
+ " dic[x] = x ** 2\n",
29
+ " # dic.update({x: x ** 2})\n",
30
+ "print(dic)"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 2,
36
+ "id": "19abd5c1",
37
+ "metadata": {},
38
+ "outputs": [
39
+ {
40
+ "name": "stdout",
41
+ "output_type": "stream",
42
+ "text": [
43
+ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
44
+ ]
45
+ }
46
+ ],
47
+ "source": [
48
+ "squares = {x: x ** 2 for x in range(1, 6)}\n",
49
+ "print(squares)"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": 3,
55
+ "id": "e842f3d7",
56
+ "metadata": {},
57
+ "outputs": [
58
+ {
59
+ "name": "stdout",
60
+ "output_type": "stream",
61
+ "text": [
62
+ "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n"
63
+ ]
64
+ }
65
+ ],
66
+ "source": [
67
+ "dic = {}\n",
68
+ "for x in range(1, 11):\n",
69
+ " if x % 2 == 0:\n",
70
+ " dic[x] = x ** 2\n",
71
+ " # dic.update({x: x ** 2})\n",
72
+ "print(dic)"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": 4,
78
+ "id": "44546b3d",
79
+ "metadata": {},
80
+ "outputs": [
81
+ {
82
+ "name": "stdout",
83
+ "output_type": "stream",
84
+ "text": [
85
+ "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n"
86
+ ]
87
+ }
88
+ ],
89
+ "source": [
90
+ "squares = {x: x ** 2 for x in range(1, 11) if x % 2 == 0}\n",
91
+ "print(squares)"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": 5,
97
+ "id": "56cdc396",
98
+ "metadata": {},
99
+ "outputs": [],
100
+ "source": [
101
+ "users = [\n",
102
+ " {\n",
103
+ " \"_id\": \"u12345\",\n",
104
+ " \"name\": \"Rahman\",\n",
105
+ " \"email\": \"rahman@example.com\",\n",
106
+ " \"age\": 28,\n",
107
+ " \"isActive\": True,\n",
108
+ " \"address\": {\n",
109
+ " \"street\": \"123 Lakeview Road\",\n",
110
+ " \"city\": \"Dhaka\",\n",
111
+ " \"zip\": \"1207\"\n",
112
+ " },\n",
113
+ " \"hobbies\": [\"reading\", \"gaming\", \"traveling\"],\n",
114
+ " \"orders\": [\n",
115
+ " {\n",
116
+ " \"order_id\": \"ord1001\",\n",
117
+ " \"date\": \"2025-05-01\",\n",
118
+ " \"items\": [\n",
119
+ " {\"product\": \"Book\", \"price\": 12.99, \"quantity\": 1},\n",
120
+ " {\"product\": \"Pen\", \"price\": 1.99, \"quantity\": 3}\n",
121
+ " ],\n",
122
+ " \"total\": 18.96\n",
123
+ " },\n",
124
+ " {\n",
125
+ " \"order_id\": \"ord1002\",\n",
126
+ " \"date\": \"2025-05-04\",\n",
127
+ " \"items\": [\n",
128
+ " {\"product\": \"Shoes\", \"price\": 45.00, \"quantity\": 1}\n",
129
+ " ],\n",
130
+ " \"total\": 45.00\n",
131
+ " }\n",
132
+ " ]\n",
133
+ " },\n",
134
+ " {\n",
135
+ " \"_id\": \"u12346\",\n",
136
+ " \"name\": \"Fatima\",\n",
137
+ " \"email\": \"fatima@example.com\",\n",
138
+ " \"age\": 32,\n",
139
+ " \"isActive\": False,\n",
140
+ " \"address\": {\n",
141
+ " \"street\": \"78 Rose Lane\",\n",
142
+ " \"city\": \"Chittagong\",\n",
143
+ " \"zip\": \"4000\"\n",
144
+ " },\n",
145
+ " \"hobbies\": [\"painting\", \"yoga\"],\n",
146
+ " \"orders\": [\n",
147
+ " {\n",
148
+ " \"order_id\": \"ord1003\",\n",
149
+ " \"date\": \"2025-05-02\",\n",
150
+ " \"items\": [\n",
151
+ " {\"product\": \"Canvas\", \"price\": 25.50, \"quantity\": 2}\n",
152
+ " ],\n",
153
+ " \"total\": 51.00\n",
154
+ " }\n",
155
+ " ]\n",
156
+ " },\n",
157
+ " {\n",
158
+ " \"_id\": \"u12347\",\n",
159
+ " \"name\": \"Karim\",\n",
160
+ " \"email\": \"karim@example.com\",\n",
161
+ " \"age\": 40,\n",
162
+ " \"isActive\": True,\n",
163
+ " \"address\": {\n",
164
+ " \"street\": \"10 Green Street\",\n",
165
+ " \"city\": \"Khulna\",\n",
166
+ " \"zip\": \"9100\"\n",
167
+ " },\n",
168
+ " \"hobbies\": [\"fishing\", \"cycling\", \"chess\"],\n",
169
+ " \"orders\": []\n",
170
+ " },\n",
171
+ " {\n",
172
+ " \"_id\": \"u12348\",\n",
173
+ " \"name\": \"Ayesha\",\n",
174
+ " \"email\": \"ayesha@example.com\",\n",
175
+ " \"age\": 25,\n",
176
+ " \"isActive\": True,\n",
177
+ " \"address\": {\n",
178
+ " \"street\": \"56 Mango Street\",\n",
179
+ " \"city\": \"Sylhet\",\n",
180
+ " \"zip\": \"3100\"\n",
181
+ " },\n",
182
+ " \"hobbies\": [\"dancing\", \"baking\"],\n",
183
+ " \"orders\": [\n",
184
+ " {\n",
185
+ " \"order_id\": \"ord1004\",\n",
186
+ " \"date\": \"2025-05-05\",\n",
187
+ " \"items\": [\n",
188
+ " {\"product\": \"Dress\", \"price\": 30.00, \"quantity\": 1},\n",
189
+ " {\"product\": \"Handbag\", \"price\": 40.00, \"quantity\": 1}\n",
190
+ " ],\n",
191
+ " \"total\": 70.00\n",
192
+ " }\n",
193
+ " ]\n",
194
+ " }\n",
195
+ "]"
196
+ ]
197
+ },
198
+ {
199
+ "cell_type": "code",
200
+ "execution_count": 6,
201
+ "id": "11151195",
202
+ "metadata": {},
203
+ "outputs": [
204
+ {
205
+ "data": {
206
+ "text/plain": [
207
+ "[{'_id': 'u12345',\n",
208
+ " 'name': 'Rahman',\n",
209
+ " 'email': 'rahman@example.com',\n",
210
+ " 'age': 28,\n",
211
+ " 'isActive': True,\n",
212
+ " 'address': {'street': '123 Lakeview Road', 'city': 'Dhaka', 'zip': '1207'},\n",
213
+ " 'hobbies': ['reading', 'gaming', 'traveling'],\n",
214
+ " 'orders': [{'order_id': 'ord1001',\n",
215
+ " 'date': '2025-05-01',\n",
216
+ " 'items': [{'product': 'Book', 'price': 12.99, 'quantity': 1},\n",
217
+ " {'product': 'Pen', 'price': 1.99, 'quantity': 3}],\n",
218
+ " 'total': 18.96},\n",
219
+ " {'order_id': 'ord1002',\n",
220
+ " 'date': '2025-05-04',\n",
221
+ " 'items': [{'product': 'Shoes', 'price': 45.0, 'quantity': 1}],\n",
222
+ " 'total': 45.0}]},\n",
223
+ " {'_id': 'u12347',\n",
224
+ " 'name': 'Karim',\n",
225
+ " 'email': 'karim@example.com',\n",
226
+ " 'age': 40,\n",
227
+ " 'isActive': True,\n",
228
+ " 'address': {'street': '10 Green Street', 'city': 'Khulna', 'zip': '9100'},\n",
229
+ " 'hobbies': ['fishing', 'cycling', 'chess'],\n",
230
+ " 'orders': []},\n",
231
+ " {'_id': 'u12348',\n",
232
+ " 'name': 'Ayesha',\n",
233
+ " 'email': 'ayesha@example.com',\n",
234
+ " 'age': 25,\n",
235
+ " 'isActive': True,\n",
236
+ " 'address': {'street': '56 Mango Street', 'city': 'Sylhet', 'zip': '3100'},\n",
237
+ " 'hobbies': ['dancing', 'baking'],\n",
238
+ " 'orders': [{'order_id': 'ord1004',\n",
239
+ " 'date': '2025-05-05',\n",
240
+ " 'items': [{'product': 'Dress', 'price': 30.0, 'quantity': 1},\n",
241
+ " {'product': 'Handbag', 'price': 40.0, 'quantity': 1}],\n",
242
+ " 'total': 70.0}]}]"
243
+ ]
244
+ },
245
+ "execution_count": 6,
246
+ "metadata": {},
247
+ "output_type": "execute_result"
248
+ }
249
+ ],
250
+ "source": [
251
+ "active_users = [user for user in users if user[\"isActive\"]]\n",
252
+ "active_users"
253
+ ]
254
+ },
255
+ {
256
+ "cell_type": "code",
257
+ "execution_count": 7,
258
+ "id": "5d4e9086",
259
+ "metadata": {},
260
+ "outputs": [
261
+ {
262
+ "data": {
263
+ "text/plain": [
264
+ "[{'name': 'Rahman', 'email': 'rahman@example.com'},\n",
265
+ " {'name': 'Fatima', 'email': 'fatima@example.com'},\n",
266
+ " {'name': 'Ayesha', 'email': 'ayesha@example.com'}]"
267
+ ]
268
+ },
269
+ "execution_count": 7,
270
+ "metadata": {},
271
+ "output_type": "execute_result"
272
+ }
273
+ ],
274
+ "source": [
275
+ "users_with_orders = [\n",
276
+ " {\"name\": user[\"name\"], \"email\": user[\"email\"]}\n",
277
+ " for user in users if len(user[\"orders\"]) > 0\n",
278
+ "]\n",
279
+ "users_with_orders"
280
+ ]
281
+ },
282
+ {
283
+ "cell_type": "code",
284
+ "execution_count": 8,
285
+ "id": "75657de8",
286
+ "metadata": {},
287
+ "outputs": [
288
+ {
289
+ "data": {
290
+ "text/plain": [
291
+ "[{'name': 'Rahman', 'total_spent': 63.96},\n",
292
+ " {'name': 'Fatima', 'total_spent': 51.0},\n",
293
+ " {'name': 'Karim', 'total_spent': 0},\n",
294
+ " {'name': 'Ayesha', 'total_spent': 70.0}]"
295
+ ]
296
+ },
297
+ "execution_count": 8,
298
+ "metadata": {},
299
+ "output_type": "execute_result"
300
+ }
301
+ ],
302
+ "source": [
303
+ "user_spending = [\n",
304
+ " {\n",
305
+ " \"name\": user[\"name\"],\n",
306
+ " \"total_spent\": sum(order[\"total\"] for order in user[\"orders\"])\n",
307
+ " }\n",
308
+ " for user in users\n",
309
+ "]\n",
310
+ "user_spending"
311
+ ]
312
+ },
313
+ {
314
+ "cell_type": "code",
315
+ "execution_count": 9,
316
+ "id": "04860cbc",
317
+ "metadata": {},
318
+ "outputs": [
319
+ {
320
+ "name": "stdout",
321
+ "output_type": "stream",
322
+ "text": [
323
+ "Book\n",
324
+ "Pen\n",
325
+ "Shoes\n"
326
+ ]
327
+ }
328
+ ],
329
+ "source": [
330
+ "for user in users:\n",
331
+ " if user['name'] == 'Rahman':\n",
332
+ " for i in user['orders']:\n",
333
+ " for j in i['items']:\n",
334
+ " print(j['product'])"
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "execution_count": 12,
340
+ "id": "940feb5c",
341
+ "metadata": {},
342
+ "outputs": [
343
+ {
344
+ "data": {
345
+ "text/plain": [
346
+ "31.25"
347
+ ]
348
+ },
349
+ "execution_count": 12,
350
+ "metadata": {},
351
+ "output_type": "execute_result"
352
+ }
353
+ ],
354
+ "source": [
355
+ "avg_age = sum(user[\"age\"] for user in users) / len(users)\n",
356
+ "avg_age"
357
+ ]
358
+ }
359
+ ],
360
+ "metadata": {
361
+ "kernelspec": {
362
+ "display_name": "all",
363
+ "language": "python",
364
+ "name": "python3"
365
+ },
366
+ "language_info": {
367
+ "codemirror_mode": {
368
+ "name": "ipython",
369
+ "version": 3
370
+ },
371
+ "file_extension": ".py",
372
+ "mimetype": "text/x-python",
373
+ "name": "python",
374
+ "nbconvert_exporter": "python",
375
+ "pygments_lexer": "ipython3",
376
+ "version": "3.12.9"
377
+ }
378
+ },
379
+ "nbformat": 4,
380
+ "nbformat_minor": 5
381
+ }