What are *args and **kwargs in Python Programming?

Reza Kalantar
3 min readMay 7, 2023

--

Image generated by Reza Kalantar

Introduction

In Python, functions play a significant role in building efficient and reusable code. Sometimes, a function may require a flexible number of arguments, which can be difficult to handle with traditional fixed parameters. This is where the magic of *args and **kwargs comes into play. These powerful Python tools help developers create more adaptable and dynamic functions, making code more robust and versatile.

In this article, we will explore *args and **kwargs, understand their use cases, and learn how to apply them effectively in Python functions.

  1. Understanding *args:

args is a shorthand notation for “arguments.” It allows you to pass a variable number of non-keyword (positional) arguments to a function. The asterisk () before the parameter name is used to indicate that the function can accept any number of positional arguments. These arguments are then bundled into a tuple within the function.

Example:

def sum_numbers(*args):
result = 0
for num in args:
result += num
return result

print(sum_numbers(1, 2, 3, 4)) # Output: 10
print(sum_numbers(2.5, -2, 7, 10, 0)) # Output: 16.5

2. Utilizing **kwargs:

kwargs stands for “keyword arguments.” It enables you to pass a variable number of keyword arguments to a function. The double asterisks () before the parameter name signify that the function can accept any number of keyword arguments. These arguments are then packed into a dictionary within the function.

Example:

def print_user_data(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

print_user_data(name="John", age=30, city="London")

Output:

name: John
age: 30
city: London

3. Combining *args and **kwargs:

You can use both *args and **kwargs in the same function to accept any combination of positional and keyword arguments. Just ensure that *args precedes **kwargs in the function definition.

Example:

def combined_args(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key}: {value}")

combined_args(1, 2, 3, name="Jen", age=25)

Output:

1
2
3
name: Jen
age: 25

Key Takeaways:

  • Use *args to accept a variable number of non-keyword (positional) arguments.
  • Use **kwargs to accept a variable number of keyword arguments.
  • Combine *args and **kwargs to handle any combination of positional and keyword arguments.

By mastering *args and **kwargs in Python, you can create more flexible functions that accept a variable number of arguments. This will lead to more adaptable and maintainable code, ultimately making you a more efficient Python programmer. Start incorporating *args and **kwargs in your projects and experience the benefits of this powerful feature.

Thank you for reading! If you find my blogs interesting or would like to get in touch, reach out on here, Github or LinkedIn.

Read more:

--

--

Reza Kalantar

Medical AI Researcher by Profession • Scientist/Engineer by Trade • Investor by Instinct • Explorer by Nature • Procrastinator by Choice