Skip to content

Commit 89ae76b

Browse files
committed
Adds detail to tuple section
1 parent 6821da7 commit 89ae76b

File tree

1 file changed

+126
-12
lines changed

1 file changed

+126
-12
lines changed

part-4.ipynb

Lines changed: 126 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:c233bf101727c5056cde182a01a23f07f07d4bfdefeaf7e93814b7b6cddc0478"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -518,24 +518,33 @@
518518
"\n",
519519
"Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n",
520520
"\n",
521+
"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",
522+
"\n",
521523
"Tuples are comma-separated lists:"
522524
]
523525
},
524526
{
525527
"cell_type": "code",
526528
"collapsed": false,
527529
"input": [
528-
"stuff = (1, 2, \"hello\")"
530+
"food = (\"cones\", 100, 3.79)"
529531
],
530532
"language": "python",
531533
"metadata": {},
532534
"outputs": []
533535
},
536+
{
537+
"cell_type": "markdown",
538+
"metadata": {},
539+
"source": [
540+
"This is sometimes called packing a tuple."
541+
]
542+
},
534543
{
535544
"cell_type": "code",
536545
"collapsed": false,
537546
"input": [
538-
"stuff"
547+
"food"
539548
],
540549
"language": "python",
541550
"metadata": {},
@@ -545,7 +554,7 @@
545554
"cell_type": "code",
546555
"collapsed": false,
547556
"input": [
548-
"type(stuff)"
557+
"type(food)"
549558
],
550559
"language": "python",
551560
"metadata": {},
@@ -562,7 +571,7 @@
562571
"cell_type": "code",
563572
"collapsed": false,
564573
"input": [
565-
"stuff = 1, 2, 3"
574+
"food = \"sprinkles\", 10, 4.99"
566575
],
567576
"language": "python",
568577
"metadata": {},
@@ -572,7 +581,7 @@
572581
"cell_type": "code",
573582
"collapsed": false,
574583
"input": [
575-
"stuff"
584+
"food"
576585
],
577586
"language": "python",
578587
"metadata": {},
@@ -589,7 +598,7 @@
589598
"cell_type": "code",
590599
"collapsed": false,
591600
"input": [
592-
"stuff[2] = 3"
601+
"food[2] = 3"
593602
],
594603
"language": "python",
595604
"metadata": {},
@@ -612,6 +621,26 @@
612621
"metadata": {},
613622
"outputs": []
614623
},
624+
{
625+
"cell_type": "code",
626+
"collapsed": false,
627+
"input": [
628+
"empty"
629+
],
630+
"language": "python",
631+
"metadata": {},
632+
"outputs": []
633+
},
634+
{
635+
"cell_type": "code",
636+
"collapsed": false,
637+
"input": [
638+
"type(empty)"
639+
],
640+
"language": "python",
641+
"metadata": {},
642+
"outputs": []
643+
},
615644
{
616645
"cell_type": "markdown",
617646
"metadata": {},
@@ -623,7 +652,7 @@
623652
"cell_type": "code",
624653
"collapsed": false,
625654
"input": [
626-
"things = (3)"
655+
"food = (3)"
627656
],
628657
"language": "python",
629658
"metadata": {},
@@ -633,7 +662,7 @@
633662
"cell_type": "code",
634663
"collapsed": false,
635664
"input": [
636-
"things"
665+
"food"
637666
],
638667
"language": "python",
639668
"metadata": {},
@@ -643,7 +672,7 @@
643672
"cell_type": "code",
644673
"collapsed": false,
645674
"input": [
646-
"type(things)"
675+
"type(food)"
647676
],
648677
"language": "python",
649678
"metadata": {},
@@ -660,7 +689,7 @@
660689
"cell_type": "code",
661690
"collapsed": false,
662691
"input": [
663-
"things = (3,)"
692+
"food = (3,)"
664693
],
665694
"language": "python",
666695
"metadata": {},
@@ -670,11 +699,96 @@
670699
"cell_type": "code",
671700
"collapsed": false,
672701
"input": [
673-
"things"
702+
"food"
674703
],
675704
"language": "python",
676705
"metadata": {},
677706
"outputs": []
707+
},
708+
{
709+
"cell_type": "code",
710+
"collapsed": false,
711+
"input": [
712+
"type(food)"
713+
],
714+
"language": "python",
715+
"metadata": {},
716+
"outputs": []
717+
},
718+
{
719+
"cell_type": "markdown",
720+
"metadata": {},
721+
"source": [
722+
"Sometimes, unpacking a tuple is also desired."
723+
]
724+
},
725+
{
726+
"cell_type": "code",
727+
"collapsed": false,
728+
"input": [
729+
"yummies = [(\"cones\", 100, 3.79), (\"sprinkles\", 10, 4.99), (\"hot fudge\", 8, 3.29), (\"nuts\", 6, 8.99)]"
730+
],
731+
"language": "python",
732+
"metadata": {},
733+
"outputs": []
734+
},
735+
{
736+
"cell_type": "code",
737+
"collapsed": false,
738+
"input": [
739+
"yummies[2]"
740+
],
741+
"language": "python",
742+
"metadata": {},
743+
"outputs": []
744+
},
745+
{
746+
"cell_type": "code",
747+
"collapsed": false,
748+
"input": [
749+
"name, amount, price = yummies[2]"
750+
],
751+
"language": "python",
752+
"metadata": {},
753+
"outputs": []
754+
},
755+
{
756+
"cell_type": "code",
757+
"collapsed": false,
758+
"input": [
759+
"print(\"Our ice cream shop serves \" + name + \".\")"
760+
],
761+
"language": "python",
762+
"metadata": {},
763+
"outputs": []
764+
},
765+
{
766+
"cell_type": "code",
767+
"collapsed": false,
768+
"input": [
769+
"print(\"The shop's inventory value for \" + name + \" is $\" + str(amount * price) + \".\")"
770+
],
771+
"language": "python",
772+
"metadata": {},
773+
"outputs": []
774+
},
775+
{
776+
"cell_type": "code",
777+
"collapsed": false,
778+
"input": [
779+
"print(\"All this food talk is making me hungry for \" + yummies[3][0] + \".\")"
780+
],
781+
"language": "python",
782+
"metadata": {},
783+
"outputs": []
784+
},
785+
{
786+
"cell_type": "code",
787+
"collapsed": false,
788+
"input": [],
789+
"language": "python",
790+
"metadata": {},
791+
"outputs": []
678792
}
679793
],
680794
"metadata": {}

0 commit comments

Comments
 (0)