Use of asterisk in variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

7th Jun 2018, 5:31 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
3 Answers
+ 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.
9th Jun 2018, 4:57 AM
David Ashton
David Ashton - avatar
+ 2
David Ashton thanks! is now more clear for me :)
11th Jun 2018, 3:42 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
Its a pointer. This was covered in the C course.
8th Jun 2018, 12:04 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar