diff --git a/part-4.ipynb b/part-4.ipynb index c28681f..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, @@ -246,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": {}, @@ -340,11 +375,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", " 'el': \"the\",\n", @@ -360,34 +391,13 @@ ], "language": "python", "metadata": {}, - "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, - "input": [ - "translate(\"el gato comio la casa\")" + "translate(\"el gato esta en la casa\")" ], "language": "python", "metadata": {}, @@ -397,137 +407,80 @@ "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": [], - "prompt_number": 17 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Mexican Spanish to English translation:" - ] + "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el perro bebi\u00f3 el jugo y comio las papas\")" + "translate(\"el jugo esta en la casa\")" ], "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", "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 perro bebi\u00f3 el zumo y comio las patatas\")" + "del words['jugo']\n", + "words['zumo'] = \"juice\"" ], "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", "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": [], - "prompt_number": 18 + "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": [], - "prompt_number": 22 + "source": [ + "Now let's try our Spanish Spanish sentence:" + ] }, { "cell_type": "code", "collapsed": false, "input": [ - "translate(\"el perro bebi\u00f3 el zumo y comio las patatas-fritas y las galletas\")" + "translate(\"el zumo esta en la casa\")" ], "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", @@ -561,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": {}, @@ -583,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": {}, @@ -601,8 +572,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "empty_set = set()\n", - "type(empty_set)" + "stuff" ], "language": "python", "metadata": {}, @@ -612,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": {}, @@ -629,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": {}, @@ -656,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": {}, @@ -684,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": {}, @@ -704,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": {},