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?
Similar Posts
- Python Measure the execution time of small bits of Python code with the timeit module
- [Python] Use json.dumps() to pretty-print Python dicts
- [Python] The get() method on Python dicts and its "default" arg
- [Python] Different ways to test multiple flags at once in Python
- Python collection - List in python
- Python collection - Tuples in python
- Python collection - sets in python