Python Forum
Using function *args to multiply multiple arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using function *args to multiply multiple arguments
#1
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?
Reply
#2
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.
Reply
#3
Here is a printing example with the *args argument
>>> L = [2, 5, 6]
>>> print(*L)
2 5 6
Reply
#4
(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.
Reply
#5
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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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.
Reply
#8
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
Question How to compare two parameters in a function that has *args? Milan 4 1,182 Mar-26-2023, 07:43 PM
Last Post: Milan
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  'namespace' shorthand for function arguments? shadowphile 5 2,541 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  *args implementation and clarification about tuple status amjass12 10 3,913 Jul-07-2021, 10:29 AM
Last Post: amjass12
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Function - Return multiple values tester_V 10 4,316 Jun-02-2021, 05:34 AM
Last Post: tester_V
  [SOLVED] Good way to handle input args? Winfried 2 2,003 May-18-2021, 07:33 PM
Last Post: Winfried
  Possible to dynamically pass arguments to a function? grimm1111 2 2,123 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Two Questions, *args and //= beginner721 8 3,421 Feb-01-2021, 09:11 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020