+ 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
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.
+ 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?
+ 7
Oh HonFu got it thanks
+ 3
No, it has no relation to pointers.
+ 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
+ 2
Is there a similarity to how * is used here to 'pointers' in C++?
+ 2
It works like an ES6 spread and rest operator.
+ 1
" * " is used to multiply the values in python and also in all programming languages.
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)
0
Multiplication
0
Whew, fancy! :)
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.
0
it can mean multiply as shown below:
3*5
15
- 1
The multiplaction function would be the most basic answer