{"id":5706,"date":"2021-05-16T10:43:59","date_gmt":"2021-05-16T05:13:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=5706"},"modified":"2021-11-22T18:42:36","modified_gmt":"2021-11-22T13:12:36","slug":"python-args-how-to-pass-multiple-arguments-to-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/","title":{"rendered":"Python : *args | How to pass multiple arguments to function ?"},"content":{"rendered":"

Passing multiple arguments to function in Python.<\/h2>\n

In this article we will discuss how we can pass multiple arguments\u00a0 to function using *args<\/code> in Python.<\/p>\n

Let’s say if we want to calculate average of four numbers by a function e.g avg(n1,n2,n3,n4) then we can calculate average of any four numbers\u00a0 by passing some numbers arguments.<\/p>\n

Like<\/p>\n

# Program :\r\n\r\ndef avg(n1,n2,n3,n4):\r\n    # function to calculate average of 4 numbers\r\n    return (n1+n2+n3+n4)\/4\r\naverage = avg(10,20,30,40)\r\nprint(average)<\/pre>\n
Output :\r\n25.0<\/pre>\n

But what if we want to calculate average of 10 numbers then we can’t take the above function. Here, in this article we shall define a function in python which can accepts any number of arguments. So, let’s start the topic to know how we can achieve this.<\/p>\n

Defining a function that can accept variable length arguments :<\/h3>\n

We can give any number of arguments in function by prefixing function with symbol ‘*<\/code>‘.<\/p>\n

# Program :\r\n\r\ndef calcAvg(*args):\r\n    '''Accepts variable length arguments and calculate average of n numbers'''\r\n    # to get the count of total arguments passed\r\n    argNums = len(args)\r\n    if argNums > 0 :\r\n        sum_Nums = 0\r\n        # to calculate average from arguments passed\r\n        for ele in args :\r\n            sum_Nums += ele\r\n        return sum_Nums \/ argNums\r\n        print(sum_Nums)\r\n    else:\r\n        return \r\nif __name__ == '__main__':\r\n    avg_Num = calcAvg(10,20,30,40,50)\r\n    print(\"Average is \" , avg_Num)\r\n<\/pre>\n
Output :\r\nAverage is 30.0<\/pre>\n

Important points about *args :<\/h3>\n

Positioning of parameter *args :<\/h4>\n

Along with *args<\/code> we can also add other parameters. But it should be make sure that *args<\/code> should be after formal arguments.<\/p>\n

Let’s see the representation of that.<\/p>\n

# Program :\r\n\r\ndef publishError(startPar, endPar, *args):\r\n# Accepts variable length arguments and publish error\r\n    print(startPar)\r\n    for el in args :\r\n        print(\"Error : \" , el)\r\n    print(endPar)    \r\npublishError(\"[Begin]\" , \"[Ends]\" , \"Unknown error\")\r\n<\/pre>\n
Output :\r\n[Begin]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\nError :\u00a0 Unknown error\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \r\n[Ends]<\/pre>\n

Variable length arguments can be of any type :<\/h3>\n

In *arg<\/code> we can not only pass variable number of arguments, but also it can be of any data type.<\/p>\n

# Programs :\r\n\r\ndef publishError(startPar, endPar, *args):\r\n# Accepts variable length arguments and publish error\r\n    print(startPar)\r\n    for el in args :\r\n        print(\"TypeError : \" , el)\r\n    print(endPar)    \r\npublishError(\"[Begin]\" , \"[Ends]\" , [10, 6.5, 8], ('Holla','Hello'), \"\")\r\n<\/pre>\n
Output :\r\n[Begin]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\nTypeError :\u00a0 [10, 6.5, 8]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\nTypeError :\u00a0 ('Holla', 'Hello')\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\nTypeError :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\n[Ends]<\/pre>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

Passing multiple arguments to function in Python. In this article we will discuss how we can pass multiple arguments\u00a0 to function using *args in Python. Let’s say if we want to calculate average of four numbers by a function e.g avg(n1,n2,n3,n4) then we can calculate average of any four numbers\u00a0 by passing some numbers arguments. …<\/p>\n

Python : *args | How to pass multiple arguments to function ?<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : *args | How to pass multiple arguments to function ? - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : *args | How to pass multiple arguments to function ? - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Passing multiple arguments to function in Python. In this article we will discuss how we can pass multiple arguments\u00a0 to function using *args in Python. Let’s say if we want to calculate average of four numbers by a function e.g avg(n1,n2,n3,n4) then we can calculate average of any four numbers\u00a0 by passing some numbers arguments. … Python : *args | How to pass multiple arguments to function ? Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-16T05:13:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:12:36+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/\",\"name\":\"Python : *args | How to pass multiple arguments to function ? - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-05-16T05:13:59+00:00\",\"dateModified\":\"2021-11-22T13:12:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : *args | How to pass multiple arguments to function ?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python : *args | How to pass multiple arguments to function ?\",\"datePublished\":\"2021-05-16T05:13:59+00:00\",\"dateModified\":\"2021-11-22T13:12:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#webpage\"},\"wordCount\":202,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : *args | How to pass multiple arguments to function ? - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/","og_locale":"en_US","og_type":"article","og_title":"Python : *args | How to pass multiple arguments to function ? - Python Programs","og_description":"Passing multiple arguments to function in Python. In this article we will discuss how we can pass multiple arguments\u00a0 to function using *args in Python. Let’s say if we want to calculate average of four numbers by a function e.g avg(n1,n2,n3,n4) then we can calculate average of any four numbers\u00a0 by passing some numbers arguments. … Python : *args | How to pass multiple arguments to function ? Read More »","og_url":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-05-16T05:13:59+00:00","article_modified_time":"2021-11-22T13:12:36+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#webpage","url":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/","name":"Python : *args | How to pass multiple arguments to function ? - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-05-16T05:13:59+00:00","dateModified":"2021-11-22T13:12:36+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : *args | How to pass multiple arguments to function ?"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python : *args | How to pass multiple arguments to function ?","datePublished":"2021-05-16T05:13:59+00:00","dateModified":"2021-11-22T13:12:36+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#webpage"},"wordCount":202,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-args-how-to-pass-multiple-arguments-to-function\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/5706"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=5706"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/5706\/revisions"}],"predecessor-version":[{"id":5720,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/5706\/revisions\/5720"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=5706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=5706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=5706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}