Great question! In Python, *args and **kwargs are used in function definitions to allow
variable numbers of arguments.
What are *args?
*args lets a function accept any number of positional arguments.
Inside the function, args is a tuple of all positional arguments passed beyond the defined ones.
Example:
def greet(*args):
for name in args:
print(f"Hello, {name}!")
greet("Alice", "Bob", "Charlie")
# Output:
# Hello, Alice!
# Hello, Bob!
# Hello, Charlie!
What are **kwargs?
**kwargs lets a function accept any number of keyword arguments (named arguments).
Inside the function, kwargs is a dictionary of all keyword arguments passed.
Example:
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Alice", age=30, city="NY")
# Output:
# name: Alice
# age: 30
# city: NY
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Great question! In Python,
*argsand**kwargsare used in function definitions to allow variable numbers of arguments.What are
*args?*argslets a function accept any number of positional arguments.argsis a tuple of all positional arguments passed beyond the defined ones.Example:
What are
**kwargs?**kwargslets a function accept any number of keyword arguments (named arguments).kwargsis a dictionary of all keyword arguments passed.Example:
Using both together:
Summary
*args**kwargs