In the tutorial, we will learn about how to use python function with argument and how we can add arguments in user-defined functions.
What is Python Function Argument
We can add user-defined parameters in functions as Arguments.
Python arguments are specified after the function name inside the parentheses. You can add as many arguments as you want; just separate them with a comma.
We can add an argument in between the brackets after the function. Python parameters can be defined in two ways.
Syntax of function with argument.
Let see an example of the python function with an argument.
As we can see, we are using the name as an argument, and we are printing the argument inside the function.
Then, we are calling the function with the parameters in python as a string.
Multiple Python Function Argument
We can also add more than one argument in the python function by adding a comma (,) between two arguments.
Syntax of multiple arguments.
Let’s check an example of multiple arguments.
The output will be as follows.
Here we are giving two string arguments as the first name as Elon and last name as Musk, and as we can see the output, there is no space between first and last name because we are not using any space in the argument.
Default Python Argument Function
We can add the default value in the function argument.
We can provide a default value as an argument by using the assignment operator (=).
Syntax of default argument.
Let’s check an example of the default argument in the python function.
When we execute the above program, we will get the following output.
As we can see, we have used the default value in the msg argument. When we call the function, we are only using one parameter. The second argument is called automatically, as we already declared.
Python Arbitrary Arguments, *args
Python Arbitrary Argument used too many arguments without knowing the number of arguments passed into your function.
We can use the * operator before the parameter name in the function definition.
Syntax of Python arbitrary argument.
Let check an example of Arbitrary Arguments.
This way, the function will receive a tuple of arguments and can access the items accordingly:
Arbitrary Keyword Arguments, **kwargs
If we do not know how many keyword arguments will be passed into your function, add two asterisks: ** before the parameter name in the function definition.
This way, the function will receive a dictionary of arguments and access the items accordingly.
Let see an example of kwargs arguments.
When we run the above program we will get following output:
Passing a List as an Argument using for loop
We can send any data type of argument to a function (string, number, list, dictionary, etc.), and it will be treated as the same data type inside the function.
Example of addition of list using for loop:
The output will be as follow.
Using if condition in Python Function
We can add if conditional statement inside a python function.
Let us check an example of if condition in the python function.
The output will be as follows.
Here we are adding a for loop inside the function for iterate through the list, and inside the for loop, we are giving the condition to check whether the banana is present in the list.