Contenido del Curso
Python Functions Tutorial
Python Functions Tutorial
Arbitrary Arguments
We have already explored the function's positional and optional arguments. However, what should we do when there are numerous arguments, or when we are uncertain about all the arguments that must be passed to a function? This issue can be addressed using arbitrary arguments (variable-length arguments). These arguments enable a function to accept an unspecified number of arguments. This is particularly useful when you want to define a function that can take a variable number of input values without specifying the exact number of arguments in advance.
Note
Remember that each argument can be a data structure, such as a list or dictionary. You can pass an arbitrary number of such structures using arbitrary arguments.
In Python, you can define arbitrary arguments as a single positional argument by placing an asterisk *
before the argument name. Here's an example:
# Define function with arbitrary arguments named values def calculate_sum(*values): return sum(values) # Test the function using different number of arguments print(calculate_sum(1, 2, 3)) print(calculate_sum(1, 2, 3, 4)) print(calculate_sum(1, 2, 3, 4, 5))
In the code above, we used *
to declare that the values
variable contains several arbitrary arguments and is 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. Note that we use this argument by name without *
in the function body.
Note
To define an arbitrary argument tuple, it's enough to use
*
before the argument name. However, it is recommended to use the*args
construction. This improves the interpretability and readability of the code.
def example_function(*args): # Print the type of args print(f'Type of args: {type(args)}') # Print the whole tuple print(f'Args tuple: {args}') # Iterate over the tuple for arg in args: print(arg) # Call the function without any arguments print("Call without arguments:") example_function() # Call the function with one argument print("\nCall with one argument:") example_function(1) # Call the function with multiple arguments print("\nCall with multiple arguments:") example_function(1, 2, 3, 'hello', [4, 5, 6])
As you can see from the results:
- When the function is called without any arguments,
args
will be an empty tuple()
. - When the function is called with one argument,
args
will be a tuple with one element(1,)
. - When the function is called with multiple arguments, all of them are stored in the tuple, for example
(1, 2, 3, 'hello', [4, 5, 6])
.
With *args
, you can work with the values just like you would with any other tuple in Python.
Swipe to show code editor
Write a function that calculates the average value of an arbitrary number of arguments. Use args
as the name of an arbitrary argument.
¡Gracias por tus comentarios!
Arbitrary Arguments
We have already explored the function's positional and optional arguments. However, what should we do when there are numerous arguments, or when we are uncertain about all the arguments that must be passed to a function? This issue can be addressed using arbitrary arguments (variable-length arguments). These arguments enable a function to accept an unspecified number of arguments. This is particularly useful when you want to define a function that can take a variable number of input values without specifying the exact number of arguments in advance.
Note
Remember that each argument can be a data structure, such as a list or dictionary. You can pass an arbitrary number of such structures using arbitrary arguments.
In Python, you can define arbitrary arguments as a single positional argument by placing an asterisk *
before the argument name. Here's an example:
# Define function with arbitrary arguments named values def calculate_sum(*values): return sum(values) # Test the function using different number of arguments print(calculate_sum(1, 2, 3)) print(calculate_sum(1, 2, 3, 4)) print(calculate_sum(1, 2, 3, 4, 5))
In the code above, we used *
to declare that the values
variable contains several arbitrary arguments and is 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. Note that we use this argument by name without *
in the function body.
Note
To define an arbitrary argument tuple, it's enough to use
*
before the argument name. However, it is recommended to use the*args
construction. This improves the interpretability and readability of the code.
def example_function(*args): # Print the type of args print(f'Type of args: {type(args)}') # Print the whole tuple print(f'Args tuple: {args}') # Iterate over the tuple for arg in args: print(arg) # Call the function without any arguments print("Call without arguments:") example_function() # Call the function with one argument print("\nCall with one argument:") example_function(1) # Call the function with multiple arguments print("\nCall with multiple arguments:") example_function(1, 2, 3, 'hello', [4, 5, 6])
As you can see from the results:
- When the function is called without any arguments,
args
will be an empty tuple()
. - When the function is called with one argument,
args
will be a tuple with one element(1,)
. - When the function is called with multiple arguments, all of them are stored in the tuple, for example
(1, 2, 3, 'hello', [4, 5, 6])
.
With *args
, you can work with the values just like you would with any other tuple in Python.
Swipe to show code editor
Write a function that calculates the average value of an arbitrary number of arguments. Use args
as the name of an arbitrary argument.
¡Gracias por tus comentarios!