Let's talk about another data type, lists, which are also a sequence.
A list is literally just a list, but we have a few different
types in Python. We have this one called a list;
we have another one called a tuple or a tuple,
some people call it a tuple; and a set.
These are the three main ones you're usually going to work
with in Python code.
Now, these lists or these type of sequences, you can think
of them as a 'grocery', 'Grocery list'.
So you're going to the store and you have to pick up milk, bread and eggs,
And I've made this metaphor a few times now, but you've got
milk, bread and eggs. And you walk into the store and you're
like, "Oh, Hey, I'm going to pick up milk, cross it off your list.
I am going to go pick up eggs,
cross that off your list.
I'm going to go pick up bread,
and you cross that off your list. That's all these are doing.
So let's actually turn this into code.
So a list starts with and ends with these hard brackets, '[]'
And I just made that one step bigger there.
And you could say 'Milk',
and that's a string.
You could say 'Eggs'.
That's also a string.
'Bread'. That's also a string.
And remember, each one of these is also a sequence, but it
is also its own item in this list, in this array.
And then just for funsies, I want to show you that we can
also use a number,
so let's say the number 42; and a Boolean.
And if I just display this, 'groceries', we can see we've got
'['Milk', 'Eggs', 'Bread', 42, and True]'.
Now I'm going to create this thing called a for loop.
You don't need to know what that is right now.
You don't need to understand how it works or the syntax behind
it, because this is just for a demonstration.
We'll get into loops down the road and we'll get some proper
education around that as well.
But for now, this is just going to be a demonstration.
So 'for item in groceries',
and because this is a sequence or a list, it's going to go
through 'Milk', 'Eggs', 'Bread', 42,
and True. And so what I'm doing here, is I'm just saying for
each one of these, whatever it's called in here, 'Milk', 'Eggs',
'Bread', 42 or True, just call it an 'item'.
And so that is a variable called 'item'.
And I can just print that.
And when I run this, 'Milk', 'Eggs', 'Bread', 42 and True. And now
if we want to, we can actually change this to be a tuple.
So let's rerun the cell, this cell,
and you can see that the brackets changed here as well,
so we know it's a tuple.
And when I rererun the for loop, it looks like nothing happened,
but that's because it just has the same items in there.
We can even do the same thing with a set.
And a set looks almost identical.
But let's go ahead and add 'Bread' for a second time, and also
'Milk' for a second time.
So we've got 'Milk', 'Eggs', 'Bread', 'Bread', 'Milk', 42, and True.
Let's run that and run this.
And now you can actually see that it's not in any particular
order, but 'Bread' and 'Milk' only show up once, whereas up here
it was specified twice.
So that's the difference between a set and a list. A set
makes sure that every item in there is unique.
So you have 42, 'Bread', 'Eggs', 'Milk', and True.
It is also a sequence,
so when we rerun this instead of saying 'Bread' twice and 'Milk'
twice, it only shows up with 'Milk' and 'Bread' once because
it's unique to that particular sequence.
Now, one thing to keep in mind here is that this sequence
did not keep its order at all.
It started with 'Milk', 'Eggs', and 'Bread',
and then for whatever reason, when Python internalized this,
it went to 42, 'Bread', 'Eggs', and then even in our for loop,
when we just wanted to print back all these different items
on their own lines, it started with - True, 'Bread', 42.
So none of those actually match up.
So it didn't keep their order.
So if you need something to absolutely keep their order,
definitely use a tuple, which has the softer parentheses,
brackets; run, run, run;
and that keeps its order.
Or you can use the list with the harder brackets; run, run, run;
and get the exact same results.
So that is lists and lists like sequences in a nutshell.
Now that you sort of understand what those kind of look like
a little bit, let's move on to the next one.