WEBVTT

1
00:00:00.000 --> 00:00:03.900
All right, that you're familiar with
packing, let's talk about unpacking.

2
00:00:03.900 --> 00:00:06.010
Unpacking is basically
the opposite of packing.

3
00:00:06.010 --> 00:00:10.140
It's exploding a Python sequence
into individual variables.

4
00:00:10.140 --> 00:00:13.050
This is also sometimes
called multiple assignment.

5
00:00:13.050 --> 00:00:15.022
You'll see this often in function returns,
but

6
00:00:15.022 --> 00:00:18.230
that's definitely not the only
place where this comes in handy.

7
00:00:18.230 --> 00:00:19.650
The same is true for packing as well.

8
00:00:19.650 --> 00:00:23.511
Both packing and unpacking are extremely
useful Python tools that you can take

9
00:00:23.511 --> 00:00:25.572
advantage of throughout your codebase.

10
00:00:25.572 --> 00:00:28.470
Here we have a function called unpacker.

11
00:00:28.470 --> 00:00:29.640
Unpacker doesn't do a whole lot.

12
00:00:29.640 --> 00:00:32.450
It simply returns a tuple
with three values in it.

13
00:00:32.450 --> 00:00:34.113
One, two, and three.

14
00:00:34.113 --> 00:00:35.416
Unpacking, however,

15
00:00:35.416 --> 00:00:40.000
allows us to assign each of those values
inside the tuple to its own variable.

16
00:00:40.000 --> 00:00:40.850
That's gonna look like this.

17
00:00:50.230 --> 00:00:51.846
Let's discuss what's happening here.

18
00:00:51.846 --> 00:00:55.284
On the right side of this variable
assignment, I'm calling the function,

19
00:00:55.284 --> 00:00:55.830
unpacker.

20
00:00:57.240 --> 00:01:02.010
Unpacker returns a tuple
containing the values 1, 2, and 3.

21
00:01:02.010 --> 00:01:05.738
Because the left side of our variable
assignment has more than one variable

22
00:01:05.738 --> 00:01:09.510
name, Python understands that
we're trying to unpack this tuple.

23
00:01:09.510 --> 00:01:13.308
Just like passing multiple arguments
to multiple parameters is positional,

24
00:01:13.308 --> 00:01:14.695
so is unpacking a sequence.

25
00:01:14.695 --> 00:01:22.760
In this example, the first element in
the tuple 1 will be assigned to var1.

26
00:01:22.760 --> 00:01:24.820
The second, 2, will be assigned to var2.

27
00:01:24.820 --> 00:01:28.147
And finally, 3 will be assigned to var3.

28
00:01:28.147 --> 00:01:31.410
I'm gonna print out each of
these variables so you can see.

29
00:01:41.240 --> 00:01:43.050
Now I'll save this and run it.

30
00:01:48.971 --> 00:01:52.659
Awesome, each element in the tuple
was assigned to its own variable and

31
00:01:52.659 --> 00:01:54.800
printed to its own line.

32
00:01:54.800 --> 00:01:56.670
Notice how in the variable assignment,

33
00:01:56.670 --> 00:01:59.864
I matched the number of variables
exactly to the length of the tuple.

34
00:01:59.864 --> 00:02:03.810
Unpacking is strict and
requires exactness.

35
00:02:03.810 --> 00:02:05.112
Open up the attached workspace.

36
00:02:05.112 --> 00:02:07.610
Inside you'll see what's
up here on screen.

37
00:02:07.610 --> 00:02:10.510
Pause the video and try to change
the code to see what happens.

38
00:02:10.510 --> 00:02:13.510
What's the result of removing
a variable name on the left side?

39
00:02:13.510 --> 00:02:15.024
What about adding one?

40
00:02:15.024 --> 00:02:18.930
What about changing the two bullets
returned from the function?

41
00:02:18.930 --> 00:02:22.560
After you're done playing around,
unpause the video and we'll keep going.

42
00:02:22.560 --> 00:02:24.110
Welcome back.

43
00:02:24.110 --> 00:02:26.355
We've just seen how
unpacking works with tuples.

44
00:02:26.355 --> 00:02:30.410
But it also works with any
Python sequence, even strings.

45
00:02:30.410 --> 00:02:32.670
Let's rewrite the unpacker function
a little bit so it returns a string.

46
00:02:40.060 --> 00:02:42.881
Now, keeping everything else the same,
let's run the program and

47
00:02:42.881 --> 00:02:43.880
see what it prints out.

48
00:02:43.880 --> 00:02:45.950
Let me clear this so
there's a little more room.

49
00:02:51.030 --> 00:02:54.870
Cool, each letter of the Python
sequence hey, which is just a string,

50
00:02:54.870 --> 00:02:57.930
has been unpacked into its own variable.

51
00:02:57.930 --> 00:02:58.960
This also works with lists.

52
00:02:58.960 --> 00:03:01.590
If you can loop over it,
you can unpack it.

53
00:03:01.590 --> 00:03:03.900
Let's more forward and
look at a practical example.
