Instructor: Hooray.
It's time for our third data structure,
as we continue down to our different data types in Python.
And this one is called a tuple.
Or tuple, depending on how you like to pronounce it.
And we actually saw this slightly,
when we talked about dictionaries.
But before we get into that,
what are tuples?
Well, a tuple are like lists, but unlike lists,
we cannot modify them.
They are immutable, so you can think of them as
immutable lists and they look like this.
So let's call it my_tuple.
Is going to use brackets to denote a tuple.
And we could do 1, 2, 3, 4, 5, just like a list.
Now you remember how in a list we could've modified this
and said, index of 1 well now is going to equal Z.
If I run this, it's going to error out.
It's going to tell me,
hey, tuple object does not support item assignment.
A tuple is immutable.
Once you create it, it's the way it is.
You can't sort it or reverse it like we saw with lists.
It's just there.
What about this?
Can I just print
a tuple?
And just grab the second item on that tuple?
Well, yes, you can.
Just like a list, you can access it through an index.
Can I maybe do
does 5 exist
in my tuple?
Just like we could in a list?
Well, yes we can.
The difference is that it's immutable right?
Now why do we need this?
I mean, we already have lists.
So why do I have to learn tuple if you know,
it's pretty much the same as lists.
It's the only thing is I can't change it.
There's a few benefits to this.
For example, if you don't need to change the list,
that makes things easier because it tells
other programmers looking at your code,
hey, this shouldn't be changed.
Unlike a list, I want this to stay the way it is.
Keep this order.
So it makes things easier and more efficient.
It makes your code safer because people can't modify, right?
It makes code more predictable.
However, it makes code more predictable,
but it's less flexible than a list.
I can't really sort a tuple.
I can't run reverse on tuple.
Now because of this, because they are less flexible,
the other good thing about them is that
they're slightly more performant than lists,
so they're usually faster than lists.
And if you don't need a list to change, well use a tuple.
So a good use of a tuple, for example,
if you work at Uber is a geographic location
and coordinates, right?
You can have latitude and longitude here.
And because, let's say this won't change,
or maybe a user's location or pickup point
doesn't often change, we can just use a tuple.
However, maybe when you're in a car as a driver,
and you're a Uber driver,
well you probably need your coordinates,
your latitude longitude in a list
because your car is moving.
That's constantly changing.
And we saw a tuple when we talked about dictionaries, right?
Remember when we did user, or we did user.items?
And then I said, hey, just print user.items
and we saw here that we got a dictionary items,
and inside of this dictionary
we see that basket 1, 2, 3 greet hello,
and age 20 are a tuple.
So this actually returns our key in value as a tuple.
And as I mentioned before a dictionary
can have keys that are immutable.
So a tuple is a completely valid key in Python.
So I can do 1, 2 here,
and that is a valid key in a dictionary.
And the way I would access this would be
like user curly bracket 1, 2
so that if I click run here,
oh I have an extra bracket, let's fix that.
Let's click run again.
And there you go.
I'm able to grab user tuple 1, 2, and grab 1, 2, 3 list.
You see here how I'm trying to use different
data structures, combining them together.
Let's take a break and
finish off our understanding of tuple in the next video.