‫Hurray!
‫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 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 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?
‫It's the only thing is I can 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 it, 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 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 an 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 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 h 20 are a tuple.
‫So this actually returns our key and 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 one to here.
‫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.
‫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 one, two and grab one, two, three list.
‫You see here how I'm starting to use different data structures, combining them together.
‫Let's take a break and finish off our understanding of tuple in the next video.