Variable Length Parameter (*, **), Variable Parameter
Variable Length Parameter
Python also supports variable-length parameter lists. Variable Length parameters can be tuples or dictionaries.
1. tuples
When a parameter starts with *, the variable length parameter is considered as a tuples in the following format:
Def func (* t ):
In the func () function, t is treated as a tuples and every variable length parameter is obtained using t [index.
For example:
1 def func1 (* t): 2 print ("variable length parameter quantity:") 3 print (len (t) 4 print ("in sequence :") 5 for x in range (len (t): 6 print (t [x]); 7 8 func1 (1, 2, 4 ); 9 10 # output 11 variable length parameters: 12 413 in sequence: 14 115 216 317 4
2. Dictionary
When the parameter starts with **, it indicates that the variable length parameter will be treated as a dictionary in the following format:
Def func (** t ):
You can use the func () function for any number of arguments. The format of arguments is as follows:
Key = value # For example sum (a = 1, B = 2, c = 3)
Example:
Def sum (** t): print (t) sum (a = 1, B = 2, c = 3) # output {'A': 1, 'B ': 2, 'C': 3}