The *args and **kwargs keywords are used as parameters. Who can share a snippet using those keywords and explain the meaning,pls | Sololearn: Learn to code for FREE!

+1

The *args and **kwargs keywords are used as parameters. Who can share a snippet using those keywords and explain the meaning,pls

7/22/2020 6:11:58 PM

Kelvin

1 Answer

New Answer

+5

def func(x, *args): #This function takes as *args all the arguments that we give after x print(*args) func(6, 7, 8, 9, 2, 6) >>>7, 8, 9, 2, 6 def func(x, **kwargs): # This function takes key=value pairs, which we put after x print(**kwargs) func(3, seven=7, nine=9) >>>{'seven': 7, 'nine': 9} # Kelvin, extra func :) def func(x, *args, **kwargs): print(x) print(*args) print(**kwargs) func(3, 6, 7, a=7, b=8) >>>3 6, 7 {'a: 7, 'b': 8}