Arbitrary Arguments
We have already considered the function's positional and optional arguments. But what can we do when there are many arguments, or we don't know all the arguments that must be passed to a function? We can solve this problem using arbitrary arguments (variable-length arguments).
These arguments will allow a function to accept an arbitrary number of arguments. It is useful when you want to define a function that can take a variable number of input values without specifying the exact number of arguments beforehand.
Note
Pay attention that each argument can be a data structure - list, dictionary, etc. You can pass an arbitrary number of such structures using arbitrary arguments.
In Python, you can define arbitrary arguments as one positional argument using an asterisk *
before the argument name. Here's an example:
In the code above, we used *
to declare that the values
variable contains several arbitrary arguments and has to be interpreted as a tuple. Then we used the built-in sum
function to calculate the sum. We can see that the result is correct for all examples, and we don't have to specify the number of arguments directly. Pay attention that we use this argument by name without *
in function body.
Note
To define an arbitrary argument tuple, it's enough to use
*
before the argument name. But it is recommended to use the*args
construction. This improves the interpretability and readability of the code.
The *args
parameter allows you to pass a variable number of arguments to a function. These arguments are stored in a tuple. For example, when calling a function with different numbers of arguments, *args
will collect all of them into a single tuple, regardless of whether there are any values passed or not. Here is an example:
As you can see from the results:
- If the function is called without any arguments,
args
will be an empty tuple()
; - If the function is called with one argument,
args
will be a tuple with one element(1,)
; - If the function is called with multiple arguments, all of them are stored in the tuple, for example
(1, 2, 3, 'hello', [4, 5, 6])
.
Therefore, with *args
, you can work with the values just like you would with any other tuple in Python.
Task
Write a function that calculates the average value of an arbitrary number of arguments. Use args
as the name of an arbitrary argument.
Everything was clear?
Course Content
Python Functions Tutorial
1. What is Function in Python?
2. Positional and Optional Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions
Python Functions Tutorial
Arbitrary Arguments
We have already considered the function's positional and optional arguments. But what can we do when there are many arguments, or we don't know all the arguments that must be passed to a function? We can solve this problem using arbitrary arguments (variable-length arguments).
These arguments will allow a function to accept an arbitrary number of arguments. It is useful when you want to define a function that can take a variable number of input values without specifying the exact number of arguments beforehand.
Note
Pay attention that each argument can be a data structure - list, dictionary, etc. You can pass an arbitrary number of such structures using arbitrary arguments.
In Python, you can define arbitrary arguments as one positional argument using an asterisk *
before the argument name. Here's an example:
In the code above, we used *
to declare that the values
variable contains several arbitrary arguments and has to be interpreted as a tuple. Then we used the built-in sum
function to calculate the sum. We can see that the result is correct for all examples, and we don't have to specify the number of arguments directly. Pay attention that we use this argument by name without *
in function body.
Note
To define an arbitrary argument tuple, it's enough to use
*
before the argument name. But it is recommended to use the*args
construction. This improves the interpretability and readability of the code.
The *args
parameter allows you to pass a variable number of arguments to a function. These arguments are stored in a tuple. For example, when calling a function with different numbers of arguments, *args
will collect all of them into a single tuple, regardless of whether there are any values passed or not. Here is an example:
As you can see from the results:
- If the function is called without any arguments,
args
will be an empty tuple()
; - If the function is called with one argument,
args
will be a tuple with one element(1,)
; - If the function is called with multiple arguments, all of them are stored in the tuple, for example
(1, 2, 3, 'hello', [4, 5, 6])
.
Therefore, with *args
, you can work with the values just like you would with any other tuple in Python.
Task
Write a function that calculates the average value of an arbitrary number of arguments. Use args
as the name of an arbitrary argument.
Everything was clear?