So far, we have been talking about programs that only contain single pieces of data. You might have multiple variables, but each variable only contains one thing. Might be a number, might be a string, might be a Boolean value like true or false, but regardless, it contains only one thing. Now, that's okay for simple programs, But as your programs get more complex and interesting, you're going to need to be able to have the capability of associating multiple pieces of data together with each other. You want to hold them together. Now, Python has many different ways of doing this, but the most common is to use a sequence type. There are many different sequence types in Python but today we're going to talk about the most common one, Lists. So with that, let's get started with our lecture on Lists. So I want to talk about Lists. Okay? What is a List? Well, let's make a list. Alright? I've got a list of things I need to do. I need to teach class. I need to get some sleep. Yeah, like that's actually going to happen, okay? I need to skip my meetings. Let's all six of them that want to go. Alright, so I have a list here and I think you'll agree, this is a list. This is my to do list, perhaps for today of things that I would like to get done and probably won't. Okay? So, How does that relate to this in Python? Well, pretty much this is the general concept. I have a bunch of things that I want to sequence together. In fact, I put them in an order here. I might actually number them if I wanted to, you know, this is number zero, this is number one and this is number two on my list of priorities. Okay? In Python, I would make a list. I don't write it out like that, I write it in square brackets. So the square bracket says a list and this right here is an empty list. Okay? There's nothing in it. So, I have the square brackets around it. I could create a list of numbers one, two, three. I could create a list of strings. And don't forget the quotes around here. Okay? So all of these things are lists and they associate a bunch of data, Zero or more pieces actually of data together. So I've got my empty list, I've got my list with three numbers, I've got a list with two strings in it. Now, usually I will be assigning these to variables so I might have something like, position equals the list four, nine and implicitly, this here is maybe is my x coordinate and this is my y coordinate, So I know that my position is x = four and y = nine, okay? So, Out of context, without me writing this, you don't necessarily know what these things are. You know, I've got this empty list, what does that mean? I've got nothing to do today? You know, that would be nice. [laugh] I'll take that. This list of numbers here, who knows what this means. Maybe these are the times I wish my next three meetings that I fully intend to skip are going to occur at. Maybe these strings here are greetings that I'm going to say to you, depending on whether I'm just meeting you or I'm about to leave, okay? Again, the position might be xy, the position might be some other coordinate system. Okay. So, a list is a way of associating data, But it doesn't give you a way of saying exactly what the are, that's something that you are going to keep track of. Okay? But basic, the basic idea here, a list, square brackets, Zero more things doesn't have to be zero, one, three, two, whatever, that I've got here. It could be zero, five, a 1000, a 100, whatever, okay, but I'm just going to keep a bunch of data together. And this is going to allow me to have one variable here, position, that keeps track of the x and y position of something rather than having to have two variables, one for the x value and one for the y value. Okay? So let's look at what this actually means in a program. Alright, let's actually have some fun with lists. So what can I do with lists? Well, before I can do anything, I need to be able to create a list. Let's create an empty list. Alright, maybe this is not so useful, but I just want to show you that you can have a list with no elements. When I print it out, it uses this bracket form, so two brackets with nothing inside them, mt<u>list. It's not all that useful, maybe sometimes</u> I might want that, but usually I'm not going to. Let's make a list with a bunch of numbers in it. And print that. You can see that I now have a list over here that has a bunch of numbers in it., [1, three, four, -seven, 62, 43]. Lists are not limited to numbers. I can have other things in my list as well to put my grocery lists up here. , There we go, print two. Now I have a list with strings in it. Okay? So there's no reason why it has to be numbers, it can be strings, I can have Boolean values, I can even have other lists in my list. . Okay, so what did I just do here? I created a list that has lists inside it. Now, here's a question for you. How many elements are actually in this list? Let me give you a second to think about that. Right. There are a couple of possible answers here. I can count three, three, a, b, c so I can get five. That's actually not the correct answer. The correct answer is three. Okay? There is this first list right here, then there is this second list right here, and then there is this empty list right here. Okay? So I want to point out the characteristics of a list when you create them in this fashion, I have to have an open bracket and a closed bracket. Inside the brackets I can have any number of elements seperated by a commas, and each element can pretty much be anything that Python will, allow you to put into a variable, any kind of value, so it could be numbers, could be strings, could be other list, could be any other type we're going to talk about, Okay? So, That's nice. I've been able to create a list, I can print a list out to the screen, But it's not really all that useful. To be most useful I need to actually be able to access these things. And before I start accessing, I want to point out the following that you can actually call the len function. And let me do len(l3) just to prove to you that the answer is, in fact, not this last list has a length of three. Okay, so the empty list has a length of zero, this list here has a length of six, this last list here has a length of three. Now, at this point you might be saying, hey, I've seen len before." Yes, you have. You saw len when we talked about strings. And if you go over into the documentation, the CodeSkulptor documentation, I want to point out that both lists and strings are sequences and so a lot of the, operations that we talked about for Strings are also valid for lists. Okay. So you can go over here and take a look at that. And, if you can remember anything that we talked about with strings, they're still going to be valid here. Okay? So, I can still print out the length. I can also access individual elements. Alright. I can access the first elements, I can access the last element, and if you remember from the lecture on strings, there's a shortcut to get to the last element. I can use - one. If I do this, you'll see, I'm going to print one, which was the first element, 43, which was the last element. Okay? I can also access elements in the middle of things. Let's look at that, First element of l3. if I print out that, I remember, first actually is the second, because computer scientists are all crazy and we start with zero. Right, So if I run this, I get the list a, B, c. Write the entire list, because that entire list is the elements in the one position here in this list of list. Okay. I can also do slicing just like I could with strings, let's say I decide I no longer want milk or butter. I can, well, let's actually assign it to a new list. I can do this. Print l4. Okay? What happens? I get a new list with just eggs and bread. Now, recall in Python, when I talk about ranges of things, I have this [1:3] syntax, That means starting at element one up to, but not including element three. So I get element one. Oops. Element one and element two, eggs and bread. Okay? Now, Lists also have a capability that strings do not and we're going to talk about this more as we go along, But I can actually change elements of lists. So if I decide in my grocery list, I no longer want milk. Instead, I want cheese, I can assign to the zero element of the list a new thing. Okay. And I run this. Now, you'll notice, I have cheese, egg, bread, and butter instead of milk, eggs, bread and butter. Okay. Alright. Now, you'll also notice that all of my lists here, I've always had the same kind of thing as every element. You can have as many elements as you want in the list, but good programers keep their lists homogeneous. Where they, if they have a string in the list, all the elements are strings. If they have a numbers in the list, all the elements are numbers. And if there's a list that is an element, then all the elements are a lists. And, trust me this makes your programming life a lot easier. So, I highly recommend that you try to follow that discipline as well. In many ways this is probably a pretty boring lecture. Right? We didn't actually see any useful or interesting example programs, Instead we just looked at the mechanics of lists. Okay. Well, We did that because you need to get the basics down and pretty much all the other lectures this week are going to use Lists while they teach other concepts, so you're going to see plenty of examples with less. So don't worry too much about that, okay? So, if you only take one thing away from this lecture, it should be, well, that I'm going to skip the rest of my, mostly with Joe anyway, right? No? No, no, no, okay? You should take away the fact that lists are a collection of objects. They allow us to keep data that belongs together, together without having to have a bunch of variables to hold it. Okay? We're going to see how that works in action. We're going to see how you can do that in programs as we go on. And you also might have noticed, we've snuck lists in here and there, throughout the, the previous lectures of the class. Every once in a while, they, they found their way into the program. So now you know what's going on, and you're about to know more.