Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Tuples
Lecture: Tuple assignment and unpacking

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Tuples play a central role in Python, we are going to look at variety of techniques and ways to use tuples that are particularly Pythonic,
0:10 let's start with assignment and unpacking, and we'll start in code. So, you probably know that tuples are defined like this, so it could be a number,
0:20 another number, a string, even be like an array. So if we wanted to just look at the our little creation here, we can go like this,
0:29 we'll see that it's actually not the parentheses that have anything to do with this,
0:35 usually, we could just as well write like this, and we get the same output, it's the commas, not the parentheses that make the tuple.
0:45 In fact, we can come down here and can have a shorter one, like this,
0:50 sometimes you want to have a tuple with just one element in it, we could write this,
0:54 there is a tuple of length 1, let's find out. There, a tuple of length 1. So that tuples, they can't grow after they are created,
1:04 they are basically immutable objects that it can be added to or moved, things like that. So that's not the Pythonic thing, that's just tuples.
1:13 We get values out of them like this, if we wanted to say print out the word "cat" that's in the second index position, zero-based,
1:19 so there we'd print "cat", if we didn't actually do this, there that prints "cat",
1:29 so the first thing we want to look at is unpacking these into variables, let's go over here and work with the shorter version,
1:35 so let's suppose we had the number 7 and the word "cat" and that was all,
1:39 if I want to have 2 variables, one for the number and one for the... let's say animal,
1:46 I could say "n for number = this" and I could say "a for animal = that", that would not be Pythonic, instead, what you would say is "n, a = t",
1:58 and Python will unpack the first value into the first thing here, the second value into the second thing there, and so on.
2:05 Now, if we had another one, like another number here, and we try to run this, you'll see it crashes because it says too many values,
2:12 so if we are doing that we would typically say I don't care what this value is and use an underscore to say "please ignore it", so let's run that.
2:19 And PyCharm is just saying "look you assign these values and you never use them here", so let's do this.
2:25 Now, maybe we should show you what numbers came out or what values came out of there, so we'll say like so, "n is 7", "a is cat", beautiful.
2:33 And underscore, well we could grab it but we don't care, that's the whole point of it. So this concept lets us assign values in a single line
2:41 so I could say "x, y = 1, 2", then I could print "x" and "y" and I would get 1 and 2.
2:50 Finally, this tuple unpacking is a very important when we are talking about loops,
2:56 remember our numerical "for...in" loop where it gives us both index and the item,
3:01 we wrote something like this: "for index, item in enumerate", something like this, for enumerating over a collection, we'll get the index and the item,
3:10 so 0 had 1, "cat" and so on, there, 0 goes to 1, 2 goes to "cat", well this returns a tuple and we unpack them into index and item,
3:23 so this tuple unpacking is super important and we'll see more of it as we go through this chapter.
3:29 So we saw we can create a tuple, we can unpack it into a variety of values,
3:32 should we have a tuple of 4 values, we unpack it into greeting and one into enclosing, then we just print them out, you see the values come out
3:40 as if they had been pulled out individually, we also saw that enumerate returns tuples
3:45 and the way that we actually separate the values in a really nice clean way maybe didn't even notice this one has happened
3:51 is we are actually using tuple unpacking into those two items as it comes back from enumerate.

Talk Python's Mastodon Michael Kennedy's Mastodon