Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to use variable-length arguments in a function in Python?

When I am using the following code

def multiply(*args):
    y = 1   
    for num in args:
        y *= num
    print(y)
multiply(3, 7)
multiply(9, 8)
multiply(3, 4, 7)
multiply(5, 6, 10, 8)

I am able to use variable-length arguments in a python function? How is it possible?


1 Answer
Rajendra Dharmkar

In Python, the single-asterisk form of *args can be used as a parameter to send a non-keyworded variable-length argument list to functions. It is seen that the asterisk (*) is important here, and along with the word args it means there is a variable length list of non-keyworded arguments.

def multiply(*args):
    y = 1   
    for num in args:
        y *= num
    print(y)
multiply(3, 7)
multiply(9, 8)
multiply(3, 4, 7)
multiply(5, 6, 10, 8)

OUTPUT

21
72
84
2400
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.