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.

Python Function Arguments with Types, Syntax and Examples

Python Function Arguments with Types, Syntax and Examples


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

Then to call this, we simply use the function’s name with parentheses. Also, notice the docstring.

>>> sayhello()



This one takes no arguments. Now, let’s see one with python function arguments.

>>> def sum(a,b):
                return a+b
>>> sum(2,3)



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>


TypeError: sum() missing 1 required positional argument: ‘b’

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')


Hello, Ayushi
>>> greeting()


Hello, User

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


SyntaxError: non-default argument follows default argument

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)



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)


>>> divide(b=2,a=1)



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)


SyntaxError: positional argument follows keyword argument in python.

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')


Hello, Ayushi
Hello, Leo
Hello, Megha

This was all about the Python Function Arguments.

Python Interview Questions on Function Arguments

  1. What are function Arguments in Python?
  2. How do you create a function argument in Python?
  3. How do you check if a function is an argument in Python?
  4. What is an argument in Python? Give an example.
  5. What are the types of function arguments in Python?


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.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google


7 Responses

  1. venu says:

    for what purpose the arguements are using!!

    • DataFlair Team says:

      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.

  2. Shraddha says:

    can we pass argument of 2 diff datatype in any user define python function?

    • DataFlair says:

      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):

  3. Jacksepticeye says:

    what is the priority of the types of arguments

    • DataFlair says:

      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.

  4. Arun says:

    what this function declarion do:
    def func1() -> webdriver
    The abv what it does.

Leave a Reply

Your email address will not be published. Required fields are marked *