Program to illustrate scatter in terms of tuple in Python

So, in this article, we will see Python functions that can take up variable length arguments. In many cases, we will have to deal with variable number of arguments depending upon the requirements.

We will learn how to handle such a situation in this tutorial.

variable-length argument tuples

It is a feature that allows the function to take up any number of arguments. In Python many built in functions like max (), min (), sum (), etc., use variable-length arguments.

These functions can take up any number of arguments. In such cases, to specify that it is a variable length argument, we use a symbol ‘*’.

  • Any argument that starts with ‘*’ symbol is known as gather and specifies a variable-length argument.
  • The opposite of gather is scatter.

So, when there is a function that takes up multiple arguments but not a tuple, then the tuple is scattered to passed to individual elements.

Here is the code to demonstrate the following.

Tup=(50,3)
#values are now scattered and passed.
#q and r represents quotient and remainder resp.
q,r=divmod(*Tup)
print(q,r)

output:

16       2

In the code given, the tuple was passed as a single argument but the divmod () functions expects two arguments. So, the symbol ‘*’ denotes that there may be more than one argument present in the argument. (here, it is quotient and remainder).

The given example here is a division operation. This concept can be applied to a number of functions in Python.

The function extracts and scatters them and the corresponding operation is performed. Once the output is obtained, it is scattered and displayed.

 

Leave a Reply

Your email address will not be published.