|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 | 3 | "name": "",
|
4 |
| - "signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559" |
| 4 | + "signature": "sha256:7b5c09563e39ddf2ede88b8ff357dcdc654af16292c9c86e3bca2fbf703f64c2" |
5 | 5 | },
|
6 | 6 | "nbformat": 3,
|
7 | 7 | "nbformat_minor": 0,
|
|
558 | 558 | "\n",
|
559 | 559 | "Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n",
|
560 | 560 | "\n",
|
| 561 | + "One real world use of tuples is to hold one particular record within a complex file. For example, a stock holding or a restaurant inventory.\n", |
| 562 | + "\n", |
561 | 563 | "Tuples are comma-separated lists:"
|
562 | 564 | ]
|
563 | 565 | },
|
564 | 566 | {
|
565 | 567 | "cell_type": "code",
|
566 | 568 | "collapsed": false,
|
567 | 569 | "input": [
|
568 |
| - "stuff = (1, 2, \"hello\")" |
| 570 | + "food = (\"cones\", 100, 3.79)" |
569 | 571 | ],
|
570 | 572 | "language": "python",
|
571 | 573 | "metadata": {},
|
572 | 574 | "outputs": []
|
573 | 575 | },
|
| 576 | + { |
| 577 | + "cell_type": "markdown", |
| 578 | + "metadata": {}, |
| 579 | + "source": [ |
| 580 | + "This is sometimes called packing a tuple." |
| 581 | + ] |
| 582 | + }, |
574 | 583 | {
|
575 | 584 | "cell_type": "code",
|
576 | 585 | "collapsed": false,
|
577 | 586 | "input": [
|
578 |
| - "stuff" |
| 587 | + "food" |
579 | 588 | ],
|
580 | 589 | "language": "python",
|
581 | 590 | "metadata": {},
|
|
585 | 594 | "cell_type": "code",
|
586 | 595 | "collapsed": false,
|
587 | 596 | "input": [
|
588 |
| - "type(stuff)" |
| 597 | + "type(food)" |
589 | 598 | ],
|
590 | 599 | "language": "python",
|
591 | 600 | "metadata": {},
|
|
602 | 611 | "cell_type": "code",
|
603 | 612 | "collapsed": false,
|
604 | 613 | "input": [
|
605 |
| - "stuff = 1, 2, 3" |
| 614 | + "food = \"sprinkles\", 10, 4.99" |
606 | 615 | ],
|
607 | 616 | "language": "python",
|
608 | 617 | "metadata": {},
|
|
612 | 621 | "cell_type": "code",
|
613 | 622 | "collapsed": false,
|
614 | 623 | "input": [
|
615 |
| - "stuff" |
| 624 | + "food" |
616 | 625 | ],
|
617 | 626 | "language": "python",
|
618 | 627 | "metadata": {},
|
|
629 | 638 | "cell_type": "code",
|
630 | 639 | "collapsed": false,
|
631 | 640 | "input": [
|
632 |
| - "stuff[2] = 3" |
| 641 | + "food[2] = 3" |
633 | 642 | ],
|
634 | 643 | "language": "python",
|
635 | 644 | "metadata": {},
|
|
652 | 661 | "metadata": {},
|
653 | 662 | "outputs": []
|
654 | 663 | },
|
| 664 | + { |
| 665 | + "cell_type": "code", |
| 666 | + "collapsed": false, |
| 667 | + "input": [ |
| 668 | + "empty" |
| 669 | + ], |
| 670 | + "language": "python", |
| 671 | + "metadata": {}, |
| 672 | + "outputs": [] |
| 673 | + }, |
| 674 | + { |
| 675 | + "cell_type": "code", |
| 676 | + "collapsed": false, |
| 677 | + "input": [ |
| 678 | + "type(empty)" |
| 679 | + ], |
| 680 | + "language": "python", |
| 681 | + "metadata": {}, |
| 682 | + "outputs": [] |
| 683 | + }, |
655 | 684 | {
|
656 | 685 | "cell_type": "markdown",
|
657 | 686 | "metadata": {},
|
|
663 | 692 | "cell_type": "code",
|
664 | 693 | "collapsed": false,
|
665 | 694 | "input": [
|
666 |
| - "things = (3)" |
| 695 | + "food = (3)" |
667 | 696 | ],
|
668 | 697 | "language": "python",
|
669 | 698 | "metadata": {},
|
|
673 | 702 | "cell_type": "code",
|
674 | 703 | "collapsed": false,
|
675 | 704 | "input": [
|
676 |
| - "things" |
| 705 | + "food" |
677 | 706 | ],
|
678 | 707 | "language": "python",
|
679 | 708 | "metadata": {},
|
|
683 | 712 | "cell_type": "code",
|
684 | 713 | "collapsed": false,
|
685 | 714 | "input": [
|
686 |
| - "type(things)" |
| 715 | + "type(food)" |
687 | 716 | ],
|
688 | 717 | "language": "python",
|
689 | 718 | "metadata": {},
|
|
700 | 729 | "cell_type": "code",
|
701 | 730 | "collapsed": false,
|
702 | 731 | "input": [
|
703 |
| - "things = (3,)" |
| 732 | + "food = (3,)" |
704 | 733 | ],
|
705 | 734 | "language": "python",
|
706 | 735 | "metadata": {},
|
|
710 | 739 | "cell_type": "code",
|
711 | 740 | "collapsed": false,
|
712 | 741 | "input": [
|
713 |
| - "things" |
| 742 | + "food" |
714 | 743 | ],
|
715 | 744 | "language": "python",
|
716 | 745 | "metadata": {},
|
717 | 746 | "outputs": []
|
| 747 | + }, |
| 748 | + { |
| 749 | + "cell_type": "code", |
| 750 | + "collapsed": false, |
| 751 | + "input": [ |
| 752 | + "type(food)" |
| 753 | + ], |
| 754 | + "language": "python", |
| 755 | + "metadata": {}, |
| 756 | + "outputs": [] |
| 757 | + }, |
| 758 | + { |
| 759 | + "cell_type": "markdown", |
| 760 | + "metadata": {}, |
| 761 | + "source": [ |
| 762 | + "Sometimes, unpacking a tuple is also desired." |
| 763 | + ] |
| 764 | + }, |
| 765 | + { |
| 766 | + "cell_type": "code", |
| 767 | + "collapsed": false, |
| 768 | + "input": [ |
| 769 | + "yummies = [(\"cones\", 100, 3.79), (\"sprinkles\", 10, 4.99), (\"hot fudge\", 8, 3.29), (\"nuts\", 6, 8.99)]" |
| 770 | + ], |
| 771 | + "language": "python", |
| 772 | + "metadata": {}, |
| 773 | + "outputs": [] |
| 774 | + }, |
| 775 | + { |
| 776 | + "cell_type": "code", |
| 777 | + "collapsed": false, |
| 778 | + "input": [ |
| 779 | + "yummies[2]" |
| 780 | + ], |
| 781 | + "language": "python", |
| 782 | + "metadata": {}, |
| 783 | + "outputs": [] |
| 784 | + }, |
| 785 | + { |
| 786 | + "cell_type": "code", |
| 787 | + "collapsed": false, |
| 788 | + "input": [ |
| 789 | + "name, amount, price = yummies[2]" |
| 790 | + ], |
| 791 | + "language": "python", |
| 792 | + "metadata": {}, |
| 793 | + "outputs": [] |
| 794 | + }, |
| 795 | + { |
| 796 | + "cell_type": "code", |
| 797 | + "collapsed": false, |
| 798 | + "input": [ |
| 799 | + "print(\"Our ice cream shop serves \" + name + \".\")" |
| 800 | + ], |
| 801 | + "language": "python", |
| 802 | + "metadata": {}, |
| 803 | + "outputs": [] |
| 804 | + }, |
| 805 | + { |
| 806 | + "cell_type": "code", |
| 807 | + "collapsed": false, |
| 808 | + "input": [ |
| 809 | + "print(\"The shop's inventory value for \" + name + \" is $\" + str(amount * price) + \".\")" |
| 810 | + ], |
| 811 | + "language": "python", |
| 812 | + "metadata": {}, |
| 813 | + "outputs": [] |
| 814 | + }, |
| 815 | + { |
| 816 | + "cell_type": "code", |
| 817 | + "collapsed": false, |
| 818 | + "input": [ |
| 819 | + "print(\"All this food talk is making me hungry for \" + yummies[3][0] + \".\")" |
| 820 | + ], |
| 821 | + "language": "python", |
| 822 | + "metadata": {}, |
| 823 | + "outputs": [] |
| 824 | + }, |
| 825 | + { |
| 826 | + "cell_type": "markdown", |
| 827 | + "metadata": {}, |
| 828 | + "source": [ |
| 829 | + "Why not always use lists instead of tuples? Tuples use less storage space than a list. So if you are reading 1000s of small records from a file, a tuple can be an efficient way to go.\n", |
| 830 | + "\n", |
| 831 | + "A practical learning tip: When learning a new language, there are many new terms and concepts. Sometimes it is helpful to see a bigger picture of how everything comes together. I'm a lover of \"Table of Contents\" since I can scan the big picture and see where something like a tuple fits in. The official Python tutorial has a very good [Table of Contents]( https://docs.python.org/3.4/tutorial/index.html), and I sometimes take 5 minutes to just click and explore something new to me.\n", |
| 832 | + "\n", |
| 833 | + "Wow! We've come a long way today. " |
| 834 | + ] |
| 835 | + }, |
| 836 | + { |
| 837 | + "cell_type": "code", |
| 838 | + "collapsed": false, |
| 839 | + "input": [], |
| 840 | + "language": "python", |
| 841 | + "metadata": {}, |
| 842 | + "outputs": [] |
718 | 843 | }
|
719 | 844 | ],
|
720 | 845 | "metadata": {}
|
|
0 commit comments