-1

Is there any difference between the variables b and *b in these two statements? If so, what is it?

(a, b, c) = 1, 2, 3

(a, *b, c) = 1, 2, 3

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
Damodara Sahu
  • 115
  • 1
  • 8

2 Answers2

1

Please check below:

>>> (a, *b, c) = 1, 2, 3
>>> a,b,c
(1, [2], 3)
>>> type(a)
<class 'int'>
>>> type(b)
<class 'list'>
>>> type(c)
<class 'int'>

You can clearly see that using * declares b as list.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
-2
(a, b, c) = 1, 2, 3
print(a, b, c)  # Output: 1 2 3

(a, *b, c) = 1, 2, 3
print(a, b, c)  # Output: 1 [2] 3
4b0
  • 21,981
  • 30
  • 95
  • 142
NITHISH T
  • 1
  • 1
  • 2
    Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Aug 24 '23 at 05:33