Python Fundamentals Tuples
Transcript from the "Tuples" Lesson
>> Nina Zakharenko: Tuples are a lightweight collection that are generally used to keep track of related but different items. And again, that's just how they're used in practice, but you're free to do whatever you want, Python's not gonna stop you.
>> Nina Zakharenko: While lists are mutable, right, we were able to I add, remove items from them, changed the values.
[00:00:26] Tuples are immutable, that means that once a tuple has been created, the items in it can't change. Okay, and you might ask, okay, we just learned about lists, they are awesome, why do we need tuple that sounds complicated?
>> Nina Zakharenko: Tuples are useful for things like containing a snapshot of data, so let's say you wanna represent a spreadsheet in your code, a tuple might represent each row in the spreadsheet, right?
[00:01:00] So your spreadsheet has different columns, and each position in the tuple relates to a value in a different column. And these tuples can't continually be changed, they can't be added to or removed from like you would do with a list, which also provides you a little bit of added security with your data, right?
[00:01:21] You generally know that the tuple is gonna look the same when you access it later on, so looking at tuples in practice.
>> Nina Zakharenko: To make empty and one-item tuples,
>> Nina Zakharenko: You use parentheses, just empty parentheses, the type(a| we'll see is a class tuple.
>> Nina Zakharenko: There's something kind of tricky here, all right?
[00:02:02] If the square brackets are associated with the list, you might think that the parentheses are associated with tuples, right? We use two empty parentheses, and we end up with an empty tuple, but pay attention, things get tricky when we try to make a tuple that just has one item in it.
>> Nina Zakharenko: Let's say I try to make this tuple b, if I check the type(b|, we'll see that the class is actually int, okay, what happened there? So tuples are not just parentheses, tuples are about commas, they're kind of the combination of,
>> Nina Zakharenko: Optional parentheses, but the comma is the important part.
[00:02:57] So knowing that,
>> Nina Zakharenko: How would I make my one item tuple be a tuple?
>> Class: Add a comma.
>> Nina Zakharenko: Add a comma, and where would the comma go?
>> Class: After the 1.
>> Nina Zakharenko: After the 1, if I check the type (c| now, it's a tuple.
>> Nina Zakharenko: Unfortunately, doing this is a syntax error in Python, right, just doesn't make sense.
[00:03:32] So that's why we have the two empty parentheses being the empty tuple, but the comma here being what makes a one item tuple. And now that I have my comma, I can make tuples that are as long as I want. Let's say we have a spreadsheet of students, and we'd like to represent each row in the spreadsheet with a tuple.
[00:03:56] We can say student is opening parentheses, and let's say her students name is Marcy, and she's 8, and her favorite subject is history, and her GPA is 3.5. Just like with a list, I can go through and access the items in my tuple by index, so what year will student|0| return?
>> Class: Marcy.
>> Nina Zakharenko: Marcy, right, and student |1|.
>> Class: 8.
>> Nina Zakharenko: 8, right, okay, I told you that tuples are immutable, so what if I try to change the value in my tuple, what will I see?
>> Student: TypeError, object does not supported-
>> Nina Zakharenko: TypeError, tuple object does not support item assignment, makes sense, right, it's immutable.
>> Nina Zakharenko: And because of that, do you think tuples have an append or extend method, or pop method?
>> Class: No.
>> Nina Zakharenko: No, right, they don't, you can't change them. All right, so all of this sounds like a lot of work without a lot of benefit, right, kind of already have lists, why do we need this new data structure?
[00:05:14] Well, not so, tuples are actually really great when you depend on your data staying unchanged. And because of this guarantee, you can use tuples in other types of containers where the container needs a key that is immutable. Like a dictionary key needs to be immutable, you can use a tuple, as long as the tuple contains all immutable data.
[00:05:41] So we'll talk about that a little bit later, but that's an important distinction, you can not use a mutable data type that can be changed, added to, as a key in data structure like a dictionary, you will just get an error.