InterviewSolution
Saved Bookmarks
| 1. |
*args and **kwargs in Python |
|
Answer» We can pass a variable number of arguments to a function using special symbols called *args and **kwargs. The usage is as follows:
Example: # the function will take in a variable number of arguments# and print all of their values def tester(*argv): for arg in argv: print(arg) tester('Sunday', 'Monday', 'Tuesday', 'Wednesday') Output: Sunday Monday Tuesday Wednesday
Example: # The function will take variable number of arguments# and print them as key value pairs def tester(**kwargs): for key, value in kwargs.items(): print(key, value) tester(Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4) Output: Sunday 1 Monday 2 Tuesday 3 Wednesday 4 |
|