Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Combining values, making multiple variables, and having smarter return values from functions. One of the most common design patterns in Python.
As you look through code written by other Python developers, and the Python docs themselves, notice how often you see the *args, **kwargs
pattern in function definitions. It's everywhere!

0:00
So think back a few videos to when we talked about packing and

0:03
unpacking dictionaries.

0:04
We could take a dictionary and unpack it into keyword arguments for a function, or

0:08
we could pack arbitrary keyword arguments to a function

0:11
into a single dictionary parameter.

0:13
Both of these use the double asterisk operator.

0:15
Tuples have packing and unpacking too and it's similar to how dictionaries work but

0:20
a bit more useful.

0:21
One of the biggest advantages of Tuples is that they have simultaneous assignment

0:25
which lets us swap values around or assign multiple values at once.

0:28
Why we would want to do that?

0:29
Well, it lets us be more memory efficient and lazy.

0:33
Lets say we have two values.

0:34
I'll use something simple but these could honestly be anything, strings,

0:37
dictionaries, objects, whole programs.

0:39
And for some reason, I need to swap these two values around.

0:42
I need a to be equal to whatever b is, and vice versa, so b has a's current value.

0:47
In most languages, I'd have to bring in another variable.

0:50
I'd make c equal to b's value, make b equal to a's value,

0:53
make a equal to c's value and then delete c.

0:57
Well, it's confusing just saying it.

0:59
You do this a few times and you get used to it but it's not necessary in Python.

1:03
Let me show you Python's way of handling the same problem and more in work spaces.

1:08
Let me recreate the problem from that last little bit of video.

1:10
So I'm gonna have a is equal to 5 and I'm gonna have b is equal to 20.

1:15
And now I need a to be 20 and b to be 5 without bringing in c.

1:20
and believe it or not, this is just a single command in Python a, b = b, a.

1:26
If I look at a, it's 20 and if I look at b, it's 5.

1:31
But that's magic, right?

1:33
Well, it sort of is, until you remember how tuples are made in Python and

1:37
you learn how unpacking works with them.

1:40
Let me reset things and make another example, right?

1:42
So we had a equal to 5 and we had b equal to 20 and I wanna make c.

1:48
I know I said I would bring in c but here's c.

1:51
And I make it equal to b comma a. Now let's look at c.

1:54
Okay, so I made a new variable named c.

1:58
And I made it equal to a tuple of b and a., right?

2:00
Because here we have b and a.

2:03
Here we have b and a.

2:04
So c is equal to this, right?

2:07
It's the same as that.

2:09
So it's a tuple of 20 and 5.

2:11
Remember though right that tuples are made with commas not parentheses.

2:15
And you can only make new data on the right side of an equal sign, right?

2:19
By trying to do like 5 = d then that doesn't work, right?

2:23
Okay, so let's go back to the swap before I did a, b = b, a, right?

2:31
Which means that Python made a tuple right here on the right side of the equal sign.

2:37
In that tuple where the value is 20 and 5.

2:40
And then Python unpacked that tuple into two variables a and b.

2:45
Unlike unpacking a dictionary,

2:47
tuples don't unpack in the key value pairs because well, they don't have keys.

2:52
Instead each item in the tuple unpacks into a variable on the left side

2:57
of the equal sign.

2:58
It looks like magic, but it's following all of the rules we've talked about so

3:01
far, so that's unpacking.

3:03
How do we pack values into a tuple?

3:06
This is mostly done in functions.

3:08
Just like we can use **kwargs to pack a dictionary.

3:11
We can use a single asterisk to pack a tuple.

3:15
All right, for instance if we did def add(*nums) and

3:21
then said that total = 0 for num in nums,

3:27
total += num and then let's return total.

3:32
And if we did add(5, 5) and we get 10.

3:37
If we did add 32 we get 32 because 32 plus zero is 32.

3:43
If we were to print out *nums or just nums inside of the function,

3:48
we'd see that it's a tuple of the arguments to the function.

3:51
Most of the time, you'll see this called args and

3:54
of course any other parameters will get put into their own variables.

3:57
So we could rewrite this as def add(base,

4:01
args) and we can say total = base.

4:06
And for num in args because that's the common name,

4:09
total += num, and then return total.

4:14
And now we get to add(5, 20) to get 25.

4:19
With the exception of **kwargs,

4:22
*args has to come after any parameters in your function definitions.

4:26
So you have like def add(*args, **kwargs).

4:31
That's always the order those two go in.

4:36
Swapping values with tuples is super handy, and so

4:39
is being able to capture an arbitrary number of values in your functions.

4:42
But one of the most common ways that tuples and their packing and unpacking

4:45
powers are used is with functions and methods that return more than one value.

4:49
This is one of the most common uses of tuples in Python, and

4:52
I think it deserves a deeper look.

4:53
Let's check it out in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up