We already learned about functions in the previous chapter. In this section, we will look at the different types of arguments we can pass to a function.
In the previous chapter, we passed some arguments to functions. However, a function can also take a variable number of arguments.
Let’s look at a fixed number of arguments first.
Python Fixed Arguments
The number of arguments passed in a function call are equal to the number of parameters in the function definition.
Python Positional Arguments
In all the previous examples, we passed a fixed number of arguments in the correct order. These arguments which are passed in the same order as that of the parameters to which they have to be assigned are called positional arguments.
def display(name, age):
print("My name is", name, "and my age is", age)
display("John", 45)
The function display()
has two parameters - name and age. Therefore, we have passed two arguments in the function call so that the first argument “John” is assigned to the first parameter name
and the second argument 45 is assigned to the second parameter age
.
In the above example, if only one argument is passed to the function, we will get an error of missing positional argument.
def display(name, age):
print("My name is", name, "and my age is", age)
display("John")
Python Keyword Arguments
We can also pass a value assigned to a parameter as named argument. Let’s see how.
def display(name, age):
print("My name is", name, "and my age is", age)
display(name = "John", age = 45)
In this example, name = "John"
and age = 45
are the two keyword arguments. The first keyword argument name = "John"
assigns the value "John" to the parameter name
and the second keyword argument age = 45
assigns the value 45 to the parameter age
.
The order (position) in which keyword arguments are passed can be changed. Look at the following example.
def display(name, age):
print("My name is", name, "and my age is", age)
display(age = 45, name = "John")
We can also pass a mix of positional and keyword arguments in a function call, but keyword arguments must follow positional arguments i.e., all keyword arguments must come after positional arguments.
def display(name, age, country):
print("My name is", name, "my age is", age, "and I live in", country)
display("John", 45, country = "Canada")
In the above example, "John" and 45 are positional arguments and country = "Canada" is a keyword argument.
If we give positional argument after keyword argument, then we get an error.
def display(name, age, country):
print("My name is", name, "my age is", age, "and I live in", country)
display("John", country = "Canada", 45)
Here, we have passed a position argument 45 after keyword argument country=”Canada” and that’s why we got the error.
Python Variable Arguments
For variable arguments, the number of arguments passed in a function call may not be equal to the number of parameters in the function definition.
Python Default Arguments
We can assign default values to parameters in function definition. These are called default arguments.
def display(name = "xyz"):
print("My name is", name)
display()
display("John")
The parameter name
is assigned a default value "xyz". In the first function call, when no value is passed for this parameter, then the parameter takes this default value. In the second function call, "John" is passed in the function call, so the parameter name
takes the value "John".
We can give any number of default arguments in a function. Default arguments must follow non-default arguments (positional or keyword arguments) when passed together.
Let’s see an example in which the function has a positional and a default argument.
def display(name, age = 45):
print("My name is", name, "and my age is", age)
display("John")
display("John", 50)
Here, passing the first argument (positional argument) is mandatory and the second argument is optional. If no second argument is passed, then the parameter age
takes the default value 45.
Look at another example in which the function has a mix of positional, keyword and default arguments.
def display_marks(maths, physics, chem, bio = 80):
print("Maths:", maths, "Physics:", physics, "Chemistry:", chem, "Biology:", bio)
display_marks(85, chem = 45, physics = 90)
display_marks(85, chem = 45, physics = 90, bio = 50)
As mentioned, keyword arguments must follow positional arguments and default arguments must follow non-default arguments (positional and keyword arguments). Violation of this order results in an error.
The following program throws an error because the default argument is given before a non-default argument in function definition.
def display_marks(maths, bio = 80, physics):
print("Maths:", maths, "Physics:", physics, "Biology:", bio)
display_marks(85, physics = 90)
Python Arbitrary Arguments
In some situations, we don’t know in advance how many arguments will be passed in function call. For those cases, we can use arbitrary arguments.
Arbitrary arguments are variable-length arguments and are used when we are not sure of the number of arguments to be passed in function call.
There are two types of arbitrary arguments.
- Arbitrary positional arguments
- Arbitrary keyword arguments
Python Arbitrary Positional Arguments
An asterisk (*
) is used before the parameter name for arbitrary positional arguments. For example,
We can pass any number of non-keyword arguments to this parameter.
*parameter_name
will take all those non-keyword arguments and parameter_name
will be a tuple containing all those arguments.
Let’s look at an example.
def some_function(*some_parameter):
print(some_parameter)
some_function(1,2,3,4)
some_function(1,2,3,4,5,6,7)
You can see that all passed arguments 1,2,3,4 and 1,2,3,4,5,6,7 are available as elements of the tuple some_parameter
.
Let’s take a function product_summary()
which displays the list of products purchased by different customers.
def product_summary(*products):
print("Products purchased:")
for product in products:
print(product)
print("### Customer 1 ###")
product_summary("TV", "Fridge", "Washing Machine", "AC", "Heater")
print("### Customer 2 ###")
product_summary("AC", "Microwave", "Heater")
Customer 1 purchased 5 items and customer 2 purchased 3 items.
In the first function call, we passed five values. These values got assigned to the parameter
products in the form of a tuple, making products = ('TV', 'Fridge', 'Washing Machine', 'AC', 'Heater'). We printed the elements of this tuple using a for
loop.
Similarly, in the second function call, we passed three values, making products = ('AC', 'Microwave', 'Heater').
This example must have given you an idea of the use case of arbitrary arguments.
Python Arbitrary Keyword Arguments
A double asterisk (**
) is used before the parameter name for arbitrary keyword arguments. We can pass any number of keyword arguments to this parameter.
Now, suppose the function product_summary()
displays the list of products along with their quantities purchased by different customers.
def product_summary(**products):
print("Products purchased:")
for product, quantity in products.items():
print(product,":",quantity)
print("*** Customer 1 ***")
product_summary(TV = 3, Fridge = 4)
print("*** Customer 2 ***")
product_summary(AC = 3, Microwave = 2, Heater = 2)
In the first function call, we passed two keyword values. These values got assigned to the parameter products
in the form of a dictionary, making products = {'TV': 3, 'Fridge': 4}. Similarly, in the second function call, we passed three keyword values, making products = {'AC': 3, 'Microwave': 2, 'Heater': 2}.
With this, you just finished the first part of Python programming. So give yourself a pat and have a cup of coffee.