+ 20

[SOLVED] What is the use of '*' in python

Hi! What is the use of '*' in python. Well! I m not talking about the multiplication or wildcard operator. I have seen this was used many time with python lists For example ls = [3,5,3,5,35] print(*ls) #will produce the list without , and [] Also I have heard this was used for unpacking list. But what are the real used of it. And can u explain each example - including the things i have mentioned above

19th Feb 2019, 2:40 PM
Seniru
Seniru - avatar
14 Respostas
+ 51
Let's say your function takes two int arguments: def f(x, y): ... Now you have a tuple t = (5, 7) If you pass it just like this, you'll get an error - not enough args. So instead you write: f(*t) You tuple is 'unpacked' and becomes two values for the call. You can also do this for dicts: d = {'x': 1, 'y': 3} f(**d) In this case, the keys of the dict must match the parameter names. Also you can use this in parameters: def f(*t, **d): ... Here you can pass as many args as you want; unnamed ones land in tuple t, named ones in dict d. And in assignments: a, *b = (1, 2, 3) b will become a list (!) with 2 and 3 (because a takes 1 and b gets what's left). a, *b = (5,) Here b would become an empty list, because that's what's left - nothing.
19th Feb 2019, 2:49 PM
HonFu
HonFu - avatar
+ 17
Another interesting feature of * is to force the usage of keyword parameters when calling a function: def foo(a, b, *, c=4, d=2): return 42 foo(1, 2, 3, 4) This will result in an error foo(1, 2, c=3, d=4) This returns 42 with no issues every parameter to the right of the * must be used with its corresponding keyword source: Real Python newsletter https://www.getdrip.com/deliveries/ksz5js8gfrvfnrstby5n?
20th Feb 2019, 9:44 PM
Burey
Burey - avatar
+ 7
Oh HonFu got it thanks
19th Feb 2019, 2:50 PM
Seniru
Seniru - avatar
+ 3
No, it has no relation to pointers.
20th Feb 2019, 1:27 PM
HonFu
HonFu - avatar
+ 3
great answer HonFu. I have used A single * in defining my printf for python, but not the ** yet. Printf.py https://code.sololearn.com/cH7fs55oWP8G/?ref=app
20th Feb 2019, 8:35 PM
Rick Shiffman
Rick Shiffman - avatar
+ 2
Is there a similarity to how * is used here to 'pointers' in C++?
20th Feb 2019, 1:21 PM
Clement
Clement - avatar
+ 2
It works like an ES6 spread and rest operator.
21st Feb 2019, 4:35 AM
👑 Prometheus 🇾🇬
👑 Prometheus 🇾🇬 - avatar
+ 1
" * " is used to multiply the values in python and also in all programming languages.
21st Feb 2019, 1:13 PM
Aravinda Krishnan
Aravinda Krishnan - avatar
0
Basically * represents variable no of positional argument and ** represents variable no of key word argument Ex def fun(*args): for i in args: print(i) for j in kwargs: print(j,kwargs[I]) The function fun can be passed with any number of positional and keyword argument Ex. fun(1,2,3,4) fun(1,2,3,4,5,6,7,8) Ex fun(a = 1,b = 2,c = 3) fun(a = 1,b = 2)
20th Feb 2019, 5:52 PM
sheethal gowda
sheethal gowda - avatar
0
Multiplication
20th Feb 2019, 6:40 PM
Anurag
0
Whew, fancy! :)
20th Feb 2019, 10:26 PM
HonFu
HonFu - avatar
0
Let's say your function takes two int arguments: def f(x, y): ... Now you have a tuple t = (5, 7) If you pass it just like this, you'll get an error - not enough args. So instead you write: f(*t) You tuple is 'unpacked' and becomes two values for the call. You can also do this for dicts: d = {'x': 1, 'y': 3} f(**d) In this case, the keys of the dict must match the parameter names. Also you can use this in parameters: def f(*t, **d): ... Here you can pass as many args as you want; unnamed ones land in tuple t, named ones in dict d. And in assignments: a, *b = (1, 2, 3) b will become a list (!) with 2 and 3 (because a takes 1 and b gets what's left). a, *b = (5,) Here b would become an empty list, because that's what's left - nothing.
21st Feb 2019, 4:34 AM
Huzaifa usman
Huzaifa usman - avatar
0
it can mean multiply as shown below: 3*5 15
21st Feb 2019, 9:48 AM
Alexander Wood
Alexander Wood - avatar
- 1
The multiplaction function would be the most basic answer
20th Feb 2019, 5:42 PM
Timothy Makori
Timothy Makori - avatar