Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

How can a variable number of an arguements need to be passed to a function. 

Along these lines to utilizing varargs in C or C++:

fn(a, b)

fn(a, b, c, d, ...)

closed

4 Answers

0 votes
by (25.7k points)
 
Best answer
In Python, you can use the *args syntax to pass a variable number of arguments to a function. This allows you to handle an arbitrary number of arguments within the function. Here's an example:

def fn(*args):

    # Process the arguments

    for arg in args:

        print(arg)

# Example usage

fn(a, b)

fn(a, b, c, d, ...)

In this code, *args in the function definition acts as a placeholder for the variable number of arguments passed to the function. Within the function body, you can iterate over the args tuple and perform operations on each argument.

When calling the function, you can pass any number of arguments separated by commas, and they will be collected into the args tuple. This provides flexibility when dealing with functions that need to handle different numbers of arguments.
0 votes
by (26.4k points)

Indeed. You can utilize *args as a non-keyword contention. You can then pass quite a few arguments. 

def manyArgs(*arg):

  print "I was called with", len(arg), "arguments:", arg

>>> manyArgs(1)

I was called with 1 arguments: (1,)

>>> manyArgs(1, 2, 3)

I was called with 3 arguments: (1, 2, 3)

As should be obvious, Python will unload the contentions as a solitary tuple with every one of the arguments.

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

0 votes
by (15.4k points)
In Python, you can utilize the *args syntax to pass a variable number of arguments to a function. This enables you to handle an unspecified number of arguments within the function. Consider the following example:

def fn(*args):

    # Process the arguments

    for arg in args:

        print(arg)

# Example usage

fn(a, b)

fn(a, b, c, d, ...)

In this code, the *args parameter in the function definition serves as a placeholder for the variable number of arguments supplied to the function. Within the function body, you can iterate over the args tuple and perform operations on each argument.

When calling the function, you can pass any number of arguments separated by commas. These arguments will be collected into the args tuple, allowing you to handle different numbers of arguments as needed.
0 votes
by (19k points)
In Python, you can employ the *args syntax to accept a variable number of arguments in a function. This allows the function to handle an arbitrary number of arguments that are passed to it. Consider the following example:

def fn(*args):

    # Process the arguments

    for arg in args:

        print(arg)

# Example usage

fn(a, b)

fn(a, b, c, d, ...)

In this code, the *args parameter in the function definition acts as a placeholder to collect any number of arguments into a tuple named args. Within the function, you can iterate over the args tuple and perform operations on each individual argument.

When invoking the function, you can pass any number of arguments separated by commas. These arguments will be gathered into the args tuple, enabling the function to handle different argument quantities as required.

Browse Categories

...