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 Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

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:

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

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:

  • 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.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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

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:

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

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:

  • 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.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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

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:

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

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:

  • 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.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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:

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

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:

  • 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.

Tarefa

Write a function that calculates the average value of an arbitrary number of arguments. Use args as the name of an arbitrary argument.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 1
Mude 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