0:00

[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.

Â 0:28

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.

Â 1:23

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.

Â 1:49

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.

Â 2:59

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?

Â 4:51

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?

Â 5:53

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.

Â