diff --git a/part-4.ipynb b/part-4.ipynb index c9abaa1..6d9dd75 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559" + "signature": "sha256:7b5c09563e39ddf2ede88b8ff357dcdc654af16292c9c86e3bca2fbf703f64c2" }, "nbformat": 3, "nbformat_minor": 0, @@ -518,6 +518,8 @@ "\n", "Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n", "\n", + "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", + "\n", "Tuples are comma-separated lists:" ] }, @@ -525,17 +527,24 @@ "cell_type": "code", "collapsed": false, "input": [ - "stuff = (1, 2, \"hello\")" + "food = (\"cones\", 100, 3.79)" ], "language": "python", "metadata": {}, "outputs": [] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is sometimes called packing a tuple." + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "stuff" + "food" ], "language": "python", "metadata": {}, @@ -545,7 +554,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "type(stuff)" + "type(food)" ], "language": "python", "metadata": {}, @@ -562,7 +571,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "stuff = 1, 2, 3" + "food = \"sprinkles\", 10, 4.99" ], "language": "python", "metadata": {}, @@ -572,7 +581,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "stuff" + "food" ], "language": "python", "metadata": {}, @@ -589,7 +598,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "stuff[2] = 3" + "food[2] = 3" ], "language": "python", "metadata": {}, @@ -612,6 +621,26 @@ "metadata": {}, "outputs": [] }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "empty" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "type(empty)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -623,7 +652,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "things = (3)" + "food = (3)" ], "language": "python", "metadata": {}, @@ -633,7 +662,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "things" + "food" ], "language": "python", "metadata": {}, @@ -643,7 +672,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "type(things)" + "type(food)" ], "language": "python", "metadata": {}, @@ -660,7 +689,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "things = (3,)" + "food = (3,)" ], "language": "python", "metadata": {}, @@ -670,11 +699,107 @@ "cell_type": "code", "collapsed": false, "input": [ - "things" + "food" ], "language": "python", "metadata": {}, "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "type(food)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sometimes, unpacking a tuple is also desired." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "yummies = [(\"cones\", 100, 3.79), (\"sprinkles\", 10, 4.99), (\"hot fudge\", 8, 3.29), (\"nuts\", 6, 8.99)]" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "yummies[2]" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "name, amount, price = yummies[2]" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print(\"Our ice cream shop serves \" + name + \".\")" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print(\"The shop's inventory value for \" + name + \" is $\" + str(amount * price) + \".\")" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print(\"All this food talk is making me hungry for \" + yummies[3][0] + \".\")" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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", + "\n", + "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", + "\n", + "Wow! We've come a long way today. " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {}