Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arbitrary Arguments | Arbitrary Arguments
Python Functions Tutorial
course content

Conteúdo do Curso

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
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:

12345678
# 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))
copy

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.

123456789101112131415161718
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])
copy

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.

Tarefa
test

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
toggle bottom row

book
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:

12345678
# 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))
copy

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.

123456789101112131415161718
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])
copy

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.

Tarefa
test

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt