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.
In [8]:
things = [100, 500, "thing"]
To see what the 'your_list' variable contains...
In [2]:
things
Out[2]:
We can confirm that this is a list by using 'type'
In [3]:
type(things)
Out[3]:
Like strings, lists have a length
In [4]:
len(things)
Out[4]:
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
In [5]:
"e" in "Hello"
Out[5]:
In [10]:
"thing" in things
Out[10]:
In [9]:
"nothing" in things
Out[9]:
In [12]:
things = [100, 500, "things"]
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.
In [13]:
things[0]
Out[13]:
Notice that we start counting at zero, which is a little funny since one might think it would start with the number, 1.
In [22]:
things[1]
Out[22]:
In [23]:
things[2]
Out[23]:
What happens if we try to access item 3?
In [16]:
things[3]
We get a traceback, but a pretty readable one.
Fortunately, we can add items to the end of the list
In [24]:
things.append("d")
In [25]:
len(things)
Out[25]:
In [26]:
things[3]
Out[26]:
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?
In [27]:
stuff = []
Now let's check the length!
In [28]:
len(stuff)
Out[28]:
I have no stuff. :(
Let's add some things to my list of stuff
In [29]:
stuff.append("laptop")
In [30]:
stuff
Out[30]:
In [32]:
stuff.append("wedding ring")
In [33]:
stuff
Out[33]:
Okay now we want to replace laptop with Macbook Pro.
In [35]:
stuff[0] = "Macobook Pro"
In [36]:
stuff
Out[36]:
Now let's replace wedding ring with tungsten wedding ring
In [37]:
stuff[2] = "tungsten wedding ring"
Here's a question: how is this different from the line above?
In [38]:
stuff[2] == "tungsten wedding ring"
Out[38]:
How do we get the last item in the list?
We know we can get the length of the list
In [39]:
len(stuff)
Out[39]:
To get the last item's index, let's try using the length of the list, stuff, minus one.
In [40]:
len(stuff) - 1
Out[40]:
In [41]:
stuff[len(stuff) - 1]
Out[41]:
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:
In [42]:
stuff[-1]
Out[42]:
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.
In [43]:
name = "Danny"
In [44]:
name[0]
Out[44]:
In [45]:
name[-1]
Out[45]:
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.
In [53]:
fruits = ["apples", "bananas", "oranges"]
We can use this square brackets syntax to get individual items
In [47]:
fruits[0]
Out[47]:
We can use negative numbers to get items from the end of a list
In [48]:
fruits[-1]
Out[48]:
We can do asignment. We can replace items in the list by using the notation to get the item and then do an assignment.
In [49]:
fruits[0] = "plums"
We can add to the end of the list with .append
In [50]:
fruits.append("cherries")
We can check the length of a list
In [51]:
len(fruits)
Out[51]:
We can do things like check for things contained in a list using the 'in' operator
In [52]:
"apples" in fruits
Out[52]:
Question: can we grab more than one item from a list?
This is the range syntax:
In [54]:
fruits = ["apples", "bananas", "oranges"]
In [55]:
fruits[0:2]
Out[55]:
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.
In [56]:
fruits[:2]
Out[56]:
The right side of the colon can also be left off. It starts from and ends at the end of the list
In [57]:
fruits[2:]
Out[57]:
By extension, if you leave off both the left and right sides you can make a copy of a list
In [58]:
fruits[:]
Out[58]:
In [59]:
my_favorite_fruits = fruits[:]
Now we see that the original fruits list
In [60]:
fruits
Out[60]:
has indeed been copied into my_favorite_fruits list
In [61]:
my_favorite_fruits
Out[61]:
In [62]:
names = ["Danny", "Audrey", "Risa", "Alain"]
What if we wanted to loop over and do some action for every item in the list?
In [ ]:
print("Hello")
print("My name is Danny")
print("I live in San Diego")
In [ ]:
print("Hello")
print("My name is Audrey")
print("I live in San Diego")
In [ ]:
print("Hello")
print("My name is Risa")
print("I live in San Diego")
That's the hard way. The easy way is by using a 'for' loop. A simple one:
In [63]:
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.
In [64]:
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
In [85]:
for name in names:
print("Hello", name)
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
In [66]:
name = "Audrey"
In [67]:
name[0]
Out[67]:
We want to check whether it equals A, E, I, O, or U. There are a lot of different ways to write this
In [68]:
name[0] == "A" or name[0] == "E" or name[0] == "I" or name[0] == "O" or name[0] == "U"
Out[68]:
We could also say this:
In [69]:
name[0] in "AEIOU"
Out[69]:
We could even say:
In [70]:
name[0] in ["A", "E", "I", "O", "U"]
Out[70]:
Now let's setup our for loop
In [71]:
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.
In [72]:
vowel_names = []
In [73]:
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
In [74]:
vowel_names
Out[74]:
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?
In [75]:
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.
In [76]:
total = 0
In [77]:
for cost in prices:
total = total + cost # looks funny but when you think about it, it makes sense
In [78]:
total
Out[78]:
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
In [ ]:
sum(prices)
In general, be comfortable with not knowing things. Looking things up is important when programming.
Remember that earlier example?
In [80]:
print("Hello")
print("My name is Danny")
print("I live in San Diego")
What if I didn't live in San Diego and Alain does? How would we I resolve that with a list and a loop?
The answer is to use a list of lists.
In [88]:
mentors = [["Danny", "Inland Empire"], ["Audrey", "Corona"], ["Alain", "San Diego"]]
print(mentors)
Out[88]:
okay, let's loop through them.
In [89]:
for mentor in mentors:
print(mentor)
We can loop through the big list, then use indexes to grab stuff from the internal list
In [91]:
for mentor in mentors: # Don't forget to end the statement with a :
print("Hello")
print("My name is", mentor[0])
print("I live in", mentor[1])
In [92]:
for name, city in mentors: # Don't forget to end the statement with a :
print("Hello")
print("My name is", name)
print("I live in", city)
You can also do this without the use of a loop
In [94]:
danny, audrey, alain = mentors
print(danny)
Out[94]:
A set is similar to a list, except it eats the duplicates.
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.
In [98]:
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."""
We'll use the split() method of strings to chop that up into words.
In [99]:
words = gettysburg_address.split(" ")
print(words)
Not perfect, but lets count the words it generates using the len() method
In [100]:
print(len(words))
If you look at the address however, you'll see that 'and' is used several times. Let's turn our list into a set. Using.... well.... the set method.
In [102]:
wordset = set(words)
print(wordset)
Whoa! Looks different! Curly braces instead of bracket.s Maybe even shorter! Let's check, shall we?
In [103]:
print(len(wordset))
As you can see, sets removed duplicates.