Packing
To pack multiple variables, you need to use the *
iterable unpacking operator. Simply place an asterisk *
before the variable, and it will pack any number of variables. Packing a variable is only possible within a tuple or a list.
Code Description
All samples on the left side are tuples, even if they are not in parentheses.a, b, *c = 1, 2, 3, 4, 5
This line demonstrates extended unpacking in Python. The first two elements of the tuple (1 and 2) are assigned to a and b, respectively. The remaining elements (3, 4, 5) are gathered into a list and assigned to c.a, *b, c = 1, 2, 3, 4, 5
Here, the first value 1 is assigned to a and the last value 5 is assigned to c. The values in between (2, 3, 4) are captured into a list and assigned to b. This form of unpacking is helpful when you want to split a sequence into parts, keeping the first and last elements separate from the rest.a, b, *c = 1, 2
In this case, a and b are assigned the values 1 and 2 respectively. Since there are no additional values to assign, c is assigned an empty list. This illustrates that the starred variable (*c in this case) will capture an empty list if there are no remaining elements.*a, b = 1, 2, 3
This line assigns the last element of the sequence 3 to b and the preceding elements 1 and 2 to a as a list. It's a pattern that is useful when you want to separate the last element from the rest of a sequence.*a, = 1, 2, 3
same as(*a,) = 1, 2, 3
. This syntax unpacks all elements into a as a list. The parentheses and commas indicate that we are dealing with tuple unpacking, but since there's only one element in the tuple (the starred *a), it results in assigning all values as a list.[*a] = 1, 2, 3
Here, the list notation is used explicitly. The elements of the tuple are unpacked into the list a. It's another way to convert a tuple into a list through unpacking.*a = 1, 2, 3
This line attempts to use a starred expression on the left side of an assignment without it being part of a list or tuple, which is a syntax error in Python. Starred expressions in assignments need to be within a list or tuple to be valid. This line demonstrates a common mistake when trying to use starred expressions outside of a list or tuple context.
But the SyntaxError will occur if to use more than one unpacking operator.
¿Todo estuvo claro?
Contenido del Curso
Intermediate Python: Arguments, Scopes and Decorators
2. Arguments in Function
3. Function as an Argument
Intermediate Python: Arguments, Scopes and Decorators
Packing
To pack multiple variables, you need to use the *
iterable unpacking operator. Simply place an asterisk *
before the variable, and it will pack any number of variables. Packing a variable is only possible within a tuple or a list.
Code Description
All samples on the left side are tuples, even if they are not in parentheses.a, b, *c = 1, 2, 3, 4, 5
This line demonstrates extended unpacking in Python. The first two elements of the tuple (1 and 2) are assigned to a and b, respectively. The remaining elements (3, 4, 5) are gathered into a list and assigned to c.a, *b, c = 1, 2, 3, 4, 5
Here, the first value 1 is assigned to a and the last value 5 is assigned to c. The values in between (2, 3, 4) are captured into a list and assigned to b. This form of unpacking is helpful when you want to split a sequence into parts, keeping the first and last elements separate from the rest.a, b, *c = 1, 2
In this case, a and b are assigned the values 1 and 2 respectively. Since there are no additional values to assign, c is assigned an empty list. This illustrates that the starred variable (*c in this case) will capture an empty list if there are no remaining elements.*a, b = 1, 2, 3
This line assigns the last element of the sequence 3 to b and the preceding elements 1 and 2 to a as a list. It's a pattern that is useful when you want to separate the last element from the rest of a sequence.*a, = 1, 2, 3
same as(*a,) = 1, 2, 3
. This syntax unpacks all elements into a as a list. The parentheses and commas indicate that we are dealing with tuple unpacking, but since there's only one element in the tuple (the starred *a), it results in assigning all values as a list.[*a] = 1, 2, 3
Here, the list notation is used explicitly. The elements of the tuple are unpacked into the list a. It's another way to convert a tuple into a list through unpacking.*a = 1, 2, 3
This line attempts to use a starred expression on the left side of an assignment without it being part of a list or tuple, which is a syntax error in Python. Starred expressions in assignments need to be within a list or tuple to be valid. This line demonstrates a common mistake when trying to use starred expressions outside of a list or tuple context.
But the SyntaxError will occur if to use more than one unpacking operator.
¿Todo estuvo claro?