Python functions and usage part-2
-
date_range 10/01/2019 00:00 infosortPython3-TutoriallabelPython3-tutorials
Hi There, In this post we’ll learn more about python functions.
Passing flexible number of arguments to a function
-
In Python We can also pass N no. of arguments to a function as per our requirement. For this we have to use
*argsas parameter to that function. -
Example:
>>> def sum_all_nums(*args): ... total = 0 ... for num in args: ... total += num ... return total ... >>> sum_all_nums(1, 2, 3, 4, 5) 15 >>> sum_all_nums(1, 2) 3 >>> sum_all_nums(2, 4, 5, 6, 7, 2, 3, 4, 5 , 5, 7) 50 >>> - In the above example we can observe that we called
sum_all_numsfunction three time with different number of arguments this is because we have used*argsas a parameter. - Here the word args can be of any name Ex:
*nums,*words.
**kwargs in Python
-
The double asterisk form of
**kwargsis used to pass a keyworded, variable-length argument dictionary to a function. You should use**kwargsif you want to handle named arguments in a function. -
Like
*args,**kwargscan take however many arguments you would like to supply to it. However,**kwargsdiffers from*argsin that you will need to assign keywords. -
*argsand**kwargsmake the function flexible. -
Usage of
**kwargs:>>> def print_kwargs(**kwargs): ... return kwargs ... >>> print_kwargs(name="Shark", age=24, weight=58) {'age': 24, 'name': 'Shark', 'weight': 58} >>>
Parameters ordering
- While passing the parameters to a function they should follow the below order if the function has below all arguments.
- parameters
*args- default parameters
**kwargs
Tuple Unpacking
-
Using
*as an argument:>>> def unpack(*args): ... return args ... >>> nums = [1, 2, 3, 4, 5] >>> >>> unpack(nums) ([1, 2, 3, 4, 5],) >>> >>> >>> unpack(*nums) #Tuple unpacking using *nums (1, 2, 3, 4, 5) >>> -
To unpack the values from a sequnce we need to pass
*infornt of the sequence variable.
Dictonary Unpacking
-
Using
**as an argument:>>> def display_names(a, b, c): ... return a + b + c ... >>> nums = {'a': 1, 'b': 2, 'c': 3} >>> >>> display_names(**nums) 6 -
To unpack the values from a dictonary we need to pass
**infornt of the dictonary variable.
