*args

*args and **kwargs in python

*args allows us to pass a variable number of arguments to a function . it uses asterisk( * ) to iterate number of arguments and then a variable name .There are various type of functional arguments in python including:

  1. Default Arguments
  2. Keyword arguments
  3. Positional Arguments
  4. Arbitrary Arguments

Why *args and **kwargs

Basically, we use *args and **kwargs when we have to pass variable number of arguments in function . In simple words we can say , it allows us to pass any number of arguments during function call . We also use them when we don’t know exact number of parameters which we need to pass . It supports dynamic time argument declaration / passing . args and kwargs are just only naming conventions , you can use any name instead .

For Example , if you want a function to add numbers. In that case it is depend upon user , how many number of arguments he need to pass . In that case we  can use *args or *kwargs .

*args

It is an type of Arbitrary arguments , it allows us to pass variable number of arguments .It is used for positional / non-keyword arguments . Here is the syntax of function with variable number of arguments :

def function_name( *args ):
    pass

Example 1 – Accessing *args

def fun(a, *args):
    print(a)
    for i in args:  #Iterating to arguments
        print("Arbitary Argument",i)

fun(10,"Hello",20,30,"There")

Output

args
args

Explanation

  • As you can see in the example above, here is a function named fun() .
  • This fun() accepts any number of arguments .
  • We use single asterisk(*) arguments for non-keyword arguments .
  • In example above , it takes first argument as an positional argument which refers to ‘a’ parameter .

Example 2 – Addition of variable number of arguments

def add(*args):
    sum=0
    for i in args:
        sum += i
    print("Sum=",sum)

add(10,80,50)

Output

addition of args
addition of args

**kwargs

We can use **kwargs for keyword arguments . Keyword arguments are those which comes in key and value pairs like an dictionary . We can access these arguments in the same way as access dictionary’s keys and values . Here is an example of accessing kwargs .

Syntax

def function_name(**kwargs):
    pass

Example

def fun(**kwargs):
    print(kwargs)
    for i in kwargs:
        print(i , kwargs[i])
fun(name="Aman",Course="Python",Id=1)

Output

kwargs
kwargs

Explanation

  • As shown in example above , **kwargs  are used for keyword arguments .
  • Keywords arguments are the values followed by key and equals to sign .
  • We can access these keys and values as same way as accessing dictionary items .

Example – Addition of variable number of keyword arguments

def fun(**kwargs):
    sum = 0
    for i in kwargs:
        sum += kwargs[i]
    print("Sum = ",sum)
fun(a=10,b=100,c=30,x=40)

Output

kwargs
kwargs

Download source code

Download project on Github

So it is all about *args and **kwargs , i hope you guys get it . Thanks!!!

27 thoughts on “*args

Leave a Reply

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

  1. I just like the helpful information you provide to your articles. I will bookmark your blog and check once more right here regularly. I am relatively certain I will be told plenty of new stuff right right here! Good luck for the following!

  2. This is very interesting, You’re a very skilled blogger. I have joined your rss feed and look forward to seeking more of your magnificent post. Also, I’ve shared your web site in my social networks!

  3. Hi there very nice blog!! Guy .. Excellent .. Wonderful .. I’ll bookmark your site and take the feeds also?KI’m glad to seek out a lot of useful info here in the submit, we want work out more strategies on this regard, thanks for sharing. . . . . .

  4. This is a fantastic article and also I rejoice that I discovered your web site. I have been searching for info on this subject for the last couple of days and was happily stunned to locate it on your website. I want to be back over the following couple of months to see if you have any type of other items of information that could aid me.

  5. I think this is among the most vital info for me. And i am glad reading your article. But should remark on few general things, The site style is wonderful, the articles is really nice : D. Good job, cheers

  6. I simply want to tell you that I am newbie to blogging and site-building and really savored you’re web page. Most likely I’m planning to bookmark your site . You surely have tremendous stories. Regards for revealing your blog.

  7. The subsequent time I learn a blog, I hope that it doesnt disappoint me as a lot as this one. I imply, I do know it was my option to read, however I actually thought youd have one thing fascinating to say. All I hear is a bunch of whining about one thing that you could fix when you werent too busy in search of attention.

  8. I feel this is one of the such a lot important information for me. And i am glad studying your article. However want to statement on few common things, The website style is ideal, the articles is actually excellent : D. Good task, cheers

  9. Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher! Come on over and visit my site . Thanks =)

  10. I’m not sure exactly why but this weblog is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later on and see if the problem still exists.

  11. Thanks for another excellent article. Where else could anyone get that kind of info in such an ideal way of writing? I have a presentation next week, and I am on the look for such information.

  12. Magnificent beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea

  13. What’s Taking place i’m new to this, I stumbled upon this I’ve found It positively helpful and it has helped me out loads. I hope to contribute & aid other customers like its helped me. Great job.

  14. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is magnificent blog. A great read. I will certainly be back.

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.