From 4d42af236e74af664499722b813a6f2ef7a09208 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 18 Sep 2014 16:50:32 -0700 Subject: [PATCH 1/4] Reduce number of Spanish words in dict --- part-4.ipynb | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index c28681f..b610110 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -209,6 +209,7 @@ "words = {\n", " 'gato': \"cat\",\n", " 'casa': \"house\",\n", + " 'comio': \"ate\",\n", " 'esta': \"is\",\n", " 'en': \"in\",\n", " 'el': \"the\",\n", @@ -340,10 +341,7 @@ "input": [ "words = {\n", " 'gato': \"cat\",\n", - " 'perro': \"dog\",\n", " 'casa': \"house\",\n", - " 'burrito': \"burrito\",\n", - " 'verde': \"green\",\n", " 'comio': \"ate\",\n", " 'esta': \"is\",\n", " 'en': \"in\",\n", @@ -363,26 +361,6 @@ "outputs": [], "prompt_number": 2 }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "translate(\"el perro comio el burrito verde\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "translate(\"el burrito comio el perro verde\")" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, { "cell_type": "code", "collapsed": false, @@ -432,7 +410,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el perro bebi\u00f3 el jugo y comio las papas\")" + "translate(\"el gato bebi\u00f3 el jugo y comio las papas\")" ], "language": "python", "metadata": {}, @@ -459,7 +437,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el perro bebi\u00f3 el zumo y comio las patatas\")" + "translate(\"el gato bebi\u00f3 el zumo y comio las patatas\")" ], "language": "python", "metadata": {}, @@ -513,7 +491,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el perro bebi\u00f3 el zumo y comio las patatas-fritas y las galletas\")" + "translate(\"el gato bebi\u00f3 el zumo y comio las patatas-fritas y las galletas\")" ], "language": "python", "metadata": {}, From 00bdc78d04e5f994e5a43f682326a197e87c0240 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 18 Sep 2014 16:51:07 -0700 Subject: [PATCH 2/4] Clear all output --- part-4.ipynb | 48 +++++++----------------------------------------- 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index b610110..654588c 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -358,8 +358,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 2 + "outputs": [] }, { "cell_type": "code", @@ -396,8 +395,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 17 + "outputs": [] }, { "cell_type": "markdown", @@ -414,17 +412,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 8, - "text": [ - "'the dog drank the juice and ate the potatoes'" - ] - } - ], - "prompt_number": 8 + "outputs": [] }, { "cell_type": "markdown", @@ -441,17 +429,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 11, - "text": [ - "'the dog drank the juice and ate the potatoes'" - ] - } - ], - "prompt_number": 11 + "outputs": [] }, { "cell_type": "markdown", @@ -471,8 +449,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 18 + "outputs": [] }, { "cell_type": "code", @@ -484,8 +461,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 22 + "outputs": [] }, { "cell_type": "code", @@ -495,17 +471,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 25, - "text": [ - "'the dog drank the juice and ate the chips and the bisquits'" - ] - } - ], - "prompt_number": 25 + "outputs": [] }, { "cell_type": "markdown", From 99d17984b602085337fc847e8fddbd51960207a7 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 18 Sep 2014 17:44:29 -0700 Subject: [PATCH 3/4] Shorten amount of typing for dict example --- part-4.ipynb | 89 +++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index 654588c..5ac5f67 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -209,7 +209,6 @@ "words = {\n", " 'gato': \"cat\",\n", " 'casa': \"house\",\n", - " 'comio': \"ate\",\n", " 'esta': \"is\",\n", " 'en': \"in\",\n", " 'el': \"the\",\n", @@ -247,6 +246,41 @@ "metadata": {}, "outputs": [] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also reassign keys in our dictionary:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "words['gato'] = \"dog\"\n", + "words['gato']" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's change that back so we don't confuse anyone:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "words['gato'] = \"cat\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -342,7 +376,6 @@ "words = {\n", " 'gato': \"cat\",\n", " 'casa': \"house\",\n", - " 'comio': \"ate\",\n", " 'esta': \"is\",\n", " 'en': \"in\",\n", " 'el': \"the\",\n", @@ -364,7 +397,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el gato comio la casa\")" + "translate(\"el gato esta en la casa\")" ], "language": "python", "metadata": {}, @@ -374,41 +407,24 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now let's make the words dictionary a Spain Spanish and Mexican Spanish to English dictionary. We'll start by adding a few keys and values." + "Let's add another word to our dictionary:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "words['patatas'] = 'potatoes'\n", - "words['papas'] = 'potatoes'\n", - "words['zumo'] = 'juice'\n", - "words['jugo'] = 'juice'\n", - "words['y'] = 'and'\n", - "words['comio'] = 'ate'\n", - "words['bebi\u00f3'] = 'drank'\n", - "words['las'] = 'the'\n", - "words['galletas'] = 'cookies'\n", - "words['papas-fritas'] = 'french fries'\n", - "words['patatas-fritas'] = 'french fries'" + "words['jugo'] = \"juice\"" ], "language": "python", "metadata": {}, "outputs": [] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Mexican Spanish to English translation:" - ] - }, { "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el gato bebi\u00f3 el jugo y comio las papas\")" + "translate(\"el jugo esta en la casa\")" ], "language": "python", "metadata": {}, @@ -418,14 +434,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Spain Spanish to English translation:" + "That's how we say juice in Mexican Spanish but what if we're traveling to Spain? Let's remove jugo from our dictionary and add zumo:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el gato bebi\u00f3 el zumo y comio las patatas\")" + "del words['jugo']\n", + "words['zumo'] = \"juice\"" ], "language": "python", "metadata": {}, @@ -435,39 +452,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To illustrate a few more dictionary concepts, let's convert our dictionary to Span Spanish to British English." + "Let's try our Mexican Spanish sentence again. What should happen?" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "#remove the Mexican Spanish key, values:\n", - "del words['papas']\n", - "del words['jugo']\n", - "del words['papas-fritas']" + "translate(\"el jugo esta en la casa\")" ], "language": "python", "metadata": {}, "outputs": [] }, { - "cell_type": "code", - "collapsed": false, - "input": [ - "#change the values from English to British English:\n", - "words['galletas'] = 'bisquits'\n", - "words['patatas-fritas'] = 'chips'" - ], - "language": "python", + "cell_type": "markdown", "metadata": {}, - "outputs": [] + "source": [ + "Now let's try our Spanish Spanish sentence:" + ] }, { "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el gato bebi\u00f3 el zumo y comio las patatas-fritas y las galletas\")" + "translate(\"el zumo esta en la casa\")" ], "language": "python", "metadata": {}, From 009da72aa1ba71476199acb6d8ec75c6d54fb299 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 18 Sep 2014 18:17:39 -0700 Subject: [PATCH 4/4] Replace sets with tuples --- part-4.ipynb | 80 +++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index 5ac5f67..c9abaa1 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:c41c7b2462d1eccc5d63748331ca0f9ee5635e45f1f51c325d9a923cc48cc7ad" + "signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559" }, "nbformat": 3, "nbformat_minor": 0, @@ -514,19 +514,38 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Sets\n", + "## Tuples\n", "\n", - "Sets are unordered collections of distinct items. Lists can have duplicates of the same item, but sets only store one copy.\n", + "Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n", "\n", - "Let's say we want to make a roster of people who have RSVPed to our birthday party. If someone RSVPs twice we don't want them to show up in our roster twice. Let's use a set!" + "Tuples are comma-separated lists:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "party_roster = {\"Jessica\", \"Tom\", \"Alice\"}\n", - "party_roster" + "stuff = (1, 2, \"hello\")" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "stuff" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "type(stuff)" ], "language": "python", "metadata": {}, @@ -536,15 +555,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Notice how we used curly braces around our set (just like we did for dictionaries)? What happens if we want to represent an empty set?" + "The parenthesis are often optional. They're often added for clarity. This should work too:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "empty_dictionary = {}\n", - "type(empty_dictionary)" + "stuff = 1, 2, 3" ], "language": "python", "metadata": {}, @@ -554,8 +572,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "empty_set = set()\n", - "type(empty_set)" + "stuff" ], "language": "python", "metadata": {}, @@ -565,14 +582,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Our friend Jeremy just RSVPed, let's add him to our set too:" + "Let's see what happens when we try to change our tuple:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "party_roster.add(\"Jeremy\")" + "stuff[2] = 3" ], "language": "python", "metadata": {}, @@ -582,24 +599,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can check for size and membership of sets and loop over them just like with sets, strings, and dictionaries." + "We can make an empty tuple with an empty set of parenthesis:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "\"Jessica\" in party_roster" + "empty = ()" ], "language": "python", "metadata": {}, "outputs": [] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Any guesses how we can make a single-item tuple?" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "\"Bill\" in party_roster" + "things = (3)" ], "language": "python", "metadata": {}, @@ -609,25 +633,17 @@ "cell_type": "code", "collapsed": false, "input": [ - "len(party_roster)" + "things" ], "language": "python", "metadata": {}, "outputs": [] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our party is a potluck. Everyone is supposed to bring a dinner dish or a dessert. Let's make sets for everyone bringing a dish and everyone bringing a dessert." - ] - }, { "cell_type": "code", "collapsed": false, "input": [ - "bringing_dish = {\"Jessica\", \"Jeremy\"}\n", - "bringing_dessert = {\"Alice\", \"Jeremy\"}" + "type(things)" ], "language": "python", "metadata": {}, @@ -637,17 +653,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sets allow us to use set operations from math to find the union, intersection, difference, and symmetric difference between sets. Let's try it out:" + "Python just sees this as a number with parenthesis around it. The commas are the important part. We need to add a comma after the number to make a single-item tuple:" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "bringing_dish_or_dessert = bringing_dish.union(bringing_dessert)\n", - "bringing_dish_and_dessert = bringing_dish.intersection(bringing_dessert)\n", - "bringing_just_one_item = bringing_dish.symmetric_difference(bringing_dessert)\n", - "not_bringing_food = party_roster.difference(bringing_dish_or_dessert)" + "things = (3,)" ], "language": "python", "metadata": {}, @@ -657,10 +670,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "print(bringing_dish_or_dessert)\n", - "print(bringing_dish_and_dessert)\n", - "print(bringing_just_one_item)\n", - "print(not_bringing_food)" + "things" ], "language": "python", "metadata": {},