Practical Guide to Python Tuples
Transcript from the "Tuples" Lesson
>> So, now that we've got those beginner types down, the integers, the strings, the lists, we're gonna talk about some of the more advanced data types in Python and those are sets, tuples, and dictionaries. These container types let us organize other types of objects into data structures and take actions on them.
[00:00:21] Some of these are mutable, some of them are not. They have different ways of accessing the values inside. Just like I had a cheat sheet for lists on the course website, I also have a cheat sheet for sets Tuples and dictionaries. So after this chapter if you feel like you're still not sure what the differences are, how to access values in them what methods you can call if they're mutable or not.
[00:00:49] Just go back to the cheat sheet. It will be your handy guide and spirit animal tuples are The first data type that we're going to talk about they're lightweight collections that are used to keep track of related but different items. There's no rule or syntax error that says they have to be used this way.
[00:01:09] Lists are generally used to have contained similar types of items in them. But again that's just a guideline and you can use them however you want. The difference with tuples and lists is that tuples are immutable. Once a tuple is created, you cannot change the items in it.
[00:01:29] You cannot append you cannot pop. You cannot do an assignment at an index like you could with a list But tuples are sorted. So they have an order to them and how they're stored so you can access the zeroeth item in a tuple and the first and so on and so forth.
[00:01:55] There are occasions where you expect an immutable types you don't want to pass a list into a function because it might get changed by something later down the line. And so tuple is a great way of ensuring that the data structure that you are passing around won't change.
[00:02:16] One of the good uses for tuple is storing information in a row in a spreadsheet. So a spreadsheet might have different values and different columns and the tuple can be used to represent that. Now, I'm not going to go too deep into tuples. But there are some important things that I want to show you.
[00:02:42] And again, I really recommend that you type along with me in the ripple because that's how you're going to get the muscle memory to really differentiate between these different kinds of data types. So a tuple like a list was defined with square brackets. tuple is defined with parentheses.
[00:03:08] So if we assign this to a variable And we take a look at the type. We'll see that it's a tuple. But there's kind of a gotcha here. What if I say that my tuple only has one item in it Can anyone on zoom tell me what the type is of my tubal, my tube
>> String I believe
>> It's a string. And that's because, remember when we did our math operations and we defined The order that we would like things to be executed with parentheses. Python doesn't really know how to differentiate that syntax from the tuple syntax. So tuple isn't just the parentheses tuple is actually the parentheses and a comma Since a very, very important differentiation, this one has bitten me lots and lots of times.
[00:04:16] If you want to define a tuple with one item in it, you need to make sure that you include a trailing comma. So that will look like this. Now, if I say that my_tup is equal to this and I ask for the type(my_tup), it is now a tuple.
[00:04:34] So make sure that you have that comma in there. Now, when you're creating a tuple you can just use these parentheses and then let's say we have a student and we're going to have a name. And we're going to have, age and subject maybe like a GPA. Now we can get the item that is at the zeroeth index.
[00:05:10] Remember, tuples are immutable and we cannot change it. One of the useful features of tuples that you might see and lists in Python is called tuple unpacking So you can get multiple items out of a tuple at once. And we can do that when we declare our variables we separate them with commas.
[00:05:39] And then assign those to the value of the tuple. And now I have all of these pulled out. Note that when you do this, the number Have arguments on the left hand side need to match the number of argument items in the tuple or you're going to get this value error too many values to unpack.
[00:06:01] You will see this error a lot. So remember it. That means that the items, number of items on the left hand side of the expression don't match the number of items in. The right hand side, if there's something that you don't care about, you can use a shortcut called underscore.
[00:06:19] And that means like, I need, I know I need to match the number of items. I don't care about saving this one in a variable, toss that one away. So that's also syntax that you might see.
>> Does that syntax work with arrays or only tuples? It works with lists as well.
[00:06:36] But generally, you'll mostly see the syntax used with tuples. Because remember that lists are mutable. So as lists change, you don't have that guarantee that the number of items on the left hand side are going to match the expression on the right hand side.