0
Use of asterisk in variables
I have this code: a,b,*c,d = 1,2,3; print(c) output: [] why? what is the meaning of * in the variable c?
3 Respuestas
+ 7
In this case, the * is the "unpacking" (sometimes called the "splat") operator.
see https://codeyarns.com/2012/04/26/unpack-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
Here are some examples:
a, b, c, d = 1, 2, [3, 4, 5], 6
print(a, b, *c, d) # output: 1 2 3 4 5 6
a, b, c, d = 1, 2, [3, 4], 6
print(a, b, *c, d) # output: 1 2 3 4 6
a, b, c, d = 1, 2, [3], 6
print(a, b, *c, d) # output: 1 2 3 6
a, b, c, d = 1, 2, [], 6
print(a, b, *c, d) # output: 1 2 6
You can use the unpacking operator when you don't know how many elements, if any, there are for a particular positional argument:
def full_name(first_name, middle_names, last_name):
print("My full name is", first_name, *middle_names, last_name)
full_name("John", ["Paul", "George", "Ringo"], "Beatle")
# output: My full name is John Paul George Ringo Beatle
full_name("John", [], "Beatle")
# output: My full name is John Beatle
In these examples, * is used to unpack lists. It can be used to unpack tuples in exactly the same way.
+ 2
David Ashton thanks! is now more clear for me :)
0
Its a pointer. This was covered in the C course.