Python Function Arguments with Types, Syntax and Examples
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Previously, we have covered Functions in Python. In this Python Function Arguments tutorial, we will learn about what function arguments used in Python and its type: Python Keyword Arguments, Default Arguments in Python, and Python Arbitrary Arguments.
So, let’s start Python Function Arguments.
What is Python Function?
Python function is a sequence of statements that execute in a certain order, we associate a name with it. This lets us reuse code.
We define a function using the ‘def’ keyword. Let’s take an example.
>>> def sayhello():                """                This prints Hello                """                print("Hello")
Then to call this, we simply use the function’s name with parentheses. Also, notice the docstring.
>>> sayhello()
Output
This one takes no arguments. Now, let’s see one with python function arguments.
>>> def sum(a,b): Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return a+b >>> sum(2,3)
Output
To this, if we pass only one argument, the interpreter complains.
>>> sum(3) Traceback (most recent call last): File "<pyshell#44>", line 1, in <module> sum(3)
Output
To deal with such situations, we see different types of arguments in python functions.
Types of Python Function Arguments
There are various types of Python arguments functions. Let’s learn them one by one:
1. Default Argument in Python
Python Program arguments can have default values. We assign a default value to an argument using the assignment operator in python(=).
When we call a function without a value for an argument, its default value (as mentioned) is used.
>>> def greeting(name='User'): Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print(f"Hello, {name}") >>> greeting('Ayushi')
Output
>>> greeting()
Output
Here, when we call greeting() without an argument, the name takes on its default value- ‘User’.
Any number of arguments can have a default value. But you must make sure to not have a non-default argument after a default argument.
In other words, if you provide a default argument, all others succeeding it must have default values as well.
The reason is simple. Imagine you have a function with two parameters.
The first argument has a default value, but the second doesn’t. Now when you call it(if it was allowed), you provide only one argument.
The interpreter takes it to be the first argument. What happens to the second argument, then? It has no clue.
>>> def sum(a=1,b): Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return a+b
Output
This was all about the default arguments in Python
2. Python Keyword Arguments
With keyword arguments in python, we can change the order of passing the arguments without any consequences.
Let’s take a function to divide two numbers, and return the quotient.
>>> def divide(a,b): Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return a/b >>> divide(3,2)
Output
We can call this function with arguments in any order, as long as we specify which value goes into what.
>>> divide(a=1,b=2)
Output
>>> divide(b=2,a=1)
Output
As you can see, both give us the same thing. These are keyword python function arguments.
But if you try to put a positional argument after a keyword argument, it will throw Python exception of SyntaxError.
>>> divide(b=2,1)
Output
Any Doubt yet in Python Function Arguments
3. Python Arbitrary Arguments
You may not always know how many arguments you’ll get. In that case, you use an asterisk(*) before an argument name.
>>> def sayhello(*names): Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â for name in names: Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print(f"Hello, {name}")
And then when you call the function with a number of arguments, they get wrapped into a Python tuple.
We iterate over them using the for loop in python.
>>> sayhello('Ayushi','Leo','Megha')
Output
Hello, Leo
Hello, Megha
This was all about the Python Function Arguments.
Python Interview Questions on Function Arguments
- What are function Arguments in Python?
- How do you create a function argument in Python?
- How do you check if a function is an argument in Python?
- What is an argument in Python? Give an example.
- What are the types of function arguments in Python?
Conclusion
Hence, we conclude that Python Function Arguments and its three types of arguments to functions. These are- default, keyword, and arbitrary arguments.
Where default arguments help deal with the absence of values, keyword arguments let us use any order.
Finally, arbitrary arguments in python save us in situations where we’re not sure how many arguments we’ll get.
Furthermore, if you have a query, feel free to ask in the comment box.
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google
for what purpose the arguements are using!!
Hi Venu,
We can pass arguments to a function to make it operate on their values. Like a function add(a,b) adds the two numbers a and b and needs their values to perform the addition.
can we pass argument of 2 diff datatype in any user define python function?
Yes, we can pass 0 or more arguments of same or different type to an user defined function. Here is an example
def func(i,str):
print(i)
print(str)
func(2,””DataFlair””)
what is the priority of the types of arguments
The order in which the arguments are passed is very important when we are using more than one type of arguements. Internally, Python follows to below steps to match the arguments:
1.Assigning Nonkeyword arguments by the position
2.Giving Keyword arguments by names
3.Matching extra non-keyword to *var tuple
4.Matching extra keywords to **var dictionary
5.Default values are unassigned arguments in the header
So, it is better to follow the below rules while calling the function:
1. Give the Keyword arguments after all non-keyword arguments.
2. In the function definition, mention the *var after normal and default arguments. And **var must be last.
Hope, I could solve your doubt.
what this function declarion do:
def func1() -> webdriver
The abv what it does.