[MUSIC] In this video, we're going to talk about Python Lists. Lists are an important and valuable data structure that you're probably going to see in just about every Python program you look at, or write. So what is a list? Well, a list is just a sequence of values. So first, let's figure out how we can create lists in Python. Let's get started. Sometimes you just want to type a list directly into your program, so let's look at how we do that using List Literals. Lists are indicated by square brackets in Python. So here you can see that I want to create an empty list, a list with nothing in it, so a sequence of no items. I just use [ ], right? That's an empty list, okay? But I can put things in the list, obviously. Otherwise, [LAUGH] it wouldn't be too useful. So here's a list of numbers, right? The numbers 1, 5, 8, 3, and 2. The items inside of the list are separated by commas. So we have an open square bracket, item, comma, item, comma, and so on as many as you'd like. And then when you're done you have a closed square bracket. We're not limited to using numbers inside of our list, we could also have strings. So here is list of letters or a list of full blown strings. All right, let's run this and see what happens. Okay, well, it prints them out in almost exactly the same format that you see that we wrote them in Python. Conveniently allowing me to understand, hey, this what a list literal looks like. When I print it out, it looks pretty much the same. You'll notice in these lists, all of the elements have the same type. So in my numbers list, all the items were numbers. In my letters list, they were all strings. Similarly, in my languages list, they were all strings as well. Hopefully, you enjoyed some of those languages by the way. All right, Python does not require this. It allows you to mix different types in a list and I'm going to tell you right now, don't do this. Just because a language allows you to do something, doesn't mean it's a good idea, all right? When you have a list, you really want it to be homogeneous. In general, when you're looking at different items in a list, if they're different types, it becomes pretty confusing, pretty fast. It becomes easy to forget what's what, and this leads to lots of bugs in programs. So when you have a list, I strongly recommend that you always include only items of the same type within a particular list. But, Python will not restrict you to doing that. You can see here my mixed list has a string, a number, and a boolean in it, and Python happily allows me to do this. You print it out. I've got a string, a number, and a boolean. Python also has a function list that allows me to create lists, so let's take a look at that, okay? All right, so you can see here that I have mylist. I just call the function list. And that's going to give me back a list. What do you think is in that list? Unsurprisingly, it's empty, right? I didn't give the function any items. There's no way for it to predict what I might want. So the only thing it could do is give me an empty list. So let's think about how we might create some items to put in that list, right? Python has a function called range, all right? And this is a pretty valuable function here that creates ranges of numbers. So range(5) here, what it does is it creates a sequence of numbers starting at zero up to but not including the number 5. So let's run this here, right? And so there's a 0 implied. So when we print it out, it prints out range(0, 5). So that's the string representation of that range. Okay, that's only interesting and useful if you understand what that means, but again what it is is a sequence of numbers starting at the first number 0 up to but not including the second number 5. So it should be zero, one, two, three, four. Well how can I see that? Well we can convert it to a list using the list function, so let's do so. [SOUND] There we go, we have [0, 1, 2, 3, 4] just like we thought we would get, okay? Range is a little bit more powerful than this, okay? I can go from different, starting and stopping numbers, so range(7, 13). Okay, now I am explicitly saying I want you to start at 7 instead of implying that you should start at zero. And so it should be the numbers from 7 up to but not including 13. Let's confirm that by calling list. Okay, so if I print out the object itself I get again a string representation range(7,13). If I turn it into a list I get [7, 8, 9, 10, 11, 12], okay? There's also, you can do a three number version of range, okay? So if you just pass one number to it it takes that as the end number and it starts at zero. If you pass two numbers to it, you're explicitly giving it both a start and an end. If you give it three numbers, the third number becomes a step, all right? So, range(4, 27, 5) means all the numbers starting at 4 up to, but not including 27, but increment by 5 each time, right? Before we were implicitly incrementing by one, so let's just take a look. I think it's easier to explain by seeing, okay? So 4 through 27 by 5 gives me [4, 9], right? 9 is 4 plus 5,. Then we add 5 again to get 14, then 5 again to get 19, and then 5 again to get 24, okay? So 24 is less than 27, so that one's included. If we add 5 again, we'd get 29 which is greater than or equal to 27 so that one is not in the range, okay? Interestingly, we can also have negative steps. So (9, 2, -1) says all the numbers starting at 9 up to, but not including 2. All right, up to now becomes a little bit confusing. Maybe it's down to, but not including 2, with a step of minus 1, all right? And again, let's just look at it, okay? We get [9, 8, 7, 6, 5, 4, 3], all right? Okay, so I can have just one number to range, okay? That gives me my stopping point, all right? I can have two numbers to range, giving me both a start and a stop. Or I can have three numbers, start, stop, and step. Just always remember it's up to, but not including the stopping value. Now this is a useful way of creating lists that are sequences of numbers, right? We use range, and then if we actually explicitly need it to be a list, we call the list function to turn it into one. In this video you learned how to type lists into your programs and how to use range to create sequences of numeric values when that's what you need. But we didn't actually learn how to do anything with these lists so hopefully this wet your appetite to learn more about them.