Python Forum

Full Version: Using function *args to multiply multiple arguments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm a beginner in Python and have a question.
I now understand the basics of a function and what an asterisk does when it's used in a parameter.

However the code, from https://www.digitalocean.com/community/t...n-python-3, below confuses me because of for loop.
def multiply(*args):
    z = 1
    for num in args:
        z*= num
    print(z)
so, if I call the function w/ multiple arguments like
multiply(2, 5, 6)
the result is 60. because a for loop puts 2 , 5 & 6 in order as num through z* = num. which is 2*5*6* in my understanding.

However if I call a print function like
print(2*5*6*)
it gets a syntax error because of the last *.

What am I getting it wrong in this?
Your understanding of the for loop is incorrect. Once it reaches the end last index of the tuple created by *args, it terminates without running the loop body. In effect, the for loop would perform 2 * 5 * 6.
Here is a printing example with the *args argument
>>> L = [2, 5, 6]
>>> print(*L)
2 5 6
(Nov-19-2019, 04:16 PM)stullis Wrote: [ -> ]Once it reaches the end last index of the tuple created by *args, it terminates without running the loop body. In effect, the for loop would perform 2 * 5 * 6.
Would you please guide me with more examples or a link for supplementary readings to understand what you're saying?

My understanding was that the for loop begins then,
z = 1 and 2, 5 & 6 are num and
1* = 2
1* = 5
1* = 6
...wait, I'm even more confused. I don't think I understood it at all before.


(Nov-19-2019, 04:19 PM)Gribouillis Wrote: [ -> ]Here is a printing example with the *args argument
>>> L = [2, 5, 6]
>>> print(*L)
2 5 6

Oh, nice. you can unpack it as well. Thank you.
Your understanding of z *= num is wrong.
This is a shorter form of z = z * num.
which means using your example numbers:
z = 1 * 2 , so z == 2
z = 2 * 5 , so z == 10
z = 10 * 6 , so z = 60
It looks like the initial answers in fact created more confusion. Their initial understanding of the for loop and how it calculates the result were correct. And also was corect their understanding why the second example (the print) was failing. IMHO the reason for initial confusion of OP is the fact that asterisk is used both as multiplication operator and also to handle variable number of positional arguments

@allusernamestaken, when you use *args (here args is used by convention) it handles variable number of positional arguments. The same way if you use something like **kwargs, it will handle variable number of keyword arguments

also asterisk may be used similarly for extended iterable unpacking
(Nov-19-2019, 08:12 PM)ThomasL Wrote: [ -> ]This is a shorter form of z = z * num.
Oh okay, a shorter arithmetic formula. that's it. Thank you.

(Nov-19-2019, 08:40 PM)buran Wrote: [ -> ]Their initial understanding of the for loop and how it calculates the result were correct. And also was corect their understanding why the second example (the print) was failing
Thank you for clearing everything up for me.
(Nov-19-2019, 04:10 PM)allusernametaken Wrote: [ -> ]2*5*6* in my understanding.
sorry, I overlooked your initial post and especially the last asterisk here. Your previous sentence is the correct one:
(Nov-19-2019, 04:10 PM)allusernametaken Wrote: [ -> ]because a for loop puts 2 , 5 & 6 in order as num through z* = num
this is correct. look also at excellent @TomasL explanation.
(Nov-19-2019, 09:29 PM)buran Wrote: [ -> ]sorry, I overlooked your initial post and especially the last asterisk here.
You're fine. I appreciate your contribution.