0

what does *c mean in python IE:a,b,*c,d

would someone please explain what does the * do when it is just in front of a object? d ={10: "x",1:"wx",2:"yz"} a = d.setdefault(1) b = d.setdefault(3) s = "{}"*len(d) print(s.format(*d))

14th Mar 2018, 8:04 AM
Deen Lu
Deen Lu - avatar
3 Réponses
+ 2
1. Multiplication: >>> 3*4 12 >>> "a"*4 'aaaa' 2. "Greedy" arguments collector: >>> (a, *b, c) = [1, 2, 3, 4, 5] >>> a 1 >>> b [2, 3, 4] >>> c 5 You see, first and last variables took first and last list members. *b - takes all that were not taken. As per https://stackoverflow.com/questions/400739/what-does-asterisk-mean-in-JUMP_LINK__&&__python__&&__JUMP_LINK: If the form *identifier is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form **identifier is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary. 3. Specifically when dealing with dictionaries, * takes a sequense of its keys: >>> x = {'a':1, 'b':2, 'c':3} >>> print(*x) a b c
14th Mar 2018, 1:38 PM
strawdog
strawdog - avatar
+ 2
d=[1,2,3,4] print(*d) * is unwrapping the list...and printing...
14th Mar 2018, 1:32 PM
sayan chandra
sayan chandra - avatar
+ 2
here its unwrapping dict
14th Mar 2018, 1:33 PM
sayan chandra
sayan chandra - avatar