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 there 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 can do one, two, three, four, five.
Just like a list.
Now, you remember how in a list we could have modified this and said index of one, well, now is going
to equal Z if I run this.
It's going to air, 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 five
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.
Why do I have to learn tuple if, you know, it's pretty much the same as list?
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 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, we'll use a tuple.
So a good use of a tuple, for example, if you work at Ueber 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 couple.
However, maybe when you're in a car as a driver and you're Uber driver, well, you probably need your
coordinates, your latitude, longitude and a list because your car is moving.
That's constantly changing.
And we saw tuple when we talked about dictionaries.
Right.
Remember when we did user or we did user dot items?
And then I said, hey, just print user dot items.
And we saw here that we got a dictionary items and inside of this dictionary we see that basket one,
two, three, greet hello and H20 are a tuple.
So this actually returns are key and value as a couple.
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 want to hear and that is a valid key in a dictionary.
And the way I would access this would be like user.
Curly bracket one, two.
So that if I click run here.
Well, 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 one, two and grab one, two, three 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.