python

Arbitrary Arguments *args in python

def favourite_fruit(*args):
    print("My favourite fruit is : " + args[1])

favourite_fruit("Apple", "Mango", "Banana")
Output
My favourite fruit is Mango

If you do not know how many parameters will be passed to a function then you can use arbitrary arguments in Python. In the code snippet, we have passed three fruits names and want to print the second argument passed as favorite fruit.

Here we have passed these arguments as a tuple so you can access them using its index inside the function code block.

Was this helpful?