In Python, if a function has a default parameter value at function definition, then during the function call, it will have an optional argument. Although there is only one way to define a default parameter and optional argument, in Python, we can also use the
*args
and
**kwargs
for optional arguments.
There are two ways to pass an argument value during function calling
positional
and
keyword
. In positional argument passing, we pass the argument in the same order as the parameters are defined in the function definition.
Example
def func(para1, para2): pass fun(arg1, arg2) #here para1 will have the value of arg1 and para2 will have the value of arg2
In keyword argument, we specify the parameter name in the function calling and assign the argument values to them, and here the order of parameters or arguments does not matter.
Example
def fun(para1, para2): pass fun(para2=arg2, para1=arg1) #here also para1 will have the value of arg1 and para2 will have the value of arg2
But in both the function calling statements, we have an equal number of arguments passed to the function calling as the number of parameters defined in the function definition. In the function definition, we can also specify default values to the parameters using the assignment operator. By doing so, the argument values for the default parameters become optional.
In this Python tutorial, we will discuss the Optional argument or default parameters. And learn how it works in Python. We will also discuss how we can use optional arguments without even defining a default parameter, using
*args
and
**kwargs
parameters.
What are the Arguments in Python?
Arguments are the values passed inside the parenthesis
()
and separated with commas
,
during the function calling. When the function is called Python, assign all those argument values to the function definition parameters, and those values become the local scope values for the function body.
Example
# function definition with two parameters def show_data(para1, para2): print("Value of para1: ", para1) print("Value of para2: ", para2) arg1= 23 arg2= 33 # function call with two argument show_data(arg1, arg2)
Output
Value of para1: 23 Value of para2: 33
Behind the code
In this example, when we define the function using
def
keyword there we defined the function name
show_data(para1, para2)
with two parameters
para1
and
para2
. And when we call the function
show_data(arg1, arg2)
there we pass the two arguments values
arg1
and
arg2
. When the Python executes the
show_data(arg1, arg2)
statement, it passed the values of
arg1
to
para1
and
arg2
to
para2
, and execute the function body. The values of arguments are assigned to the parameters in the same order.
Python Optional Argumnets
If there are three parameters defined in the function definition, then we need to pass three argument values during the function call. Else, we will receive the
TypeError: missing required positional argument
. But there are three scenarios when the number of arguments can be optional, and it does not need to be the same as the number of parameters.
- If we have a default parameter value.
-
If we use the
*args
as the parameter. -
If we use the
**kwagrs
as the parameter.
An optional argument is a type of argument that may or may not be passed during the function call.
1. Optional Argument using Default Parameter
The easiest way to create an optional argument is by defining a default parameter. During the function definitions, we can assign default values to the parameters, and the argument for those parameters becomes optional.
Example
# show_data function with default gender parameter def show_data(name, age, gender="Man"): print(f"Name : {name}") print(f"Age : {age}") print(f"Gender : {gender}") name = "Rahul" age = 23 # only call function on two arguments show_data(name, age)
Output
Name : Rahul Age : 23 Gender : Man
In this example, we have defined the default parameter
gender='Man'
during the function definition. This means we have an optional argument for this parameter, and we may or may not decide to pass that optional argument during the function call. If we decide to pass a value for the optional argument, it will override the value of the default parameter.
Example
# show_data function with default gender parameter def show_data(name, age, gender="Man"): print(f"Name : {name}") print(f"Age : {age}") print(f"Gender : {gender}") name = "Jiya" age = 21 gender = "Female" # call the function show_data(name, age, gender)
Output
Name : Jiya Age : 21 Gender : Female
Note: The defualt parameter must be defined after all the required or positional parameter, else you will encouter the SyntaxError: non-default argument follows default argument . Note: Python does not define the term "parameter" for the names defined in the function definition parenthesis. It refers to both parameters and arguments as arguments. Just to simplify this tutorial we are using the term parameters for the arguments defined during the function definition.
2. Optional Argument using *args
Using the
*args
as the parameter in the function definition, we can specify n number of optional arguments. Unlike default parameters with
*args
we do not have default values for all the arguments. But it makes sure that we do have an option for an arbitrary number of arguments. The
*args
accept all the arguments as a tuple object, and we can access them in our function body with the name
args
(The name
args
in
*args
is optional, but it is a conventional name and you will find the same name in all the Python programs)
Example
# show_data function *args def show_data(name, *args): print(f"Name : {name}") # args is the tuple for data in args: print("Unknown data: ",data) name = "Jiya" age = 21 gender = "Female" status = "Unmarried" # call the function show_data(name, age, gender, status)
Output
Name : Jiya Unknown data: 21 Unknown data: Female Unknown data: Unmarried
In this example, we could have also omitted the
age
,
gender
and
status
, arguments because they are optional. The
*args
parameters accept them as a tuple object. But the
name
parameter was required and positional. We have to pass it in any case. we could have also called the
show_data()
function without the optional arguments.
show_data(name) #output Name : Jiya
Note:
Like default parameters the
*args
also must specify after all the positonal parameter.
3. Optional Argument using **kwargs
In the above section, we learned how we can define optional arguments using
*args
, but while using *args, we do not have the label for the argument values. The *args is good when we are collecting arbitrary arguments from the function call. But when we require a labeled argument, then we should use
**kwargs
instead of
*args
. The
**kwargs
is similar to
*args
, as *args accept all the positional argument values as the tuple elements, the
**kwargs
accept keyword arguments as the dictionary
key:value
pairs. With **kwargs we can accept the arbitrary number of keyword arguments from the function call.
Example
# show_data function **kwargs def show_data(name, **kwargs): print(f"Name : {name}") # kwargs as dictionary for data in kwargs: print(f"{data}: ",kwargs[data]) name = "Jiya" age = 21 gender = "Female" status = "Unmarried" # call the function show_data(name, age=age, gender=gender)
Output
Name : Jiya age: 21 gender: Female
Conclusion
The optional argument is one of the important concepts of Python programming. Many Python functions and methods use default parameters,
*args
and
**kargs
to implement optional arguments. Defining an optional argument is very easy, and for many programs, it also becomes essential to define them.
People are also reading:
Leave a Comment on this Post