What are the various types of arguments supported in Python?
Python arguments
In this article we are going to discuss the various types of arguments that can be used in the function. In the previous article we have disused what is a function and various types of functions used in python. The function is a set of code that is executed only when it is called and is used to execute the same code repeatedly without rewriting it.
Example:
def out():
printf(“Inside out function”)
out()
There are various types of functions used in python and 1 such category is known as “user-defined functions”. So in this article first we will discuss user-defined functions and then will discuss the various types of arguments.
So “User-defined functions” can be further categorized into 2 categories, such as:
Simple function:
These are simple functions that are defined using “def” keyword and have simple executable statements like the example given.
Parameterized functions:
Parameterized functions are those in which 1 can pass arguments to perform various evaluations. These arguments become part of the signature of a function. Here the argument is the information that is being passed to the function when it is called. But when a function is being defined, a variable is listed within the parenthesis and is known as “Parameter”.
Example:
def add(a, b):
print(“ Sum is: “,a+b)
add(4,5)
Here the variable defined within the definition of the function (a,b) are known as “Parameter” and the values passed while calling the functions (4,5) are known as “Arguments”.
Types of argument:
Required argument:
Required argument function is the simplest user-defined functions with parameters. In these parameters are defined with the functions and then while calling the function, the same type of arguments are to be passed.
Example:
Here one should pass the same number of arguments as the numbers of parameters passed; otherwise the interpreter will give an error, the same has been shown in the example as well.
Default argument:
The default argument is the type of argument that has a default value defined along with the parameter. In other words default argument is the argument that holds a default value if the user does not pass 1 while calling the function.
Example:
Here in this example you can see when I tried to pass only a single argument’s value, the interpreter has thrown the error for missing value for the other parameter. To solve this problem, the default argument is used. You can just assign the value to either 1 or to all the parameters which will only be executed when the user does not pass the value.
Example:
def details(name, lang = "Python"):
print("The name of the developer is:",name, "and the language learnt is:",lang)
details(“Rohit”,”C++”)
details(“Diksha”)
Like:
Here you can see I have assigned “Python” as the default value for the “lang“argument so if a user does not pass the value for language, python will be assigned, by default.
Keyword argument:
In python when functions are called, the values passed are assigned to the arguments based on their positions, which means the first value passed will be assigned to the first argument. This position based distribution sometimes leaves the user confused because the user never knows the format in which function was defined.
Example:
Here in this example you can see marks obtained by a student in algorithm got saved in cs because of the positions of the arguments. So to solve this problem, Keyword arguments are used.
Keyword arguments are often known as "kwargs" are the ones that allow “key = value” syntax. This means it allows the caller to pass the argument name with its value so that the caller does not get confused with the positioning of the arguments. It ignores the order of the arguments.
Example:
def education(course,subject):
print("Course is:",course,"Subject is:",subject)
education(course = 'English hons', subject = 'litreature')
education(subject = 'Algorithms', course = 'B.tech'
education('MCA', subject = 'Data Sturctures')
education(course = 'MCA','Data Sturctures')
Like:
Arbitrary argument:
In some situations, the developer beforehand does not how many arguments will be passed into a function. In such situations asterisk symbol ‘ * ‘ is used.
Arbitrary arguments written as *args is python documents is the solution of the problem where number of arguments is not known. While defining the argument just write the asterisk symbol before the name of the parameter. This symbol tells the interpreter that this is an arbitrary argument. Values passed by this arbitrary argument will be stored in the form of a tuple.
Example:
def course(*subjects):
for sub in subjects:
print("\t",sub)
course('Data structure','DBMS','C++','Fundamentals','Operating systems')
Like:
Arbitrary- keyword argument:
As the name suggests, it is a combination of both types which is used when the numbers of arguments being passed are unknown, and “key-value” syntax is to be followed. To declare any argument as arbitrary – keyword argument double asterisk symbols are placed. Eg **a
**kwargs is used to pass arbitrary keyword argument which is stored as a dictionary in the python. Here dictionary of the keys is passed as the arbitrary keyword. And then these keywords will be used to access values.
Example:
These are the types of arguments that can be used in python function.
To know more about function: Click here