Here, we will write a Python program in which we will pass multiple arguments to the function.
Submitted by Shivang Yadav, on March 22, 2021
Functions in Python are very important as they reduce the number of lines of code.
Python allows its users to pass more than one argument to the function.
This is done using * notation,
Syntax:
def function_name [* arguments]
Program for passing multiple arguments to a function
# Python program for passing # multiple arguments to a function # python program to find sum of any number # of arguments passed to it def findSum[*data]: sumVal = 0 for item in data: sumVal += item print["Sum of all arguments is ",sumVal] # Calling function with variables... findSum[] findSum[12] findSum[12,4] findSum[12,4,6] findSum[1,2,3,4,5,6,7,8] findSum[12,45,67,78,90,56]
Output:
Sum of all arguments is 0 Sum of all arguments is 12 Sum of all arguments is 16 Sum of all arguments is 22 Sum of all arguments is 36 Sum of all arguments is 348
Python Basic Programs »
00:00 Hi, I’m Rich Bibby with realpython.com. Sometimes when you look at a function definition in Python you might see that it takes two strange arguments, *args
and **kwargs
.
00:10 If you’ve ever wondered what these peculiar variables are or why your IDE defines them in main[]
, then this tutorial is for you. You’ll learn how to use args
and kwargs
in Python to add more flexibility to your functions. In this tutorial, you’re going to learn what *args
and **kwargs
actually are and how they allow you to pass multiple arguments or keyword arguments to a function.
00:33 Then you’ll learn how to use the *args
variable in function definitions, and then also how to use the **kwargs
variable in function definitions.
00:42 Then, once you’ve learned to write functions that use args
and kwargs
, you’ll then learn about why the ordering of arguments becomes important.
00:50 Next, you’ll learn how to use single asterisks [**kwargs
1] to unpack iterables and also how to use double asterisks [**kwargs
2] to unpack dictionaries. And finally, we’ll wrap things up in the conclusion.
01:00 This tutorial assumes that you already know how to define Python functions and work with lists and dictionaries. If you don’t or if you need a refresher, then check out some of the other great tutorials here on realpython.com that cover these topics. So, let’s get started.
01:16 *args
and **kwargs
allow you to pass multiple arguments, in the case of *args
, or keyword arguments, in the case of **kwargs
, to a function. Consider this example.
01:25 This is a simple function that takes two arguments, **kwargs
7 and **kwargs
8, and returns their sum. This function works fine, but it’s limited to only two arguments. What if you need to sum a varying number of arguments, where the specific number of arguments passed is only determined at runtime?
01:43 Wouldn’t it be great to create a function that could sum all the integers passed to it, no matter how many there are? Of course it would. So keep watching, and I’ll show you how you can do just that.
It is possible to declare functions which receive a variable number of arguments, using the following syntax:
def foo[first, second, third, *therest]:
print["First: %s" % first]
print["Second: %s" % second]
print["Third: %s" % third]
print["And all the rest... %s" % list[therest]]
The "therest" variable is a list of variables, which receives all arguments which were given to the "foo" function after the first 3 arguments. So calling foo[1, 2, 3, 4, 5]
will print out:
def foo[first, second, third, *therest]:
print["First: %s" %[first]]
print["Second: %s" %[second]]
print["Third: %s" %[third]]
print["And all the rest... %s" %[list[therest]]]
foo[1, 2, 3, 4, 5]
It is also possible to send functions arguments by keyword, so that the order of the argument does not matter, using the following syntax. The following code yields the following output: The sum is: 6 Result: 1
def bar[first, second, third, **options]:
if options.get["action"] == "sum":
print["The sum is: %d" %[first + second + third]]
if options.get["number"] == "first":
return first
result = bar[1, 2, 3, action = "sum", number = "first"]
print["Result: %d" %[result]]
The "bar" function receives 3 arguments. If an additional "action" argument is received, and it instructs on summing up the numbers, then the sum is printed out. Alternatively, the function also knows it must return the first argument, if the value of the "number" parameter, passed into the function, is equal to "first".
Exercise
Fill in the foo
and bar
functions so they can receive a variable amount of arguments [3 or more] The foo
function must return the amount of extra arguments received. The bar
must return
def foo[first, second, third, *therest]:
print["First: %s" % first]
print["Second: %s" % second]
print["Third: %s" % third]
print["And all the rest... %s" % list[therest]]
0 if the argument with the keyword def foo[first, second, third, *therest]:
print["First: %s" % first]
print["Second: %s" % second]
print["Third: %s" % third]
print["And all the rest... %s" % list[therest]]
1 is worth 7, and def foo[first, second, third, *therest]:
print["First: %s" % first]
print["Second: %s" % second]
print["Third: %s" % third]
print["And all the rest... %s" % list[therest]]
2 otherwise.