DEV Community

Cover image for Back To Python Basics: *args & **Kwargs
Bek Brace
Bek Brace

Posted on • Updated on

Back To Python Basics: *args & **Kwargs

Understanding *args and **kwargs in Python
By @bekbrace

Hey there, Python enthusiasts! In this post, we'll break down two fascinating features: *args and **kwargs. Whether you're a beginner looking to understand the basics or an experienced developer needing a refresher, this guide is for you. I’ve also included a video where I explain everything in more detail.

Watch the Video!
For a more detailed explanation, watch my video where I dive into code samples and real-world scenarios to help you get comfortable with these Python (awesome as I like always to say!) features.

Why *args and **kwargs Matter

Python's flexibility in handling function arguments is one of its many strengths. *args and **kwargs allow you to write functions that can accept a variable number of arguments, making them more adaptable and reusable.

What are *args?

*args lets you pass multiple positional arguments into a function. These arguments are then stored as a tuple that can be iterated through.

def greet(*names):
    for name in names:
        print(f"Hello, {name}!")

greet("Alice", "Bob", "Charlie")
Enter fullscreen mode Exit fullscreen mode


Hello, Alice!
Hello, Bob!
Hello, Charlie!
Enter fullscreen mode Exit fullscreen mode

How *args and **kwargs Work Together

You can combine both in a single function for maximum flexibility. The order should always be *args, then **kwargs.

def profile(*skills, **details):
    for skill in skills:
        print(f" - {skill}")

    for key, value in details.items():
        print(f"{key}: {value}")

profile("Python", "Django", name="Amir", experience="5 years")
Enter fullscreen mode Exit fullscreen mode


 - Python
 - Django

name: Amir
experience: 5 years
Enter fullscreen mode Exit fullscreen mode

When to Use Them

*args: When a function needs to accept an undetermined number of positional arguments.
**kwargs: When a function requires flexibility in the names and quantity of keyword arguments.
Both: When a function could benefit from flexibility with both positional and named arguments.

Python's *args and **kwargs are indispensable tools that can make your functions more dynamic and versatile. Experiment with them and see how they can simplify your coding life.
Got questions? Leave them in the comments :)


Top comments (7)

abetasticx profile image
Albert Rasinger • Edited

The first result is a wrong copy pasta :)

Other than that its a quick shrot explanation and usefull for beginners

bekbrace profile image
Bek Brace • Edited

where ?

abetasticx profile image
Albert Rasinger

The first result in your post?

Thread Thread
Sloan, the sloth mascot
Comment deleted
abetasticx profile image
Albert Rasinger

Sorry but the result of the code you have in the code below is wrong!

Image description

Thanks but i am not a beginner at all and this is just confusing beginners that really have no idea...

Thread Thread
bekbrace profile image
Bek Brace • Edited

you're right, an honest mistake - thanks man.
I deleted my comment, cause it's a bit stupid from my side :D
thanks again !

ashc0102 profile image
Anchor Chau

TIL :)