Skip to content

Commit a1724ef

Browse files
committed
Merge pull request #41 from willingc/part4-tuple
Adds detail to tuple section
2 parents 5c714c3 + e6c825a commit a1724ef

File tree

1 file changed

+137
-12
lines changed

1 file changed

+137
-12
lines changed

part-4.ipynb

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559"
4+
"signature": "sha256:7b5c09563e39ddf2ede88b8ff357dcdc654af16292c9c86e3bca2fbf703f64c2"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -558,24 +558,33 @@
558558
"\n",
559559
"Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n",
560560
"\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",
561563
"Tuples are comma-separated lists:"
562564
]
563565
},
564566
{
565567
"cell_type": "code",
566568
"collapsed": false,
567569
"input": [
568-
"stuff = (1, 2, \"hello\")"
570+
"food = (\"cones\", 100, 3.79)"
569571
],
570572
"language": "python",
571573
"metadata": {},
572574
"outputs": []
573575
},
576+
{
577+
"cell_type": "markdown",
578+
"metadata": {},
579+
"source": [
580+
"This is sometimes called packing a tuple."
581+
]
582+
},
574583
{
575584
"cell_type": "code",
576585
"collapsed": false,
577586
"input": [
578-
"stuff"
587+
"food"
579588
],
580589
"language": "python",
581590
"metadata": {},
@@ -585,7 +594,7 @@
585594
"cell_type": "code",
586595
"collapsed": false,
587596
"input": [
588-
"type(stuff)"
597+
"type(food)"
589598
],
590599
"language": "python",
591600
"metadata": {},
@@ -602,7 +611,7 @@
602611
"cell_type": "code",
603612
"collapsed": false,
604613
"input": [
605-
"stuff = 1, 2, 3"
614+
"food = \"sprinkles\", 10, 4.99"
606615
],
607616
"language": "python",
608617
"metadata": {},
@@ -612,7 +621,7 @@
612621
"cell_type": "code",
613622
"collapsed": false,
614623
"input": [
615-
"stuff"
624+
"food"
616625
],
617626
"language": "python",
618627
"metadata": {},
@@ -629,7 +638,7 @@
629638
"cell_type": "code",
630639
"collapsed": false,
631640
"input": [
632-
"stuff[2] = 3"
641+
"food[2] = 3"
633642
],
634643
"language": "python",
635644
"metadata": {},
@@ -652,6 +661,26 @@
652661
"metadata": {},
653662
"outputs": []
654663
},
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+
},
655684
{
656685
"cell_type": "markdown",
657686
"metadata": {},
@@ -663,7 +692,7 @@
663692
"cell_type": "code",
664693
"collapsed": false,
665694
"input": [
666-
"things = (3)"
695+
"food = (3)"
667696
],
668697
"language": "python",
669698
"metadata": {},
@@ -673,7 +702,7 @@
673702
"cell_type": "code",
674703
"collapsed": false,
675704
"input": [
676-
"things"
705+
"food"
677706
],
678707
"language": "python",
679708
"metadata": {},
@@ -683,7 +712,7 @@
683712
"cell_type": "code",
684713
"collapsed": false,
685714
"input": [
686-
"type(things)"
715+
"type(food)"
687716
],
688717
"language": "python",
689718
"metadata": {},
@@ -700,7 +729,7 @@
700729
"cell_type": "code",
701730
"collapsed": false,
702731
"input": [
703-
"things = (3,)"
732+
"food = (3,)"
704733
],
705734
"language": "python",
706735
"metadata": {},
@@ -710,11 +739,107 @@
710739
"cell_type": "code",
711740
"collapsed": false,
712741
"input": [
713-
"things"
742+
"food"
714743
],
715744
"language": "python",
716745
"metadata": {},
717746
"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": []
718843
}
719844
],
720845
"metadata": {}

0 commit comments

Comments
 (0)