Double star/asterisk (**) vs Star/Asterisk (*) in Python

While in python fuctionional programming we often come across (double star/asterisk) and * (star/asterisk). Most of the beginners find this extremely confusing. In this Python tutorial article we will discuss What does this mean and What does (double star/asterisk) and * (star/asterisk) do for parameters in Python?

We all know that * (star/asterisk) and (double star/asterisk) is used for multiplication and exponentiation respectively. But when passed as parameters they both have different significance. Let’s look them one by one but before that lets learn about Positional arguments and Keyword arguments and then we will discuss What does (double star/asterisk) and * (star/asterisk) do for parameters in Python?

Table Of Contents

Positional argument vs Keyword argument

Keyword arguments are the arguments which are separated by the ‘=’ or equal to sign and can be termed as keyword=value whereas Positional arguments are separated by commas and it needs to be in an order.

* (star/asterisk)

When * (star/asterisk) is passed as a parameter it signifies number of input/parameter shall vary. Which means it is totally upon the user, how many values he/she will pass as an argument. This is often denoted with *args. This is used for Non-Keyword positional arguments. All the inputs as converted into a single tuple. Python converts it in an iterable which means this can be used with other functions like map and filter.

See the Below example code:

CODE :

# function that returns the sum of given arguments.
def expenseCalculator(*expense):
    # type() will print the data type of arguments passed
    # with * (star/asterisk) which happens to be of class tuple.
    print(type(expense))
    # sum adds the numbers provided as parameters.
    return sum(expense)

# execution of function with 3 arguments.
print('Total expense for last month was',expenseCalculator(5000,3000,4300))

OUTPUT :

<class 'tuple'>
Total expense for last month was 12300

In the above example code and output, you can see we have an expense calculator simple function which takes positional arguments which are the monthly expenses of the month and adds it with the help of sum() function and returns it to the user. Here *(star/asterisk) has been passed with expense as a prefix, which denotes number of arguments is not fixed.

**(double star/asterisk)

When (double star/asterisk) is passed along with arguments in python function it denotes, we can pass variable number of Keyword arguments. When arguments are passed with (double star/asterisk) as prefix then all the arguments are converted into dictionary or in Key: value pair. This is often denoted by **kwargs which means Keyword arguments. This is also used when the input is not pre-defined and is totally upon the user to pass the number of arguments.

Now see an example code below

CODE :

# function that returns detailed expenses and take Keyword arguments. 
def expense(**expense):
    # type() will print the data type of arguments 
    # provided with **(double star/asterisk) which happens to be of class 
    print(type(expense))
    for items,amount in expense.items():
        print('Expense for {} is {}'.format(items, amount))


expense(rent=5000,grocery=2000,other=2000)

OUTPUT :

<class 'dict'>
Expense for rent is 5000
Expense for grocery is 2000
Expense for other is 2000

So, In the above code and output you can see we have a simple function which gets some Keyword Arguments with ** (double star/asterisk) as prefix and returns a string with the monthly expenses, You can also see the Keyword arguments which are provided are now of the class dict(in key-value pair).

SUMMARY

So In this Python tutorial article of What does (double star/asterisk) and (star/asterisk) do for parameters in Python? we learned about Positional Arguments and Keyword Arguments also we learned about (star/asterisk) and (double star/asterisk).
In short these are passed along with arguments as pre-fix when the number of arguments/values which has to be passed in the function is not fixed. (star/asterisk) is used with Positional Arguments and *(double star/asterisk) is used with Keyword Arguments.

Make sure to read and write example codes in order to have a better understanding of this problem.

Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top