Master Python Kwargs and Args with Clear Example Code

Logan Jul 13, 2026

Python, renowned for its simplicity and readability, offers a feature that allows functions to accept varying numbers and kinds of arguments. This flexibility is achieved through the use of arguments and keyword arguments, often abbreviated as args and kwargs, respectively. Let's dive into these concepts with practical examples.

ARGS & KWARGS in Python
ARGS & KWARGS in Python

In Python, a typical function definition might look like this:

an image of what are arg's and kwars in python?
an image of what are arg's and kwars in python?

<code>
def greet(greeting, name):
    print(f"{greeting}, {name}!")
</code>

Here, the function `greet` expects two positional arguments, `greeting` and `name`. But what if we want to add more parameters, or pass arguments in a different order? This is where args and kwargs come into play.

Understanding *args

an image with the words arg and kwargs in python
an image with the words arg and kwargs in python

Introducing dynamic arguments to your function can be achieved with the use of `*args`. The `*args` parameter allows you to pass a variable number of non-keyword arguments to your function.

Let's modify our `greet` function to use `*args`:

Python args, kwargs, and All Other Ways to Pass Arguments to Your Function | Towards Data Science
Python args, kwargs, and All Other Ways to Pass Arguments to Your Function | Towards Data Science

<code>
def greet(*args):
    for arg in args:
        print(f"Hello, {arg}!")
</code>

Now, you can call the `greet` function with any number of arguments:

<code>
greet("Alice", "Bob", "Charlie")
</code>

This will print:

<code>
Hello, Alice!
Hello, Bob!
Hello, Charlie!
</code>

Packing Arguments

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

You can also unpack an iterable (like a list or tuple) into `*args`:

<code>
names = ["Alice", "Bob", "Charlie"]
greet(*names)
</code>

This will yield the same result as the previous example.

Unpacking Arguments

a black background with text that reads a flexible greeting
a black background with text that reads a flexible greeting

Another useful aspect of `*args` is the ability to use it to unpack arguments. This means you can pass arguments from one function call to another:

<code>
def loud_greet(*args):
    for arg in args:
        print(f"HELLO, {arg.upper()}!")

greet("Alice", "Bob", "Charlie")
</code>

The `*args` in the `loud_greet` function can accept the arguments passed to `greet`.

a poster showing the different types of people
a poster showing the different types of people
Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial
the problem with fixed arguments is that you need to know how many functions are needed
the problem with fixed arguments is that you need to know how many functions are needed
a computer screen with the words user profile on it
a computer screen with the words user profile on it
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
PYTHON BASICS
PYTHON BASICS
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Variables Tutorial for Beginners πŸš€
Python Variables Tutorial for Beginners πŸš€
Python Functions Cheat Sheet
Python Functions Cheat Sheet
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
a blue background with the words in it
a blue background with the words in it
Python operators infographic with cartoon elements
Python operators infographic with cartoon elements
Different Types of Variables in Python with Examples
Different Types of Variables in Python with Examples
three different types of python programming
three different types of python programming
Python Learning Path for Beginners 2026 πŸš€
Python Learning Path for Beginners 2026 πŸš€
python
python
Day 26: Pandas in Python for Beginners πŸΌπŸ“Š
Day 26: Pandas in Python for Beginners πŸΌπŸ“Š
| Pythonista Planet
| Pythonista Planet
🐍 Python for Everything πŸš€ | Best Python Libraries & Tools Every Developer Should Learn
🐍 Python for Everything πŸš€ | Best Python Libraries & Tools Every Developer Should Learn

Keyword Arguments with **kwargs

`**kwargs`, short for keyword arguments, allows you to pass a variable number of keyword arguments to your function. These arguments are collected into a dictionary that can be accessed via the `kwargs` parameter.

Let's modify our `greet` function to use `**kwargs`:

<code>
def greet(**kwargs):
    for key, value in kwargs.items():
        print(f"Hello, {key-capitalize()} {value}!")
</code>

Now, you can call the `greet` function with any number of keyword arguments:

<code>
greet(first_name="Alice", middle_name="Bob", last_name="Charlie")
</code>

This will print:

<code>
Hello, Alice!
Hello, Bob!
Hello, Charlie!
</code>

Accessing kwargs

The beauty of `**kwargs` lies in its ability to be used as a dictionary. For example, you can access the value associated with a specific key:

<code>
def greet(**kwargs):
    first_name = kwargs.get("first_name", "No name provided")
    print(f"Hello, {first_name}!")
</code>

Required Arguments with kwargs

You can also use `**kwargs` to make certain arguments required. Here's how:

<code>
def greet(**kwargs):
    first_name = kwargs.get("first_name", None)
    if not first_name:
        raise TypeError("first_name is required")
    print(f"Hello, {first_name}!")
</code>

This function will raise an error if the `first_name` keyword argument is not provided.

Adequate comprehension and strategic application of `*args` and `**kwargs` enhance your coding abilities in Python, making your functions versatile and user-friendly.