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.