From da80009ee767986dd60049d9461dcf336a9dea48 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sat, 23 Aug 2014 19:56:48 -0700 Subject: [PATCH 1/7] Start list comprehensions in part 4 --- part-4.ipynb | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 part-4.ipynb diff --git a/part-4.ipynb b/part-4.ipynb new file mode 100644 index 0000000..88369e5 --- /dev/null +++ b/part-4.ipynb @@ -0,0 +1,207 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:128c8fe150d3130a1c9098424e0bd5dc7b7f585570ac4626dda0d8b2b08e80c4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's learn more cool Python stuff." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**List Comprehensions**\n", + "\n", + "Now we're going to learn about list comprehensions.\n", + "\n", + "A list comprehension is kind of like an inside-out for loop. It makes it easy to do operations on elements in a list and return a new list." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Say we have a list of numbers:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 10 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's make a new list that contains a square of every number in our list." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "squared_numbers = []\n", + "for n in my_favorite_numbers:\n", + " squared_numbers.append(n * n)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "squared_numbers" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 20, + "text": [ + "[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025, 7921]" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Stop for questions?:** Does this look familiar? Any questions about this?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's do the same thing with a list comprehension:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "squared_numbers = [n * n for n in my_favorite_numbers]" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "squared_numbers" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 22, + "text": [ + "[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025, 7921]" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's revisit a problem we've already solved:\n", + "\n", + "Pick every name from a list that begins with a vowel.\n", + "\n", + "We started with a list of names first:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "names = [\"Alice\", \"Bob\", \"Cassie\", \"Diane\", \"Ellen\"]" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then we filtered names like this:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "vowel_names = []\n", + "for name in names:\n", + " if name[0] in \"AEIOU\":\n", + " vowel_names.append(name)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here's another way to grab all names starting with a vowel, using list comprehensions:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "vowel_names = [name for name in names if name[0] in \"AEIOU\"]" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "vowel_names" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file From a493da0a51cdbe477ed7e4416e2d90f327ba52cd Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sat, 6 Sep 2014 16:27:44 -0700 Subject: [PATCH 2/7] Update prompt counts --- part-4.ipynb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index 88369e5..e21f7f8 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -19,7 +19,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**List Comprehensions**\n", + "## List Comprehensions\n", "\n", "Now we're going to learn about list comprehensions.\n", "\n", @@ -42,7 +42,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 10 + "prompt_number": 1 }, { "cell_type": "markdown", @@ -62,7 +62,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 19 + "prompt_number": 2 }, { "cell_type": "code", @@ -76,13 +76,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 20, + "prompt_number": 3, "text": [ "[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025, 7921]" ] } ], - "prompt_number": 20 + "prompt_number": 3 }, { "cell_type": "markdown", @@ -107,7 +107,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 21 + "prompt_number": 4 }, { "cell_type": "code", @@ -121,13 +121,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 22, + "prompt_number": 5, "text": [ "[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025, 7921]" ] } ], - "prompt_number": 22 + "prompt_number": 5 }, { "cell_type": "markdown", @@ -149,7 +149,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 2 + "prompt_number": 6 }, { "cell_type": "markdown", @@ -170,7 +170,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 3 + "prompt_number": 7 }, { "cell_type": "markdown", @@ -188,7 +188,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 5 + "prompt_number": 8 }, { "cell_type": "code", From 5fe8d592ca7b5fd2fa3adc983b0c9ad161b5bbdf Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sat, 6 Sep 2014 16:27:56 -0700 Subject: [PATCH 3/7] Add dictionary example --- part-4.ipynb | 279 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 277 insertions(+), 2 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index e21f7f8..39c7fc2 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:128c8fe150d3130a1c9098424e0bd5dc7b7f585570ac4626dda0d8b2b08e80c4" + "signature": "sha256:f37b105c5e429feb6bdac24d25a26ff79e35b74f141c3e312cbdfba496268ab9" }, "nbformat": 3, "nbformat_minor": 0, @@ -198,7 +198,282 @@ ], "language": "python", "metadata": {}, - "outputs": [] + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 9, + "text": [ + "['Alice', 'Ellen']" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dictionaries\n", + "\n", + "Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are used for lookup and their values are returned.\n", + "\n", + "Let's make an Spanish to English translator. We'll ignore grammar and just translate word-by-word for now." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "words = {\n", + " 'gato': \"cat\",\n", + " 'casa': \"house\",\n", + " 'esta': \"is\",\n", + " 'en': \"in\",\n", + " 'el': \"the\",\n", + " 'la': \"the\",\n", + "}" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "words['gato']" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 11, + "text": [ + "'cat'" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strings have a split method which we can use to split strings with spaces into a list of words." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sentence = \"el gato esta en la casa\"\n", + "sentence_words = sentence.split()\n", + "sentence_words" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 12, + "text": [ + "['el', 'gato', 'esta', 'en', 'la', 'casa']" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's loop over each word in the list and translate each one." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for spanish_word in sentence_words:\n", + " print(words[spanish_word])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the\n", + "cat\n", + "is\n", + "in\n", + "the\n", + "house\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's make a list from the translated words." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "translated_words = []\n", + "for spanish_word in sentence_words:\n", + " translated_words.append(words[spanish_word])\n", + "\n", + "translated_words" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 14, + "text": [ + "['the', 'cat', 'is', 'in', 'the', 'house']" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We've almost made a translated sentence! Let's join our new list of words back together with spaces in between each word." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "translated_sentence = \" \".join(translated_words)\n", + "translated_sentence" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 15, + "text": [ + "'the cat is in the house'" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's put this all together into a function:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "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", + " 'la': \"the\",\n", + "}\n", + "\n", + "def translate(sentence):\n", + " spanish_words = sentence.split()\n", + " english_words = []\n", + " for w in spanish_words:\n", + " english_words.append(words[w])\n", + " return \" \".join(english_words)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "translate(\"el perro comio el burrito verde\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 17, + "text": [ + "'the dog ate the burrito green'" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "translate(\"el burrito comio el perro verde\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 18, + "text": [ + "'the burrito ate the dog green'" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "translate(\"el gato comio la casa\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 19, + "text": [ + "'the cat ate the house'" + ] + } + ], + "prompt_number": 19 } ], "metadata": {} From d1ed3f9bc71552e0e21f4f297e61866cf6a31988 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sat, 6 Sep 2014 16:31:54 -0700 Subject: [PATCH 4/7] Add extra credit for dictionary example --- part-4.ipynb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/part-4.ipynb b/part-4.ipynb index 39c7fc2..3355f65 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:f37b105c5e429feb6bdac24d25a26ff79e35b74f141c3e312cbdfba496268ab9" + "signature": "sha256:80c4f576977ccfcd97484e590ed21d446a3e584ff0483c00ad63d78a1770783a" }, "nbformat": 3, "nbformat_minor": 0, @@ -474,6 +474,13 @@ } ], "prompt_number": 19 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Extra Credit:** turn the translate function into a single list comprehension." + ] } ], "metadata": {} From 626bce2b8bc44aaa4cd9a202e4f4126cf27caf16 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 11 Sep 2014 17:26:55 -0700 Subject: [PATCH 5/7] Remove output cells from dictionary explanation --- part-4.ipynb | 167 ++++++++------------------------------------------- 1 file changed, 26 insertions(+), 141 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index 3355f65..953ce1c 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -41,8 +41,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 1 + "outputs": [] }, { "cell_type": "markdown", @@ -61,8 +60,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 2 + "outputs": [] }, { "cell_type": "code", @@ -72,17 +70,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 3, - "text": [ - "[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025, 7921]" - ] - } - ], - "prompt_number": 3 + "outputs": [] }, { "cell_type": "markdown", @@ -106,8 +94,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 4 + "outputs": [] }, { "cell_type": "code", @@ -117,17 +104,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 5, - "text": [ - "[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025, 7921]" - ] - } - ], - "prompt_number": 5 + "outputs": [] }, { "cell_type": "markdown", @@ -148,8 +125,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 6 + "outputs": [] }, { "cell_type": "markdown", @@ -169,8 +145,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 7 + "outputs": [] }, { "cell_type": "markdown", @@ -187,8 +162,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 8 + "outputs": [] }, { "cell_type": "code", @@ -198,17 +172,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 9, - "text": [ - "['Alice', 'Ellen']" - ] - } - ], - "prompt_number": 9 + "outputs": [] }, { "cell_type": "markdown", @@ -236,8 +200,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 10 + "outputs": [] }, { "cell_type": "code", @@ -247,17 +210,14 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 11, - "text": [ - "'cat'" - ] - } - ], - "prompt_number": 11 + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**TODO**: Note similarities with other iterable types (lists, strings)." + ] }, { "cell_type": "markdown", @@ -276,17 +236,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 12, - "text": [ - "['el', 'gato', 'esta', 'en', 'la', 'casa']" - ] - } - ], - "prompt_number": 12 + "outputs": [] }, { "cell_type": "markdown", @@ -304,21 +254,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "the\n", - "cat\n", - "is\n", - "in\n", - "the\n", - "house\n" - ] - } - ], - "prompt_number": 13 + "outputs": [] }, { "cell_type": "markdown", @@ -339,17 +275,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 14, - "text": [ - "['the', 'cat', 'is', 'in', 'the', 'house']" - ] - } - ], - "prompt_number": 14 + "outputs": [] }, { "cell_type": "markdown", @@ -367,17 +293,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 15, - "text": [ - "'the cat is in the house'" - ] - } - ], - "prompt_number": 15 + "outputs": [] }, { "cell_type": "markdown", @@ -412,8 +328,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 16 + "outputs": [] }, { "cell_type": "code", @@ -423,17 +338,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 17, - "text": [ - "'the dog ate the burrito green'" - ] - } - ], - "prompt_number": 17 + "outputs": [] }, { "cell_type": "code", @@ -443,17 +348,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 18, - "text": [ - "'the burrito ate the dog green'" - ] - } - ], - "prompt_number": 18 + "outputs": [] }, { "cell_type": "code", @@ -463,17 +358,7 @@ ], "language": "python", "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 19, - "text": [ - "'the cat ate the house'" - ] - } - ], - "prompt_number": 19 + "outputs": [] }, { "cell_type": "markdown", From d3c0e0d56528557b873b6996fe700679acc9ab60 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 11 Sep 2014 17:27:14 -0700 Subject: [PATCH 6/7] Add sets explanation --- part-4.ipynb | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-) diff --git a/part-4.ipynb b/part-4.ipynb index 953ce1c..e0f581a 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:80c4f576977ccfcd97484e590ed21d446a3e584ff0483c00ad63d78a1770783a" + "signature": "sha256:4ff913df76c71e0fc04dec368875c03bcc43a8ff8d40de3910c277a671c7b26a" }, "nbformat": 3, "nbformat_minor": 0, @@ -366,6 +366,162 @@ "source": [ "**Extra Credit:** turn the translate function into a single list comprehension." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sets\n", + "\n", + "Sets are unordered collections of distinct items. Lists can have duplicates of the same item, but sets only store one copy.\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 show up in our roster twice. Let's use a set!" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "party_roster = {\"Jessica\", \"Tom\", \"Alice\"}\n", + "party_roster" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "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?" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "empty_dictionary = {}\n", + "type(empty_dictionary)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "empty_set = set()\n", + "type(empty_set)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our friend Jeremy just RSVPed, let's add him to our set too:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "party_roster.add(\"Jeremy\")" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "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." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"Jessica\" in party_roster" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"Bill\" in party_roster" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "len(party_roster)" + ], + "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\"}" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "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:" + ] + }, + { + "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)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "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)" + ], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} From 8cb44737e0a19cf129dad40ae677870ff6d09461 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sat, 13 Sep 2014 16:58:23 -0700 Subject: [PATCH 7/7] Add membership and size checking for dicts --- part-4.ipynb | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/part-4.ipynb b/part-4.ipynb index e0f581a..b04c46e 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:4ff913df76c71e0fc04dec368875c03bcc43a8ff8d40de3910c277a671c7b26a" + "signature": "sha256:20f6b02042c480fae323fa6c98332c749bfe323d54bb24066930544c93c73a33" }, "nbformat": 3, "nbformat_minor": 0, @@ -216,9 +216,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**TODO**: Note similarities with other iterable types (lists, strings)." + "Similar to lists and strings, you can check whether a dictionary contains a given key." ] }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'gato' in words" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also check how many key-value pairs are in our dictionary:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "len(words)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, { "cell_type": "markdown", "metadata": {},