{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a list that has three items. Lists are a kind of 'collection' such that they collect or contain members or items. So, in the list, your_list, we have a collection of 3 chars." ] }, { "cell_type": "code", "collapsed": false, "input": [ "your_list = [\"a\", \"b\", \"c\"]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can confirm that this is a list by using 'type'" ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(your_list)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like strings lists have a length" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(your_list)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" in your_list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "your_list = [\"a\", \"b\", \"c\"]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [ "your_list[0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [ "your_list[1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "your_list[2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember, you can use the up arrow if you want to go back and edit <<< is this for Carol?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens if we try to access item 3?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "your_list[3]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [ "your_list.append(\"d\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(your_list)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "your_list[3]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [ "her_list = []" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(her_list)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also replace items in a list.\n", "Let's say we're starting a band." ] }, { "cell_type": "code", "collapsed": false, "input": [ "names = [\"Alice\", \"Amy\"]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "names.append(\"Adam\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "names" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Okay now we want to replace Alice with Jimmy." ] }, { "cell_type": "code", "collapsed": false, "input": [ "names[0] = \"Jimmy\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "names" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's replace Adam with Rachel" ] }, { "cell_type": "code", "collapsed": false, "input": [ "names[2] = \"Rachel\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a question: how is this different from the line above?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "names[2] == \"Rachel\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "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(names)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the last item's index, let's try using the length of the list, names, minus one." ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(names) - 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "names[len(names) - 1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [ "names[-1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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 = \"Carol\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "name[0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "name[-1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits[0:2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_favorite_fruits = fruits[:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we see that the original fruits list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "names = [\"Alice\", \"Bob\", \"Cassie\", \"Diane\", \"Ellen\"]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we wanted to loop over and do some action for every item in the list?\n", "\n", "The way we do this is by using a 'for' loop:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for name in names:\n", " print name " ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "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 word in names:\n", " print \"Hello \" + word" ], "language": "python", "metadata": {}, "outputs": [] }, { "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 = \"Alice\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "name[0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also say this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name[0] in \"AEIOU\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "total" ], "language": "python", "metadata": {}, "outputs": [] }, { "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." ] } ], "metadata": {} } ] }