

Pretty neat! Next step is the conclusion and the course review.Write a Python program to unpack a tuple in several variables And then inside the while loop, you’re doing something very similar, just modifying it a little bit by assigning a on the left to b.Ġ5:05 But you’re taking b and you’re saying that now equals a+b. In teaching a little bit about programming they showed this example that I liked a lot about the Fibonacci series.Ġ4:47 Here, we’re assigning the first two, 0 and 1, and then creating a while loop. It brings me to this kind of cool example that I saw from. And you can see that the swap has occurred. But in Python, the swap can be done with just a single tuple assignment.Ġ4:24 Here you’re going to say a, b = b, a. And there-you’ve swapped the two.Ġ3:58 So again, you’re making a temporary variable that holds a, taking b, assigning it into a, and then b pulling that temp back into it by reassigning it again.Ġ4:08 That’s the swap. It would look something like this.Ġ3:48 So you create a variable temp, assign a into it, assign b into a, and then say b = temp. In most programming languages, it’s necessary to store one of the values into a temporary variable while the swap occurs. Frequently when programming, you have two variables whose values you need to swap. It works the same whether the parentheses are included or not, so if you have any doubt as to whether they’re needed, go ahead and include them.Ġ3:14 This tuple assignment allows for a bit of idiomatic Python. When doing assignments like this, there’s a handful of situations where Python’s going to allow you to skip the parentheses.Ġ2:50 You can even do something like this, where both sides don’t have parentheses.Ġ3:02 Even creating that singleton. Again, the two sides have to be equal.Ġ2:23 Here, again, too many values. Packing and unpacking could be done into one statement if you wanted to.Ġ2:12 And here they all are.

And what if you went with too many? Well, in this case, it was expecting to get five, but t only provided four. There’s too many values to unpack.Ġ1:41 It was expecting three on this side and t, as you know, has four. If you tried to assign it to (s1, s2, s3) = t, you are going to raise an exception here, a ValueError. It’s pretty neat!Ġ1:27 Now, it’s important that they have the same number on both sides of that assignment. You can create another tuple of objects-in this case, (s1, s2, s3, s4), and you could assign it to the tuple that you created a moment ago, t.Ġ1:13 Now, s1, s2, s3, and s4 will have unpacked that tuple during that assignment and placed them into the appropriate objects. Again, you can access them via index.Ġ0:56 Here’s an interesting idea. As you saw in the previous video, you can create a tuple just by typing the objects into the set of parentheses, and that will pack them all in there, into that single object. When unpacking, the number of variables on the left have to match the number of values that are inside of the tuple.Ġ0:33 Let me have you explore that with some code. A literal tuple containing several items can be assigned to a single object, such as the example object here, t.Ġ0:16 Assigning that packed object to a new tuple, unpacks the individual items into the objects in that new tuple. 0 1 1 2 3 5 8 13 21Ġ0:00 In this video, I’m going to show you tuple assignment through packing and unpacking.
#Python unpacking series
> temp = a > a = b > b = temp > a, b ('egg', 'spam') > a = 'spam' > b = 'egg' > a, b ('spam', 'egg') > # Ready for Magic Time! > a, b = b, a > a, b ('egg', 'spam') > # Fibonacci series > # the sum of two elements defines the next > a, b = 0, 1 > while a < 30. t = ( 'spam', 'egg', 'bacon', 'tomato' ) > t ('spam', 'egg', 'bacon', 'tomato') > t 'spam' > t 'egg' > ( s1, s2, s3, s4 ) = t > s1 'spam' > s2 'egg' > s3 'bacon' > s4 'tomato' > ( s1, s2, s3 ) = t Traceback (most recent call last):įile "", line 1, in ( s1, s2, s3 ) = t ValueError: too many values to unpack (expected 3) > t ('spam', 'egg', 'bacon', 'tomato') > ( s1, s2, s3, s4, s5 ) = t Traceback (most recent call last):įile "", line 1, in ( s1, s2, s3, s4, s5 ) = t ValueError: not enough values to unpack (expected 5, got 4) > ( s1, s2, s3, s4 ) = ( 'spam', 'egg', 'bacon', 'tomato' ) > s1 'spam' > s2 'egg' > s3 'bacon' > s4 'tomato' > ( s1, s2, s3 ) = ( 'spam', 'egg', 'bacon', 'tomato' ) Traceback (most recent call last):įile "", line 1, in ( s1, s2, s3 ) = t ValueError: too many values to unpack (expected 3) > t = 1, 2, 3 > t (1, 2, 3) > type ( t ) > x1, x2, x3 = t > x1, x2, x3 (1, 2, 3) > x1, x2, x3 = 4, 5, 6 > x1, x2, x3 (4, 5, 6) > t = 2, > t (2,) > type ( t ) > a = 'spam' > b = 'egg' > a, b ('spam', 'egg') > # You need to define a temp variable to accomplish the swap.
