* mark in print function call [duplicate] - python

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 6 years ago.
What is this star mark in this print function call?
for i in range(int(input())):
s=input()
print(*["".join(s[::2]),"".join(s[1::2])])

It is called argument unpacking. If you were to omit it, then it would only give the list created by the list comprehension to the print function as one argument. With the asterisk it passes every item in that list as separate argument.
Consider this example:
def my_func(arg1, arg2, arg3):
print('yay it worked')
and then call it with:
my_func(*[1, 2, 3])
that way arg1 will be 1, arg2 will be 2 and arg3 will be 3.
If you change the call to:
my_func([1, 2, 3])
then you pass the list to arg1 and it will raise a TypeError because it's missing two positional arguments.

Related

How to writing a function in Python to call another function while only assigning value to some arguments? [duplicate]

This question already has answers here:
Python Argument Binders
(7 answers)
Closed 3 years ago.
Or equivalently, to pass multiple arguments to a called function?
So I am trying to do:
define a function fun1
define another function fun2, which will call fun1 but only assigning some values to fun1, with other values being assigned otherwise; or maybe I can assign multiple arguments to a called function?
def fun1(x, y):
return x*y
def fun2(fun, x):
print(fun(x))
fun2(fun1(y=2), 3) # doesn't work
fun2(fun1, (3, 2)) # doesn't work either
So how should I do this? I want fun2 to just print fun1's result with some variable for x (here I choose it to be 3) and assigned(fixed) y=2
As suggested by #kaya3
Use python's variable *args feature.
IDLE :
>>> def fun1(x, y):
return x * y
>>> def fun2(fun, x):
print(fun(*x))
>>> fun2(fun1, (3, 2))
6

What does * represent in function argument list in python? [duplicate]

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
understanding '*' "keyword only" argument notation in python3 functions [duplicate]
(2 answers)
Closed 3 years ago.
While going through the source code, I noticed the following syntax being used in the asyncio library:
#coroutine
def sleep(delay, result=None, *, loop=None):
"""Coroutine that completes after a given time (in seconds)."""
if delay == 0:
yield
return result
if loop is None:
loop = events.get_event_loop()
future = loop.create_future()
h = future._loop.call_later(delay,
futures._set_result_unless_cancelled,
future, result)
try:
return (yield from future)
finally:
h.cancel()
what does the * do in the argument list?
It means that parameter(s) that comes after * are keyword only parameters.
Consider the following:
def test(delay, result=None, *, loop=None):
print(delay, result, loop)
In this case, test(1,2,2) will raise TypeError since it is expecting at most two positional arguments, i.e. delay and result:
test(1,2,2)
TypeError: test() takes from 1 to 2 positional arguments but 3 were given
The third argument, or loop, can only be assigned if used as keyword:
test(1,2,loop=2)
# 1 2 2
# Works fine
For more detail, refer to Function Definitions

*args vs list as a arguments in Python [duplicate]

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 3 years ago.
I just want to understand why we use *args when the same work can be done by list when we pass it as a argument. In which scenarios do we really need *args, where list as a argument will fail to do that operation.
list =[1,2,3,4,5,6,7]
def method1(args):
for i in args:
print(i*2)
method1(list)
list =[1,2,3,4,5,6,7]
def method1(*args):
for i in args:
print(i*2)
method1(*list)
I didn't find any differences. Please correct me if i am wrong.
def method1(args):
print(args)
method1(5) it will print 5
method1() method1() missing 1 required positional argument: 'args'
method1(2,6) TypeError: method1() takes 1 positional argument but 2 were given
To Avoid this situation we use
def method1(*args):
print(args)
method1(1, 2, '3')
(1, 2, '3') print this
So *args is useful when we don’t know in advance how many arguments we need to pass in.
The difference is that you can pass any number of arguments in the second case where it will throw error in the first case.
Case 1:
lst = [1,2,3,4,5,6,7]
a = 1
b = 2
def method1(args):
for i in args:
print(i*2)
method1(lst, a, b)
...fails with 'TypeError: method1() takes 1 positional argument but 3 were given'.
Case 2 (i):
lst = [1,2,3,4,5,6,7]
a = 1
def method1(*args):
for i in args:
print(i*2)
method1(lst, a)
...works.
Case 2 (ii):
lst = [1,2,3,4,5,6,7]
a = 1
b = 2
def method1(*args):
for i in args:
print(i*2)
method1(lst, a, b)
...works and so on, you can pass any number of arguments.

python function that iterates over list or, in case of int, just takes int [duplicate]

This question already has answers here:
pythonic way to convert variable to list
(8 answers)
Closed 5 years ago.
Is there a more elegant way? I want to let python either iterate over arg, if it is a list, or just take arg, if it is an int. data is a numpy.ndarray
def plot(data, run, t):
if isinstance(run, int):
run = [run]
fig = plt.figure()
ax = fig.add_subplot(111)
for run in run:
plt.plot(t, data[run])
plt.savefig('data.png', dpi=200)
You can reduce a line in your code:
def fcn(arg):
for a in ([arg] if isinstance(arg, int) else arg):
print a
def example(*arg):
print "I was called with", len(arg), "arguments:", arg
>>> example(1)
I was called with 1 arguments: (1,)
>>> example(1, 2,3)
I was called with 3 arguments: (1, 2, 3)

Python&Django: What does ** mean [duplicate]

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 6 years ago.
Possibly i know the following uses of **
Power
x ** y # x power y equivalent to pow(x,y)
Passing indefinite number of args
def sample(x, **other):
print(x, other.keys)
sample(x=2,y=3,z=4)
But i don't understand when its used as follow( in Serializers)
def create(self, validated_data):
return Comment(**validated_data)
Can someone give me a hint of what is happening there
It is the opposite of your second example. When in the definition of a function, the ** operator gathers all the named arguments and makes a dictionary. When calling a function, it takes a dictionary and breaks it into named arguments
So, if you have
values = {'x': 1, 'y': 2}
f(**values)
it is the equivalent of
f(x=1, y=2)

Categories