|
29 | 29 | },
|
30 | 30 | {
|
31 | 31 | "cell_type": "code",
|
32 |
| - "execution_count": 9, |
| 32 | + "execution_count": 2, |
33 | 33 | "id": "124dfa4d",
|
34 | 34 | "metadata": {},
|
35 | 35 | "outputs": [
|
|
67 | 67 | },
|
68 | 68 | {
|
69 | 69 | "cell_type": "code",
|
70 |
| - "execution_count": 10, |
| 70 | + "execution_count": 3, |
71 | 71 | "id": "343cab68",
|
72 | 72 | "metadata": {},
|
73 | 73 | "outputs": [
|
|
96 | 96 | },
|
97 | 97 | {
|
98 | 98 | "cell_type": "code",
|
99 |
| - "execution_count": 11, |
| 99 | + "execution_count": 4, |
100 | 100 | "id": "38818fb9",
|
101 | 101 | "metadata": {},
|
102 | 102 | "outputs": [
|
|
106 | 106 | "True"
|
107 | 107 | ]
|
108 | 108 | },
|
109 |
| - "execution_count": 11, |
| 109 | + "execution_count": 4, |
110 | 110 | "metadata": {},
|
111 | 111 | "output_type": "execute_result"
|
112 | 112 | }
|
|
117 | 117 | },
|
118 | 118 | {
|
119 | 119 | "cell_type": "code",
|
120 |
| - "execution_count": 12, |
| 120 | + "execution_count": 5, |
121 | 121 | "id": "570adeb9",
|
122 | 122 | "metadata": {},
|
123 | 123 | "outputs": [
|
|
127 | 127 | "False"
|
128 | 128 | ]
|
129 | 129 | },
|
130 |
| - "execution_count": 12, |
| 130 | + "execution_count": 5, |
131 | 131 | "metadata": {},
|
132 | 132 | "output_type": "execute_result"
|
133 | 133 | }
|
|
147 | 147 | },
|
148 | 148 | {
|
149 | 149 | "cell_type": "code",
|
150 |
| - "execution_count": 14, |
| 150 | + "execution_count": 6, |
151 | 151 | "id": "9aa27411",
|
152 | 152 | "metadata": {},
|
153 | 153 | "outputs": [
|
|
184 | 184 | },
|
185 | 185 | {
|
186 | 186 | "cell_type": "code",
|
187 |
| - "execution_count": 13, |
| 187 | + "execution_count": 7, |
188 | 188 | "id": "f8d946cf",
|
189 | 189 | "metadata": {},
|
190 | 190 | "outputs": [
|
|
194 | 194 | "3"
|
195 | 195 | ]
|
196 | 196 | },
|
197 |
| - "execution_count": 13, |
| 197 | + "execution_count": 7, |
198 | 198 | "metadata": {},
|
199 | 199 | "output_type": "execute_result"
|
200 | 200 | }
|
|
215 | 215 | },
|
216 | 216 | {
|
217 | 217 | "cell_type": "code",
|
218 |
| - "execution_count": 21, |
| 218 | + "execution_count": 8, |
219 | 219 | "id": "0b11e898",
|
220 | 220 | "metadata": {},
|
221 | 221 | "outputs": [
|
|
225 | 225 | "False"
|
226 | 226 | ]
|
227 | 227 | },
|
228 |
| - "execution_count": 21, |
| 228 | + "execution_count": 8, |
229 | 229 | "metadata": {},
|
230 | 230 | "output_type": "execute_result"
|
231 | 231 | }
|
|
245 | 245 | },
|
246 | 246 | {
|
247 | 247 | "cell_type": "code",
|
248 |
| - "execution_count": 25, |
| 248 | + "execution_count": 9, |
249 | 249 | "id": "3fc754a7",
|
250 | 250 | "metadata": {},
|
251 | 251 | "outputs": [
|
|
256 | 256 | "traceback": [
|
257 | 257 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
258 | 258 | "\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", |
260 | 260 | "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
|
261 | 261 | ]
|
262 | 262 | }
|
|
294 | 294 | },
|
295 | 295 | {
|
296 | 296 | "cell_type": "code",
|
297 |
| - "execution_count": 3, |
| 297 | + "execution_count": 10, |
298 | 298 | "id": "2cd42f05",
|
299 | 299 | "metadata": {},
|
300 | 300 | "outputs": [
|
|
311 | 311 | "5"
|
312 | 312 | ]
|
313 | 313 | },
|
314 |
| - "execution_count": 3, |
| 314 | + "execution_count": 10, |
315 | 315 | "metadata": {},
|
316 | 316 | "output_type": "execute_result"
|
317 | 317 | }
|
|
334 | 334 | },
|
335 | 335 | {
|
336 | 336 | "cell_type": "code",
|
337 |
| - "execution_count": 4, |
| 337 | + "execution_count": 11, |
338 | 338 | "id": "ce2034e0",
|
339 | 339 | "metadata": {},
|
340 | 340 | "outputs": [
|
|
351 | 351 | "2"
|
352 | 352 | ]
|
353 | 353 | },
|
354 |
| - "execution_count": 4, |
| 354 | + "execution_count": 11, |
355 | 355 | "metadata": {},
|
356 | 356 | "output_type": "execute_result"
|
357 | 357 | }
|
|
374 | 374 | },
|
375 | 375 | {
|
376 | 376 | "cell_type": "code",
|
377 |
| - "execution_count": 6, |
| 377 | + "execution_count": 12, |
378 | 378 | "id": "04796b0e",
|
379 | 379 | "metadata": {},
|
380 | 380 | "outputs": [
|
|
384 | 384 | "False"
|
385 | 385 | ]
|
386 | 386 | },
|
387 |
| - "execution_count": 6, |
| 387 | + "execution_count": 12, |
388 | 388 | "metadata": {},
|
389 | 389 | "output_type": "execute_result"
|
390 | 390 | }
|
|
396 | 396 | },
|
397 | 397 | {
|
398 | 398 | "cell_type": "code",
|
399 |
| - "execution_count": 8, |
| 399 | + "execution_count": 13, |
400 | 400 | "id": "eadfbe2c",
|
401 | 401 | "metadata": {},
|
402 | 402 | "outputs": [
|
|
406 | 406 | "True"
|
407 | 407 | ]
|
408 | 408 | },
|
409 |
| - "execution_count": 8, |
| 409 | + "execution_count": 13, |
410 | 410 | "metadata": {},
|
411 | 411 | "output_type": "execute_result"
|
412 | 412 | }
|
|
436 | 436 | },
|
437 | 437 | {
|
438 | 438 | "cell_type": "code",
|
439 |
| - "execution_count": 16, |
| 439 | + "execution_count": 14, |
440 | 440 | "id": "07eacb6d",
|
441 | 441 | "metadata": {},
|
442 | 442 | "outputs": [
|
|
446 | 446 | "'A red fruit'"
|
447 | 447 | ]
|
448 | 448 | },
|
449 |
| - "execution_count": 16, |
| 449 | + "execution_count": 14, |
450 | 450 | "metadata": {},
|
451 | 451 | "output_type": "execute_result"
|
452 | 452 | }
|
|
489 | 489 | },
|
490 | 490 | {
|
491 | 491 | "cell_type": "code",
|
492 |
| - "execution_count": 18, |
| 492 | + "execution_count": 15, |
493 | 493 | "id": "40a2a490",
|
494 | 494 | "metadata": {},
|
495 | 495 | "outputs": [
|
|
519 | 519 | "print(my_dictionary[True])\n",
|
520 | 520 | "print(my_dictionary['5 is less than 4'])"
|
521 | 521 | ]
|
| 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": [] |
522 | 665 | }
|
523 | 666 | ],
|
524 | 667 | "metadata": {
|
|
0 commit comments