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

PythonServer Side ProgrammingProgramming

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.

Example

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
raja
Published on 07-Dec-2017 11:14:12
Advertisements