Understand Python tuples, an essential data structure in machine learning, and learn how their immutability protects data integrity. Gain practical skills in tuple unpacking to efficiently handle multiple return values in Python programming.
Key Insights
- Tuples in Python are immutable collections of numeric or string values enclosed in parentheses, frequently encountered in machine learning scenarios to return multiple related values without the risk of accidental modification.
- Python provides tuple unpacking, enabling developers to efficiently assign tuple contents directly to individual variables (e.g., assigning a tuple containing a name and age directly to corresponding variables).
- The article illustrates practical examples using tuples with real data, such as using
stats.mode()
to identify the mode of grades and its frequency, demonstrating tuple unpacking to simplify code readability and usability.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Let's talk about tuples. We'll see this kind of value occasionally in machine learning, because sometimes it's a useful kind of value to get back, and we need to know how to work with it. So a tuple, which is what the mode returns to us, this mode result value here, a tuple is an immutable collection of numeric values in parentheses.
So it's actually two values, right? It's our mode number, in this case, 85. And it's our count number, in this case, two. And it's pretty useful when something wants to give you multiple values.
We don't want to just know the mode number. We also want to know how often it's occurring. So let's take a look at what that looks like in real life.
If you want to create a tuple, which you don't do very often unless you yourself are writing a library, or perhaps a complex function, to return a tuple, to create a tuple, you might say instructor, I'm just going to write some, make some tuples here as an example. Instructor info equals a string, a colon Jaffe, and a parentheses allows us to put multiple values in here, similar to a list, similar to the square brackets for a list. And then we can put my age, 44.
Now, if I look at instructor info, instructor info, it looks just like what I, what I put in there. But we can also look at instructor info at index zero, just like with a list. It's a string colon Jaffe.
And index 144. Now, one of the special abilities, the reason it's immutable, we can't actually change a tuple. And that actually is because most of the time, when somebody is, oh, I'm accidentally editing this.
I'll delete it. I'll fix it in post. When somebody is giving you back a tuple, you're not really there to work with that tuple.
It's just to give you back a couple of different values. So, it's good that it's immutable, so you don't accidentally change the value that this thing gave you. We don't want to change the mode, right? Why would we change the mode? So, it's protected from that by being a tuple.
So, even though I can evaluate instructor info one, I can't change my name, my age. Oh, if only I could go back to the young age of 35. We get an error if I try to do that.
Tuple object does not support item assignment. So, that's the way in which a tuple is immutable. Now, if we want to access it, it's kind of annoying to do something like this.
If we wanted to say, like, name equals instructor info at index zero, and age equals instructor info at index one. I mean, we could do that. That's how we could save these values.
But it's kind of annoying. And you have to sit there and count the indexes. So, Python gave us a great way to handle this, which is called tuple unpacking.
I could say name comma age equals instructor info. And that will separate out the two values from instructor info into the variables name and age. And we can see that print name, print age.
Colin Jaffe 44. And the order matters here, of course. If I switch these values, age and name, well, now name is 44.
And age is Colin Jaffe. That's incorrect. So, we want to make sure that we're doing the order correctly.
And there we go. Now, let's work let's get those values from let's unpack the values from our mode in the same way. We could say mode and mode count, perhaps, equals stats.mode of our grades.
So, what that will do is stats.mode, remember, returns this tuple. Just like when you looked at mode grade up here from stats.mode grades. Gave us that tuple.
And we'll unpack it into the two values mode and mode count. We can print out mode is our mode. Nope, not mod.
Print mode count or just count is mode count. And if I run that, mode is 85, count is 2. We've successfully unpacked from that tuple. We'll be using tuple unpacking a couple of times in this course.
A few times, actually. Because one of our main ways of working with data will be to split it into different parts. And when we split it into different parts to work with different parts in different ways, we'll be getting back tuples to unpack.
So, it's good to understand this.