{ "metadata": { "name": "", "signature": "sha256:7640407737f0f1e8519c9b988e071dc2978364eabd3531de7ede1919a57b3780" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "List Basics" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Let's create a list that has three items. Lists are a kind of 'collection' such that they collect or contain things (a.ka.a 'members' or 'items'. So, in the list, things, we have a collection of 3 chars." ] }, { "cell_type": "code", "collapsed": false, "input": [ "things = [100, 500, \"thing\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see what the 'your_list' variable contains..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "things" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[100, 500, 'things']" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can confirm that this is a list by using 'type'" ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(things)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "list" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like strings, lists have a length" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(things)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "3" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also like strings, list have content that can be checked. By using the 'in' operator, we can find out if a char such as the letter 'c' is contained in the list, your_list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"e\" in \"Hello\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "True" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "\"thing\" in things" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "True" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "\"nothing\" in things" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "False" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "things = [100, 500, \"things\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "List Indexes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are ordered. We can access any individual item in a list based on its position inside the list. Let's say I wanted to get the leftmost item." ] }, { "cell_type": "code", "collapsed": false, "input": [ "things[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "100" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that we start counting at zero, which is a little funny since one might think it would start with the number, 1." ] }, { "cell_type": "code", "collapsed": false, "input": [ "things[1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "500" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "things[2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "'things'" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens if we try to access item 3?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "things[3]" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mthings\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get a traceback, but a pretty readable one." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fortunately, we can add items to the end of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "things.append(\"d\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "len(things)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "5" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "things[3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "'d'" ] } ], "prompt_number": 26 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Adding stuff to lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we want to append a bunch of stuff? A related question is how do we even know we can append to this list? We don't rely on memorization, we use documentation. Let's type *python lists* into Google.\n", "\n", "We'll see the \"extend\" function which looks like what we need to add a bunch of items to the list at once." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way of using documentation is by the built-in 'help' function. It's not pretty, and it's not as intuitive, but it works. To exit out of help, you have to press \"q\".\n", "\n", "In general, if you want to know whether it's possible to do something in Python, Google it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a question: how would you create an empty list?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stuff = []" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's check the length!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(stuff)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "0" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "I have no stuff. :(\n", "\n", "Let's add some things to my list of stuff" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stuff.append(\"laptop\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "stuff" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "['laptop']" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "stuff.append(\"wedding ring\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "stuff" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "['laptop', 'wedding ring', 'wedding ring']" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Okay now we want to replace laptop with Macbook Pro." ] }, { "cell_type": "code", "collapsed": false, "input": [ "stuff[0] = \"Macobook Pro\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "stuff" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "['Macobook Pro', 'wedding ring', 'wedding ring']" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's replace wedding ring with tungsten wedding ring" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stuff[2] = \"tungsten wedding ring\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a question: how is this different from the line above?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stuff[2] == \"tungsten wedding ring\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "True" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do we get the last item in the list?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know we can get the length of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(stuff)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "3" ] } ], "prompt_number": 39 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the last item's index, let's try using the length of the list, stuff, minus one." ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(stuff) - 1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "2" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "stuff[len(stuff) - 1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "'tungsten wedding ring'" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But there's a shortcut in Python for getting the last item from a list.\n", "\n", "You can use negative numbers to start counting from the end of the list. So -1 gives us the last item:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stuff[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "'tungsten wedding ring'" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's just doing the math for us. There's no real magic here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do the same for strings to access a character or the last character within a string, its length, and so on." ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Danny\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "name[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "'D'" ] } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "name[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "'y'" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": {}, "source": [ "A quick review of lists:\n", "So the way we create a list is we give it a name and in square brackets give it a comma-separated set of items." ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits = [\"apples\", \"bananas\", \"oranges\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 53 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use this square brackets syntax to get individual items" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "'apples'" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use negative numbers to get items from the end of a list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "'oranges'" ] } ], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do asignment. We can replace items in the list by using the notation to get the item and then do an assignment." ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[0] = \"plums\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can add to the end of the list with .append" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits.append(\"cherries\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check the length of a list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(fruits)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": [ "4" ] } ], "prompt_number": 51 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do things like check for things contained in a list using the 'in' operator" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"apples\" in fruits" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "False" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: can we grab more than one item from a list?\n", "\n", "This is the range syntax:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits = [\"apples\", \"bananas\", \"oranges\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 54 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Slicing of lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[0:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "['apples', 'bananas']" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The left side of the colon is implicit. It starts at the beginning by default, but does not include the last item in the list." ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 56, "text": [ "['apples', 'bananas']" ] } ], "prompt_number": 56 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The right side of the colon can also be left off. It starts from and ends at the end of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[2:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "['oranges']" ] } ], "prompt_number": 57 }, { "cell_type": "markdown", "metadata": {}, "source": [ "By extension, if you leave off both the left and right sides you can make a copy of a list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 58, "text": [ "['apples', 'bananas', 'oranges']" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "my_favorite_fruits = fruits[:]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 59 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we see that the original fruits list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "['apples', 'bananas', 'oranges']" ] } ], "prompt_number": 60 }, { "cell_type": "markdown", "metadata": {}, "source": [ "has indeed been copied into my_favorite_fruits list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_favorite_fruits" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "['apples', 'bananas', 'oranges']" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "names = [\"Danny\", \"Audrey\", \"Risa\", \"Alain\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 62 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we wanted to loop over and do some action for every item in the list?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello\")\n", "print(\"My name is Danny\")\n", "print(\"I live in San Diego\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello\")\n", "print(\"My name is Audrey\")\n", "print(\"I live in San Diego\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello\")\n", "print(\"My name is Risa\")\n", "print(\"I live in San Diego\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's the hard way. The easy way is by using a 'for' loop. A simple one:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for name in names:\n", " print(name)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Danny\n", "Audrey\n", "Risa\n", "Alain\n" ] } ], "prompt_number": 63 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To hammer home that this is a variable name of our choosing let's use a different variable name, x." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in names:\n", " print(x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Danny\n", "Audrey\n", "Risa\n", "Alain\n" ] } ], "prompt_number": 64 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can have arbitrarily complex statements and as many lines as we want as long as we keep it indented" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for name in names:\n", " print(\"Hello\", name)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello Danny\n", "Hello Audrey\n", "Hello Risa\n", "Hello Alain\n" ] } ], "prompt_number": 85 }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we only want to print out the names that start with a vowel?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do we check whether a name starts with a vowel?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know how to get the first character" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Audrey\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "name[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 67, "text": [ "'A'" ] } ], "prompt_number": 67 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to check whether it equals A, E, I, O, or U. There are a lot of different ways to write this" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name[0] == \"A\" or name[0] == \"E\" or name[0] == \"I\" or name[0] == \"O\" or name[0] == \"U\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 68, "text": [ "True" ] } ], "prompt_number": 68 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also say this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name[0] in \"AEIOU\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 69, "text": [ "True" ] } ], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could even say:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name[0] in [\"A\", \"E\", \"I\", \"O\", \"U\"]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 70, "text": [ "True" ] } ], "prompt_number": 70 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's setup our for loop" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for name in names:\n", " if name[0] in \"AEIOU\":\n", " print(name + \" starts with a vowel\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Audrey starts with a vowel\n", "Alain starts with a vowel\n" ] } ], "prompt_number": 71 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The amount of work we had to do here is independent of the length of the list. This list could have had a billion elements in it and this code would still work." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of printing the names, how do we build up a list of only the names that start with a vowel?\n", "\n", "We're going to need a for loop. We're also going to need some storage; so let's use a list as storage." ] }, { "cell_type": "code", "collapsed": false, "input": [ "vowel_names = []" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "for name in names:\n", " if name[0] in \"AEIOU\":\n", " vowel_names.append(name)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 73 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nothing is printed. Let's check that vowel_names has the vowel names" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vowel_names" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "['Audrey', 'Alain']" ] } ], "prompt_number": 74 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say that I'm going to the store and I bought goods and I have their costs. What if I want to add up all the items?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "prices = [1.5, 2.35, 5.99, 16.49]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 75 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's sort of like we have a running total. I need to setup some storage ahead of time again." ] }, { "cell_type": "code", "collapsed": false, "input": [ "total = 0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "for cost in prices:\n", " total = total + cost # looks funny but when you think about it, it makes sense" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "total" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 78, "text": [ "26.33" ] } ], "prompt_number": 78 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now I'm cheating on you guys a little bit because there's a function called sum that does the same thing so we'd probably use that in an actual program" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sum(prices)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In general, be comfortable with not knowing things. Looking things up is important when programming." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Looping over lists of lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that earlier example?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello\")\n", "print(\"My name is Danny\")\n", "print(\"I live in San Diego\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n", "My name is Danny\n", "I live in California\n" ] } ], "prompt_number": 80 }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if I didn't live in San Diego and Alain does? How would we I resolve that with a list and a loop? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The answer is to use a list of lists." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mentors = [[\"Danny\", \"Inland Empire\"], [\"Audrey\", \"Corona\"], [\"Alain\", \"San Diego\"]]\n", "print(mentors)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 88, "text": [ "[['Danny', 'Inland Empire'], ['Audrey', 'Corona'], ['Alain', 'San Diego']]" ] } ], "prompt_number": 88 }, { "cell_type": "markdown", "metadata": {}, "source": [ "okay, let's loop through them." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for mentor in mentors:\n", " print(mentor)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Danny', 'Inland Empire']\n", "['Audrey', 'Corona']\n", "['Alain', 'San Diego']\n" ] } ], "prompt_number": 89 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can loop through the big list, then use indexes to grab stuff from the internal list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for mentor in mentors: # Don't forget to end the statement with a :\n", " print(\"Hello\")\n", " print(\"My name is\", mentor[0])\n", " print(\"I live in\", mentor[1])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n", "My name is Danny\n", "I live in Inland Empire\n", "Hello\n", "My name is Audrey\n", "I live in Corona\n", "Hello\n", "My name is Alain\n", "I live in San Diego\n" ] } ], "prompt_number": 91 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Neato Trick" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for name, city in mentors: # Don't forget to end the statement with a :\n", " print(\"Hello\")\n", " print(\"My name is\", name)\n", " print(\"I live in\", city)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n", "My name is Danny\n", "I live in Inland Empire\n", "Hello\n", "My name is Audrey\n", "I live in Corona\n", "Hello\n", "My name is Alain\n", "I live in San Diego\n" ] } ], "prompt_number": 92 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also do this without the use of a loop" ] }, { "cell_type": "code", "collapsed": false, "input": [ "danny, audrey, alain = mentors\n", "print(danny)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 94, "text": [ "['Danny', 'Inland Empire']" ] } ], "prompt_number": 94 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A set is similar to a list, except it eats the duplicates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a list of things where we want to remove duplicates. Why? Well, what if we want to count the number of different, distinct words used in the Gettysburg Address? Google for it (it's on wikipedia and stick it into a string." ] }, { "cell_type": "code", "collapsed": false, "input": [ "gettysburg_address = \"\"\"Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.\"\"\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 98 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll use the split() method of strings to chop that up into words." ] }, { "cell_type": "code", "collapsed": false, "input": [ "words = gettysburg_address.split(\" \")\n", "print(words)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Four', 'score', 'and', 'seven', 'years', 'ago', 'our', 'fathers', 'brought', 'forth', 'on', 'this', 'continent', 'a', 'new', 'nation,', 'conceived', 'in', 'liberty,', 'and', 'dedicated', 'to', 'the', 'proposition', 'that', 'all', 'men', 'are', 'created', 'equal.']\n" ] } ], "prompt_number": 99 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not perfect, but lets count the words it generates using the len() method" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(len(words))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "30\n" ] } ], "prompt_number": 100 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you look at the address however, you'll see that 'and' is used several times. Let's turn our list into a set.\n", "Using.... well.... the set method." ] }, { "cell_type": "code", "collapsed": false, "input": [ "wordset = set(words)\n", "print(wordset)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'fathers', 'proposition', 'forth', 'equal.', 'conceived', 'men', 'nation,', 'ago', 'created', 'in', 'a', 'that', 'dedicated', 'are', 'our', 'brought', 'the', 'seven', 'and', 'score', 'all', 'this', 'Four', 'new', 'continent', 'on', 'liberty,', 'to', 'years'}\n" ] } ], "prompt_number": 102 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whoa! Looks different! Curly braces instead of bracket.s Maybe even shorter! Let's check, shall we?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(len(wordset))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "29\n" ] } ], "prompt_number": 103 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, sets removed duplicates." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Extra Credit / Pop Quiz time!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Using the help() method, add the following phrase to the Gettysburg Address **wordset**: \"Abraham Lincoln" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. sort the list of names in reverse order." ] } ], "metadata": {} } ] }