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.
your_list = ["a", "b", "c"]
We can confirm that this is a list by using 'type'
type(your_list)
Like strings lists have a length
len(your_list)
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
"e" in "Hello"
"a" in your_list
your_list = ["a", "b", "c"]
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.
your_list[0]
Notice that we start counting at zero, which is a little funny since one might think it would start with the number, 1.
your_list[1]
your_list[2]
Remember, you can use the up arrow if you want to go back and edit <<< is this for Carol?
What happens if we try to access item 3?
your_list[3]
We get a traceback, but a pretty readable one.
Fortunately, we can add items to the end of the list
your_list.append("d")
len(your_list)
your_list[3]
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.
We'll see the "extend" function which looks like what we need to add a bunch of items to the list at once.
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".
In general, if you want to know whether it's possible to do something in Python, Google it.
Here's a question: how would you create an empty list?
her_list = []
len(her_list)
We can also replace items in a list. Let's say we're starting a band.
names = ["Alice", "Amy"]
names.append("Adam")
names
Okay now we want to replace Alice with Jimmy.
names[0] = "Jimmy"
names
Now let's replace Adam with Rachel
names[2] = "Rachel"
Here's a question: how is this different from the line above?
names[2] == "Rachel"
How do we get the last item in the list?
We know we can get the length of the list
len(names)
To get the last item's index, let's try using the length of the list, names, minus one.
len(names) - 1
names[len(names) - 1]
But there's a shortcut in Python for getting the last item from a list.
You can use negative numbers to start counting from the end of the list. So -1 gives us the last item:
names[-1]
It's just doing the math for us. There's no real magic here.
We can do the same for strings to access a character or the last character within a string, its length, and so on.
name = "Carol"
name[0]
name[-1]
A quick review of lists: 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.
fruits = ["apples", "bananas", "oranges"]
We can use this square brackets syntax to get individual items
fruits[0]
We can use negative numbers to get items from the end of a list
fruits[-1]
We can do asignment. We can replace items in the list by using the notation to get the item and then do an assignment.
fruits[0] = "plums"
We can add to the end of the list with .append
fruits.append("cherries")
We can check the length of a list
len(fruits)
We can do things like check for things contained in a list using the 'in' operator
"apples" in fruits
Question: can we grab more than one item from a list?
This is the range syntax:
fruits = ["apples", "bananas", "oranges"]
fruits[0:2]
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.
fruits[:2]
The right side of the colon can also be left off. It starts from and ends at the end of the list
fruits[2:]
By extension, if you leave off both the left and right sides you can make a copy of a list
fruits[:]
my_favorite_fruits = fruits[:]
Now we see that the original fruits list
fruits
has indeed been copied into my_favorite_fruits list
my_favorite_fruits
names = ["Alice", "Bob", "Cassie", "Diane", "Ellen"]
What if we wanted to loop over and do some action for every item in the list?
The way we do this is by using a 'for' loop:
for name in names:
print name
To hammer home that this is a variable name of our choosing let's use a different variable name, x.
for x in names:
print x
We can have arbitrarily complex statements and as many lines as we want as long as we keep it indented
for word in names:
print "Hello " + word
What if we only want to print out the names that start with a vowel?
How do we check whether a name starts with a vowel?
We know how to get the first character
name = "Alice"
name[0]
We want to check whether it equals A, E, I, O, or U. There are a lot of different ways to write this
name[0] == "A" or name[0] == "E" or name[0] == "I" or name[0] == "O" or name[0] == "U"
We could also say this:
name[0] in "AEIOU"
We could even say:
name[0] in ["A", "E", "I", "O", "U"]
Now let's setup our for loop
for name in names:
if name[0] in "AEIOU":
print name + " starts with a vowel"
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.
Instead of printing the names, how do we build up a list of only the names that start with a vowel?
We're going to need a for loop. We're also going to need some storage; so let's use a list as storage.
vowel_names = []
for name in names:
if name[0] in "AEIOU":
vowel_names.append(name)
Nothing is printed. Let's check that vowel_names has the vowel names
vowel_names
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?
prices = [1.5, 2.35, 5.99, 16.49]
It's sort of like we have a running total. I need to setup some storage ahead of time again.
total = 0
for cost in prices:
total = total + cost # looks funny but when you think about it, it makes sense
total
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
sum(prices)
In general, be comfortable with not knowing things. Looking things up is important when programming.