|
| 1 | + |
| 2 | + |
| 3 | +# Suppose, we define a function for addition of 3 numbers. |
| 4 | + |
| 5 | +#Example 1: Function to add 3 numbers |
| 6 | +def adder(x,y,z): |
| 7 | + print("sum:",x+y+z) |
| 8 | + |
| 9 | +adder(10,12,13) #sum: 35 |
| 10 | +# When we run the above program, the output will be |
| 11 | + |
| 12 | + |
| 13 | +# In above program we have adder() function with three arguments x, y and z. When we pass three values while calling adder() function, we get sum of the 3 numbers as the output. |
| 14 | + |
| 15 | +# Lets see what happens when we pass more than 3 arguments in the adder() function. |
| 16 | + |
| 17 | +def adder(x,y,z): |
| 18 | + print("sum:",x+y+z) |
| 19 | + |
| 20 | +adder(5,10,15,20,25) #TypeError: adder() takes 3 positional arguments but 5 were given |
| 21 | +# When we run the above program, the output will be |
| 22 | + |
| 23 | +# In the above program, we passed 5 arguments to the adder() function instead of 3 arguments due to which we got TypeError. |
| 24 | + |
| 25 | +# Introduction to *args and **kwargs in Python |
| 26 | +# In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols: |
| 27 | + |
| 28 | +''' |
| 29 | +*args (Non Keyword Arguments) |
| 30 | +**kwargs (Keyword Arguments) |
| 31 | +We use *args and **kwargs as an argument when we are unsure about the number of arguments to pass in the functions. |
| 32 | +
|
| 33 | +Python *args |
| 34 | +As in the above example we are not sure about the number of arguments that can be passed to a function. Python has *args which allow us to pass the variable number of non keyword arguments to function. |
| 35 | +
|
| 36 | +In the function, we should use an asterisk * before the parameter name to pass variable length arguments.The arguments are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk *. |
| 37 | +''' |
| 38 | + |
| 39 | +# Example 2: Using *args to pass the variable length arguments to the function |
| 40 | + |
| 41 | +def adder(*num): |
| 42 | + sum = 0 |
| 43 | + |
| 44 | + for n in num: |
| 45 | + sum = sum + n |
| 46 | + |
| 47 | + print("Sum:",sum) |
| 48 | + |
| 49 | +adder(3,5) #Sum: 8 |
| 50 | +adder(4,5,6,7) # Sum: 22 |
| 51 | +adder(1,2,3,5,6) # Sum: 17 |
| 52 | + |
| 53 | + |
| 54 | +# In the above program, we used *num as a parameter which allows us to pass variable length argument list to the adder() function. |
| 55 | +# Inside the function, we have a loop which adds the passed argument and prints the result. |
| 56 | +# We passed 3 different tuples with variable length as an argument to the function. |
| 57 | + |
| 58 | +# Python **kwargs |
| 59 | +# Python passes variable length non keyword argument to function using *args but we cannot use this to pass keyword argument. For this problem Python has got a solution called **kwargs, it allows us to pass the variable length of keyword arguments to the function. |
| 60 | + |
| 61 | +# In the function, we use the double asterisk ** before the parameter name to denote this type of argument. The arguments are passed as a dictionary and these arguments make a dictionary inside function with name same as the parameter excluding double asterisk **. |
| 62 | + |
0 commit comments