![]() |
VOOZH | about |
*args and **kwargs are used to allow functions to accept an arbitrary number of arguments. These features provide great flexibility when designing functions that need to handle a varying number of inputs.
The below code shows how *args collects multiple positional arguments into a tuple and how **kwargs collects keyword arguments into a dictionary.
30 a 1 b 2 c 3
Python provides two special symbols for passing variable numbers of arguments:
Note: Use *args or **kwargs when the number of arguments to be passed to a function is not known in advance.*
*args syntax allows a function to accept any number of positional arguments. All passed values are collected into a tuple, which can then be accessed or iterated inside the function. This is useful when the number of arguments is not known beforehand.
Hello Welcome to GeeksforGeeks
Explanation:
Here we use *args to multiply any number of values.
24
Explanation:
**kwargs syntax allows a function to accept any number of keyword arguments. All arguments are collected into a dictionary, where the argument names become keys and their corresponding values become dictionary values.
s1 = Python s2 = is s3 = Awesome
Explanation:
Here we use **kwargs to create a formatted string from the arguments.
Name: Alice, Age: 25, City: New York
Explanation:
We can also combine *args and **kwargs in the same function. This way, the function can accept both positional and keyword arguments at once.
Subjects: ('Math', 'Science', 'English')
Details: {'Name': 'Alice', 'Age': 20, 'City': 'New York'}
Explanation: