Python functions that accept variable-length parameters. In many scenarios, depending on the needs, we will have to deal with a variable number of arguments.
Variable-length Arguments
It is a feature that enables the function to accept any number of parameters. Many built-in Python functions, such as max (), min (), sum (), and so on, take variable-length arguments.
These functions can accept an unlimited number of parameters. In such circumstances, we use the symbol ‘*’ to indicate that the argument is of variable length.
Any parameter that begins with the ‘*’ symbol is known as gather and signifies a variable-length argument.
The antonym(opposite) of gather is scatter.
So, if there is a function that accepts several inputs but not a tuple, the tuple is scattered and passed to individual elements.
Program to Show Scatter in Terms of Tuple in Python
Approach:
- Give the tuple with with 2 elements a static input and store it in a variable
- Here, the divmod() function doesn’t accept a tuple.
- So, the tuple values are scattered and passed using the ‘*’ symbol.
- Here divmod() function returns quotient and remainder and are stored in two separate variables
- Print the above-obtained quotient and remainder values.
- The Exit of the Program.
Below is the implementation:
# Give the tuple with with 2 elements a static input and store it in a variable gvn_tuple = (25, 2) # Here, the divmod() function doesn't accept a tuple. # so, the tuple values are scattered and passed using the '*' symbol. # Here divmod() function returns quotient and remainder and are # stored in two separate variables quotient, remaindr = divmod(*gvn_tuple) # Print the above obtained quotient and remainder values print("quotient:", quotient," ", "remainder:", remaindr)
Output:
quotient: 12 remainder: 1
Explanation:
The tuple was sent as a single argument in the code provided, but the divmod() function needs two inputs. As a result, the sign ‘*’ indicates that the argument may contain more than one argument. (In this case, it is quotient and remainder.)
The example given here is a division operation. This approach can be used to a variety of Python functions.
The function extracts and scatters them, then performs the relevant procedure. After obtaining the output, it is scattered and shown.