New course! Every coder should learn Generative AI!
Try a free lesson+ 3
It's a way to separate the different values of a tuple into different variables.
For example:
a, b, c = (1, 2, 3)
After running this code, you'll have three variables, each with one of the values of the tuple (in order: the first variable will hold the first item of the tuple and so). In essence, it automatizes doing:
tup = (1,2,3)
a = tup[0]
b = tup[1]
c = tup[2]
Of course, it would raise an error if there are more values than variables. However you can bypass this by using * before a variable. * means "save here all the extra values than don't fit with this number of variables".
if we have:
tup = (1,2,3,4)
a, b, c = tup.
This would raise an error, because we have more values in the tuple than variables. However, if we use * in c:
a, b, *c = tup
then we'd have
a = 1
b = 2 and
c = [3,4] (yes, it's a list, not a tuple, at least in python 3.6.5).
You can use * in whatever variable you want, but the values will be assigned in order.
a, *b, c = tup
a = 1
b= [2,3]
c = 4
Hope it's helpful!