*args and **kwargs in python

*args and **kwargs in python

·

3 min read

Table of contents

In this article, we would discover what the single asterisk(*) and double asterisks (**) mean in Python.

*args - Multiple arguments

**kwargs - Multiple keyword arguments

Before going into our main topic for today, let's talk about functions.

FUNCTIONS

In programming, a function is a block of code written or designed to perform a specific task.

Functions are useful in a way that allows programmers and developers to continuously re-use a set of code/instructions.

Python Functions

Below is a simple Python function that adds two numbers together

def add_two_numbers(a,b):
    answer = a+b
    return answer

The above Python function is designed to accept just two arguments and add them together.

Well, what if we wanted to write a function that accepts multiple arguments and returns their sum? How do we do it?

This is where the single asterisk(*) comes in.

SINGLE ASTERISK (\args*)

In other to define the function to take in multiple arguments, we add the single asterisk(*) directly infront of the argument name. This tells python that it can take as much argument as possible.

The special syntax \args* in function definitions in Python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.

Below is a simple Python function that returns the sum of any numbers passed into it

def add_multiple_numbers(*num):
    value = 0
    for i in num:
        value = value +i
    return value

print(add_multiple_numbers(1, 2, 3, 4, 5, 6))

The above code returns 21.

The arguments are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk *.

DOUBLE ASTERISK (\*kwargs*)

This works just like the single asterisk (*) except that it is used to pass a keyworded, variable-length argument list.

Keywords are words that have a predefined meaning in a programming language. Keywords are reserved words that developers or programmers cannot use as the name of say a variable, constants, and functions in most programming languages.

Examples of keywords in python are str, def, list, dict, print and so on......

One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it.

Below is a sample example of using \*kwargs* to pass variable keyword arguments to a function.

def intro(**keyword):
    print("\nData type of argument:",type(keyword))

    for key, value in keyword.items():
        print("{} is {}".format(key,value))

intro(Firstname="User1", Lastname="User1Lastname", Age=20, Phone=1234567890)

The above will return 👇

In conclusion, \args* passes variable number of arguments to a function WHILE **kwargs passes variable number of keyword arguments to a function.

Did you enjoy the above article? Ensure to like, comment your thoughts and share to help others.

Made with ❤️ from rivondave