|
513 | 513 | "id": "9838891c", |
514 | 514 | "metadata": {}, |
515 | 515 | "source": [ |
516 | | - "### Walrus Operator: Assign a Variable in an Expression" |
| 516 | + "### Write Cleaner Python with the Walrus Operatorn" |
517 | 517 | ] |
518 | 518 | }, |
519 | 519 | { |
|
522 | 522 | "id": "b2477b9e", |
523 | 523 | "metadata": {}, |
524 | 524 | "source": [ |
525 | | - "The walrus operator (`:=`) in Python 3.8 and above allows you to assign a variable in an expression. The walrus operator is useful when you want to:\n", |
526 | | - "- Debug the components in an expression\n", |
527 | | - "- Avoid repeated computations\n", |
528 | | - "- Assign a meaningful name to an expression" |
| 525 | + "The walrus operator (`:=`) in Python 3.8+ allows you to assign a variable in an expression, making your code more readable and efficient. It's useful in two main scenarios:\n", |
| 526 | + "\n", |
| 527 | + "1. Giving a meaningful name to a complex expression for better readability.\n", |
| 528 | + "2. Avoiding repeated computations by reusing a variable instead of recomputing the expression." |
| 529 | + ] |
| 530 | + }, |
| 531 | + { |
| 532 | + "cell_type": "markdown", |
| 533 | + "id": "9739695b-7ddb-4997-9408-56bd7e53dd30", |
| 534 | + "metadata": {}, |
| 535 | + "source": [ |
| 536 | + "Let's consider an example where we want to calculate the radius, area, and volume of a circle given its diameter and height:" |
529 | 537 | ] |
530 | 538 | }, |
531 | 539 | { |
532 | 540 | "cell_type": "code", |
533 | | - "execution_count": null, |
| 541 | + "execution_count": 10, |
534 | 542 | "id": "68b0e1a3", |
535 | 543 | "metadata": {}, |
536 | 544 | "outputs": [], |
537 | 545 | "source": [ |
538 | 546 | "from math import pi\n", |
539 | 547 | "\n", |
540 | | - "diameter = 4\n" |
| 548 | + "diameter = 4\n", |
| 549 | + "height = 2" |
| 550 | + ] |
| 551 | + }, |
| 552 | + { |
| 553 | + "cell_type": "markdown", |
| 554 | + "id": "d43f5da9-89e1-4e8a-b29d-f48ad247a9d0", |
| 555 | + "metadata": {}, |
| 556 | + "source": [ |
| 557 | + "Without the walrus operator, we might compute the radius and area multiple times:" |
541 | 558 | ] |
542 | 559 | }, |
543 | 560 | { |
544 | 561 | "cell_type": "code", |
545 | | - "execution_count": 16, |
| 562 | + "execution_count": 12, |
546 | 563 | "id": "14cfc887", |
547 | 564 | "metadata": {}, |
548 | 565 | "outputs": [ |
|
552 | 569 | "2.0" |
553 | 570 | ] |
554 | 571 | }, |
555 | | - "execution_count": 16, |
| 572 | + "execution_count": 12, |
556 | 573 | "metadata": {}, |
557 | 574 | "output_type": "execute_result" |
558 | 575 | } |
559 | 576 | ], |
560 | 577 | "source": [ |
561 | | - "# without Walrus operator\n", |
562 | | - "\n", |
563 | 578 | "circle = {\n", |
564 | 579 | " \"radius\": diameter / 2, # computed twice\n", |
565 | | - " \"area\": pi * (diameter / 2)**2,\n", |
566 | | - "}\n", |
567 | | - "\n", |
568 | | - "diameter / 2\n" |
| 580 | + " \"area\": pi * (diameter / 2)**2, # computed twice\n", |
| 581 | + " \"volume\": pi * (diameter / 2)**2 * height,\n", |
| 582 | + "}" |
| 583 | + ] |
| 584 | + }, |
| 585 | + { |
| 586 | + "cell_type": "markdown", |
| 587 | + "id": "4c9997c8-5069-4723-8fc1-16f823304760", |
| 588 | + "metadata": {}, |
| 589 | + "source": [ |
| 590 | + "To avoid repeated computations, we can assign the radius and area to variables before creating the dictionary:" |
569 | 591 | ] |
570 | 592 | }, |
571 | 593 | { |
572 | 594 | "cell_type": "code", |
573 | | - "execution_count": 15, |
574 | | - "id": "805e31ea", |
| 595 | + "execution_count": 13, |
| 596 | + "id": "b18fb3eb-45b2-43bb-93d5-7a0cd6ded503", |
575 | 597 | "metadata": {}, |
576 | 598 | "outputs": [ |
577 | 599 | { |
|
580 | 602 | "2.0" |
581 | 603 | ] |
582 | 604 | }, |
583 | | - "execution_count": 15, |
| 605 | + "execution_count": 13, |
584 | 606 | "metadata": {}, |
585 | 607 | "output_type": "execute_result" |
586 | 608 | } |
587 | 609 | ], |
588 | 610 | "source": [ |
589 | | - "# with Walrus operator\n", |
| 611 | + "radius = diameter / 2\n", |
| 612 | + "area = pi * radius**2\n", |
590 | 613 | "\n", |
| 614 | + "circle = {\n", |
| 615 | + " \"radius\": radius,\n", |
| 616 | + " \"area\": area,\n", |
| 617 | + " \"volume\": area * height,\n", |
| 618 | + "}" |
| 619 | + ] |
| 620 | + }, |
| 621 | + { |
| 622 | + "cell_type": "markdown", |
| 623 | + "id": "d220fa6a-6ca4-41c2-a1bb-e0b458fb03dc", |
| 624 | + "metadata": {}, |
| 625 | + "source": [ |
| 626 | + "To make the code more concise, we can use the walrus operator to assign the radius and area to variables while creating the dictionary." |
| 627 | + ] |
| 628 | + }, |
| 629 | + { |
| 630 | + "cell_type": "code", |
| 631 | + "execution_count": 16, |
| 632 | + "id": "805e31ea", |
| 633 | + "metadata": {}, |
| 634 | + "outputs": [], |
| 635 | + "source": [ |
591 | 636 | "circle = {\n", |
592 | 637 | " \"radius\": (radius := diameter / 2),\n", |
593 | | - " \"area\": pi * radius**2,\n", |
594 | | - "}\n", |
595 | | - "\n", |
596 | | - "radius" |
| 638 | + " \"area\": (area := pi * radius**2),\n", |
| 639 | + " \"volume\": area * height,\n", |
| 640 | + "}" |
| 641 | + ] |
| 642 | + }, |
| 643 | + { |
| 644 | + "cell_type": "markdown", |
| 645 | + "id": "0719cf42-6f2b-4f38-b5a2-12e4765111f7", |
| 646 | + "metadata": {}, |
| 647 | + "source": [ |
| 648 | + "After executing the code with the walrus operator, we can access the assigned variables:" |
| 649 | + ] |
| 650 | + }, |
| 651 | + { |
| 652 | + "cell_type": "code", |
| 653 | + "execution_count": 17, |
| 654 | + "id": "91908a69-8f78-42c8-b56b-cc12d6ac28d4", |
| 655 | + "metadata": {}, |
| 656 | + "outputs": [ |
| 657 | + { |
| 658 | + "name": "stdout", |
| 659 | + "output_type": "stream", |
| 660 | + "text": [ |
| 661 | + "2.0\n", |
| 662 | + "12.566370614359172\n" |
| 663 | + ] |
| 664 | + } |
| 665 | + ], |
| 666 | + "source": [ |
| 667 | + "print(radius)\n", |
| 668 | + "print(area)" |
597 | 669 | ] |
598 | 670 | }, |
599 | 671 | { |
|
713 | 785 | ], |
714 | 786 | "metadata": { |
715 | 787 | "kernelspec": { |
716 | | - "display_name": "Python 3", |
| 788 | + "display_name": "Python 3 (ipykernel)", |
717 | 789 | "language": "python", |
718 | 790 | "name": "python3" |
719 | 791 | }, |
|
0 commit comments