{"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":"
In this article we will discuss how we can pass multiple arguments\u00a0 to function using 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 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 We can give any number of arguments in function by prefixing function with symbol ‘ Along with Let’s see the representation of that.<\/p>\n In <\/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*args<\/code> in Python.<\/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
Defining a function that can accept variable length arguments :<\/h3>\n
*<\/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
*args<\/code> we can also add other parameters. But it should be make sure that
*args<\/code> should be after formal arguments.<\/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
*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