Skip to content

Commit 7837f41

Browse files
author
K-Waks
committed
added dictionary mapping
1 parent 2c35967 commit 7837f41

File tree

1 file changed

+167
-24
lines changed

1 file changed

+167
-24
lines changed

3) data structures.ipynb

+167-24
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"cell_type": "code",
32-
"execution_count": 9,
32+
"execution_count": 2,
3333
"id": "124dfa4d",
3434
"metadata": {},
3535
"outputs": [
@@ -67,7 +67,7 @@
6767
},
6868
{
6969
"cell_type": "code",
70-
"execution_count": 10,
70+
"execution_count": 3,
7171
"id": "343cab68",
7272
"metadata": {},
7373
"outputs": [
@@ -96,7 +96,7 @@
9696
},
9797
{
9898
"cell_type": "code",
99-
"execution_count": 11,
99+
"execution_count": 4,
100100
"id": "38818fb9",
101101
"metadata": {},
102102
"outputs": [
@@ -106,7 +106,7 @@
106106
"True"
107107
]
108108
},
109-
"execution_count": 11,
109+
"execution_count": 4,
110110
"metadata": {},
111111
"output_type": "execute_result"
112112
}
@@ -117,7 +117,7 @@
117117
},
118118
{
119119
"cell_type": "code",
120-
"execution_count": 12,
120+
"execution_count": 5,
121121
"id": "570adeb9",
122122
"metadata": {},
123123
"outputs": [
@@ -127,7 +127,7 @@
127127
"False"
128128
]
129129
},
130-
"execution_count": 12,
130+
"execution_count": 5,
131131
"metadata": {},
132132
"output_type": "execute_result"
133133
}
@@ -147,7 +147,7 @@
147147
},
148148
{
149149
"cell_type": "code",
150-
"execution_count": 14,
150+
"execution_count": 6,
151151
"id": "9aa27411",
152152
"metadata": {},
153153
"outputs": [
@@ -184,7 +184,7 @@
184184
},
185185
{
186186
"cell_type": "code",
187-
"execution_count": 13,
187+
"execution_count": 7,
188188
"id": "f8d946cf",
189189
"metadata": {},
190190
"outputs": [
@@ -194,7 +194,7 @@
194194
"3"
195195
]
196196
},
197-
"execution_count": 13,
197+
"execution_count": 7,
198198
"metadata": {},
199199
"output_type": "execute_result"
200200
}
@@ -215,7 +215,7 @@
215215
},
216216
{
217217
"cell_type": "code",
218-
"execution_count": 21,
218+
"execution_count": 8,
219219
"id": "0b11e898",
220220
"metadata": {},
221221
"outputs": [
@@ -225,7 +225,7 @@
225225
"False"
226226
]
227227
},
228-
"execution_count": 21,
228+
"execution_count": 8,
229229
"metadata": {},
230230
"output_type": "execute_result"
231231
}
@@ -245,7 +245,7 @@
245245
},
246246
{
247247
"cell_type": "code",
248-
"execution_count": 25,
248+
"execution_count": 9,
249249
"id": "3fc754a7",
250250
"metadata": {},
251251
"outputs": [
@@ -256,7 +256,7 @@
256256
"traceback": [
257257
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
258258
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
259-
"Input \u001b[0;32mIn [25]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_tuple\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;241m4\u001b[39m)\n",
259+
"Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m my_tuple\u001b[39m.\u001b[39;49mappend(\u001b[39m4\u001b[39m)\n",
260260
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
261261
]
262262
}
@@ -294,7 +294,7 @@
294294
},
295295
{
296296
"cell_type": "code",
297-
"execution_count": 3,
297+
"execution_count": 10,
298298
"id": "2cd42f05",
299299
"metadata": {},
300300
"outputs": [
@@ -311,7 +311,7 @@
311311
"5"
312312
]
313313
},
314-
"execution_count": 3,
314+
"execution_count": 10,
315315
"metadata": {},
316316
"output_type": "execute_result"
317317
}
@@ -334,7 +334,7 @@
334334
},
335335
{
336336
"cell_type": "code",
337-
"execution_count": 4,
337+
"execution_count": 11,
338338
"id": "ce2034e0",
339339
"metadata": {},
340340
"outputs": [
@@ -351,7 +351,7 @@
351351
"2"
352352
]
353353
},
354-
"execution_count": 4,
354+
"execution_count": 11,
355355
"metadata": {},
356356
"output_type": "execute_result"
357357
}
@@ -374,7 +374,7 @@
374374
},
375375
{
376376
"cell_type": "code",
377-
"execution_count": 6,
377+
"execution_count": 12,
378378
"id": "04796b0e",
379379
"metadata": {},
380380
"outputs": [
@@ -384,7 +384,7 @@
384384
"False"
385385
]
386386
},
387-
"execution_count": 6,
387+
"execution_count": 12,
388388
"metadata": {},
389389
"output_type": "execute_result"
390390
}
@@ -396,7 +396,7 @@
396396
},
397397
{
398398
"cell_type": "code",
399-
"execution_count": 8,
399+
"execution_count": 13,
400400
"id": "eadfbe2c",
401401
"metadata": {},
402402
"outputs": [
@@ -406,7 +406,7 @@
406406
"True"
407407
]
408408
},
409-
"execution_count": 8,
409+
"execution_count": 13,
410410
"metadata": {},
411411
"output_type": "execute_result"
412412
}
@@ -436,7 +436,7 @@
436436
},
437437
{
438438
"cell_type": "code",
439-
"execution_count": 16,
439+
"execution_count": 14,
440440
"id": "07eacb6d",
441441
"metadata": {},
442442
"outputs": [
@@ -446,7 +446,7 @@
446446
"'A red fruit'"
447447
]
448448
},
449-
"execution_count": 16,
449+
"execution_count": 14,
450450
"metadata": {},
451451
"output_type": "execute_result"
452452
}
@@ -489,7 +489,7 @@
489489
},
490490
{
491491
"cell_type": "code",
492-
"execution_count": 18,
492+
"execution_count": 15,
493493
"id": "40a2a490",
494494
"metadata": {},
495495
"outputs": [
@@ -519,6 +519,149 @@
519519
"print(my_dictionary[True])\n",
520520
"print(my_dictionary['5 is less than 4'])"
521521
]
522+
},
523+
{
524+
"attachments": {},
525+
"cell_type": "markdown",
526+
"id": "b6f9fcd8",
527+
"metadata": {},
528+
"source": [
529+
"## Dictionary mapping"
530+
]
531+
},
532+
{
533+
"attachments": {},
534+
"cell_type": "markdown",
535+
"id": "01c5f9f7",
536+
"metadata": {},
537+
"source": [
538+
"- Use the `get()` method to get the value of a key."
539+
]
540+
},
541+
{
542+
"cell_type": "code",
543+
"execution_count": 16,
544+
"id": "28a8b1d7",
545+
"metadata": {},
546+
"outputs": [
547+
{
548+
"data": {
549+
"text/plain": [
550+
"'Sometimes a green fruit'"
551+
]
552+
},
553+
"execution_count": 16,
554+
"metadata": {},
555+
"output_type": "execute_result"
556+
}
557+
],
558+
"source": [
559+
"my_dictionary.get(\"apple\")"
560+
]
561+
},
562+
{
563+
"attachments": {},
564+
"cell_type": "markdown",
565+
"id": "2b129ecb",
566+
"metadata": {},
567+
"source": [
568+
"- You can also specify a default value to be returned with `get()` when a key is not in the dictionary. "
569+
]
570+
},
571+
{
572+
"cell_type": "code",
573+
"execution_count": 20,
574+
"id": "d1c42d23",
575+
"metadata": {},
576+
"outputs": [
577+
{
578+
"data": {
579+
"text/plain": [
580+
"'One of my favorite foods'"
581+
]
582+
},
583+
"execution_count": 20,
584+
"metadata": {},
585+
"output_type": "execute_result"
586+
}
587+
],
588+
"source": [
589+
"my_dictionary.get(\"pancakes\", \"One of my favorite foods\")"
590+
]
591+
},
592+
{
593+
"attachments": {},
594+
"cell_type": "markdown",
595+
"id": "1d1803f9",
596+
"metadata": {},
597+
"source": [
598+
"- This can be useful in the sort of a `match-case` scenario."
599+
]
600+
},
601+
{
602+
"cell_type": "code",
603+
"execution_count": 21,
604+
"id": "a15f1fca",
605+
"metadata": {},
606+
"outputs": [
607+
{
608+
"name": "stdout",
609+
"output_type": "stream",
610+
"text": [
611+
"Starting the program\n"
612+
]
613+
}
614+
],
615+
"source": [
616+
"command = input(\"Enter a command: \")\n",
617+
"match command:\n",
618+
" case \"start\":\n",
619+
" print(\"Starting the program\")\n",
620+
" case \"stop\":\n",
621+
" print(\"Stopping the program\")\n",
622+
" case \"help\":\n",
623+
" print(\"Showing the help menu\")\n",
624+
" case _:\n",
625+
" print(\"Invalid command\")"
626+
]
627+
},
628+
{
629+
"cell_type": "code",
630+
"execution_count": 25,
631+
"id": "92b28113",
632+
"metadata": {},
633+
"outputs": [
634+
{
635+
"name": "stdout",
636+
"output_type": "stream",
637+
"text": [
638+
"Starting the program\n",
639+
"\n",
640+
"Invalid command\n"
641+
]
642+
}
643+
],
644+
"source": [
645+
"def switch(input):\n",
646+
"\n",
647+
" command = {\"start\": \"Starting the program\",\n",
648+
" \"stop\": \"Stopping the program\",\n",
649+
" \"help\": \"Showing the help menu\"}\n",
650+
"\n",
651+
" return command.get(input, \"Invalid command\")\n",
652+
"\n",
653+
"print(switch(\"start\"))\n",
654+
"print()\n",
655+
"print(switch(\"pause\"))"
656+
]
657+
},
658+
{
659+
"cell_type": "code",
660+
"execution_count": null,
661+
"id": "6d45948b",
662+
"metadata": {},
663+
"outputs": [],
664+
"source": []
522665
}
523666
],
524667
"metadata": {

0 commit comments

Comments
 (0)