Skip to content

Types of Arguments in Python

What are Arguments in Python?

In Python, arguments are the values that are passed to a function or method call. In other words, arguments are the data that is sent to a function or method when it is called. The function or method receives the data as input and uses it to perform the required task.

In Python, arguments are specified inside the parentheses of a function or method call. The arguments are separated by commas.

Syntax
function_name(argument1, argument2, argument3, ...)
Syntax
function_name(argument1, argument2, argument3, ...)

In the above syntax, argument1argument1, argument2argument2, argument3argument3, and so on are the arguments passed to the function or method call.

We are going to call the function and pass the arguments to it.

arguments.py
def add(x, y):
    """Adds two numbers"""
    print(x + y)
 
add(5, 10)
arguments.py
def add(x, y):
    """Adds two numbers"""
    print(x + y)
 
add(5, 10)

Output:

command
C:\Users\Your Name> python arguments.py
15
command
C:\Users\Your Name> python arguments.py
15

In the above example, we define a function named addadd that takes two parameters xx and yy and prints their sum. We then call the function with two arguments 55 and 1010. The function prints 1515 to the console.

Types of Arguments in Python

In Python, there are three types of arguments. They are:

  1. Default Arguments
  2. Positional Arguments
  3. Keyword Arguments

Default Arguments

Default arguments are the arguments that are assigned a default value. If the function is called without passing the value for the default argument, the default value is used.

Syntax
def function_name(argument1, argument2=default_value):
    """Docstring"""
    # Function body
Syntax
def function_name(argument1, argument2=default_value):
    """Docstring"""
    # Function body

In the above syntax, argument1argument1 is a positional argument and argument2argument2 is a default argument.

Let’s see an example of a function with a default argument.

default-arguments.py
def add(x, y=10):
    """Adds two numbers"""
    print(x + y)
 
add(5)
default-arguments.py
def add(x, y=10):
    """Adds two numbers"""
    print(x + y)
 
add(5)

Output:

command
C:\Users\Your Name> python default-arguments.py
15
command
C:\Users\Your Name> python default-arguments.py
15

In the above example, we define a function named addadd that takes two parameters xx and yy. The default value of yy is 1010. We then call the function with one argument 55. The function prints 1515 to the console. You don’t have to pass the value for the default argument. If you don’t pass the value for the default argument, the default value is used.

Another example of a function with a default argument.

default-arguments.py
def interest(p, r=5, t=1):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000)
interest(1000, 10)
interest(1000, 10, 2)
default-arguments.py
def interest(p, r=5, t=1):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000)
interest(1000, 10)
interest(1000, 10, 2)

Output:

command
C:\Users\Your Name> python default-arguments.py
Simple interest is 50.0
Simple interest is 100.0
Simple interest is 200.0
command
C:\Users\Your Name> python default-arguments.py
Simple interest is 50.0
Simple interest is 100.0
Simple interest is 200.0

In the above example, we define a function named interestinterest that takes three parameters pp, rr, and tt. The default value of rr is 55 and the default value of tt is 11. We then call the function with one argument 10001000. The function prints 50.050.0 to the console. We then call the function with two arguments 10001000 and 1010. The function prints 100.0100.0 to the console. We then call the function with three arguments 10001000, 1010, and 22. The function prints 200.0200.0 to the console. In this function, if you don’t pass the value for rr and tt, the default values are used.

Positional Arguments

Positional arguments are the arguments that are passed to a function or method without a keyword. In other words, positional arguments are the arguments that are passed to a function or method without a keyword.

Syntax
function_name(value1, value2, value3, ...)
Syntax
function_name(value1, value2, value3, ...)

In the above syntax, value1value1, value2value2, value3value3, and so on are the values passed to the function or method call.

Let’s see an example of a function with positional arguments.

positional-arguments.py
def add(x, y):
    """Adds two numbers"""
    print(x + y)
 
add(5, 10)
positional-arguments.py
def add(x, y):
    """Adds two numbers"""
    print(x + y)
 
add(5, 10)

Output:

command
C:\Users\Your Name> python positional-arguments.py
15
command
C:\Users\Your Name> python positional-arguments.py
15

In the above example, we define a function named addadd that takes two parameters xx and yy. We then call the function with two positional arguments 55 and 1010. The function prints 1515 to the console.

Another example of a function with positional arguments.

positional-arguments.py
def interest(p, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, 5, 1)
interest(1000, 10, 2)
interest(1000, 10, 6)
positional-arguments.py
def interest(p, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, 5, 1)
interest(1000, 10, 2)
interest(1000, 10, 6)

Output:

command
C:\Users\Your Name> python positional-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0
command
C:\Users\Your Name> python positional-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0

In this example, we are calling the function with positional arguments. We are passing the arguments in different orders. In the positional arguments, you have to pass the arguments in the same order as they are defined in the function definition but in the keyword arguments, you can pass the arguments in any order.

Keyword Arguments

Keyword arguments are the arguments that are passed to a function or method with a keyword. In other words, keyword arguments are the arguments that are passed to a function or method with a keyword.

Syntax
function_name(argument1=value1, argument2=value2, argument3=value3, ...)
Syntax
function_name(argument1=value1, argument2=value2, argument3=value3, ...)

In the above syntax, argument1argument1, argument2argument2, argument3argument3, and so on are the arguments passed to the function or method call. value1value1, value2value2, value3value3, and so on are the values passed to the function or method call.

Let’s see an example of a function with keyword arguments.

keyword-arguments.py
def add(x, y):
    """Adds two numbers"""
    print(x + y)
 
add(x=5, y=10)
keyword-arguments.py
def add(x, y):
    """Adds two numbers"""
    print(x + y)
 
add(x=5, y=10)

Output:

command
C:\Users\Your Name> python keyword-arguments.py
15
command
C:\Users\Your Name> python keyword-arguments.py
15

In the above example, we define a function named addadd that takes two parameters xx and yy. We then call the function with two keyword arguments x=5x=5 and y=10y=10. The function prints 1515 to the console.

Another example of a function with keyword arguments.

keyword-arguments.py
def interest(p, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(p=1000, r=5, t=1)
interest(t=2, p=1000, r=10)
interest(r=10, t=2, p=1000)
keyword-arguments.py
def interest(p, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(p=1000, r=5, t=1)
interest(t=2, p=1000, r=10)
interest(r=10, t=2, p=1000)

Output:

command
C:\Users\Your Name> python keyword-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0
command
C:\Users\Your Name> python keyword-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0

In this example, we are calling the function with keyword arguments. We are passing the arguments in different orders. In the positional arguments, you have to pass the arguments in the same order as they are defined in the function definition but in the keyword arguments, you can pass the arguments in any order.

Keyword Arguments vs Positional Arguments

Let’s see the difference between keyword arguments and positional arguments.

Keyword ArgumentsPositional Arguments
Keyword arguments are the arguments that are passed to a function or method with a keyword.Positional arguments are the arguments that are passed to a function or method without a keyword.
You can pass the keyword arguments in any order.You have to pass the positional arguments in the same order as they are defined in the function definition.
You cannot pass the positional arguments after the keyword arguments.You can pass the positional arguments after the keyword arguments.
You can call a function with positional arguments and keyword arguments.You can call a function with positional arguments and keyword arguments. But you have to pass the positional arguments before the keyword arguments.

Positional Only Arguments

In Python, you can define a function with positional only arguments. Positional only arguments are the arguments that can be passed to a function or method only without a keyword. In other words, positional only arguments are the arguments that can be passed to a function or method only without a keyword.

Syntax
def function_name(argument1, argument2, argument3, /):
    """Docstring"""
    # Function body
Syntax
def function_name(argument1, argument2, argument3, /):
    """Docstring"""
    # Function body

In the above syntax, argument1argument1, argument2argument2, argument3argument3, and so on are the positional only arguments passed to the function or method call. You have to pass the positional only arguments without a keyword. You cannot pass the positional only arguments with a keyword.

Let’s see an example of a function with positional only arguments.

positional-only-arguments.py
def add(x, y, /):
    """Adds two numbers"""
    print(x + y)
 
add(5, 10)
positional-only-arguments.py
def add(x, y, /):
    """Adds two numbers"""
    print(x + y)
 
add(5, 10)

Output:

command
C:\Users\Your Name> python positional-only-arguments.py
15
command
C:\Users\Your Name> python positional-only-arguments.py
15

In the above example, we define a function named addadd that takes two positional only arguments xx and yy. We then call the function with two positional arguments 55 and 1010. The function prints 1515 to the console.

Another example of a function with positional only arguments.

positional-only-arguments.py
def interest(p, /, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, 5, 1)
interest(1000, r=10, t=2)
interest(1000, 10, t=2)
positional-only-arguments.py
def interest(p, /, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, 5, 1)
interest(1000, r=10, t=2)
interest(1000, 10, t=2)

Output:

command
C:\Users\Your Name> python positional-only-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0
command
C:\Users\Your Name> python positional-only-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0

In this example, we declare pp as a positional only argument and rr and tt as keyword arguments. We are calling the function with positional arguments. We are passing the arguments in different orders. In the positional arguments, you have to pass the arguments in the same order as they are defined in the function definition but in the keyword arguments, you can pass the arguments in any order.

Keyword Only Arguments

In Python, you can define a function with keyword only arguments. Keyword only arguments are the arguments that can be passed to a function or method only with a keyword. In other words, keyword only arguments are the arguments that can be passed to a function or method only with a keyword.

Syntax
def function_name(*, argument1, argument2, argument3, ...):
    """Docstring"""
    # Function body
Syntax
def function_name(*, argument1, argument2, argument3, ...):
    """Docstring"""
    # Function body

In the above syntax, argument1argument1, argument2argument2, argument3argument3, and so on are the keyword only arguments passed to the function or method call. You have to pass the keyword only arguments with a keyword. You cannot pass the keyword only arguments without a keyword.

Let’s see an example of a function with keyword only arguments.

keyword-only-arguments.py
def add(*, x, y):
    """Adds two numbers"""
    print(x + y)
 
add(x=5, y=10)
keyword-only-arguments.py
def add(*, x, y):
    """Adds two numbers"""
    print(x + y)
 
add(x=5, y=10)

Output:

command
C:\Users\Your Name> python keyword-only-arguments.py
15
command
C:\Users\Your Name> python keyword-only-arguments.py
15

In the above example, we define a function named addadd that takes two keyword only arguments xx and yy. We then call the function with two keyword arguments x=5x=5 and y=10y=10. The function prints 1515 to the console.

Another example of a function with keyword only arguments.

keyword-only-arguments.py
def interest(p, *, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, r=5, t=1)
interest(1000, t=2, r=10)
interest(1000, r=10, t=2)
keyword-only-arguments.py
def interest(p, *, r, t):
    """Calculates the simple interest"""
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, r=5, t=1)
interest(1000, t=2, r=10)
interest(1000, r=10, t=2)

Output:

command
C:\Users\Your Name> python keyword-only-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0
command
C:\Users\Your Name> python keyword-only-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0

In this example, we are calling the function with keyword only arguments. We are passing the arguments in different orders. In the positional arguments, you have to pass the arguments in the same order as they are defined in the function definition but in the keyword arguments, you can pass the arguments in any order.

Arbitrary Arguments

In Python, you can define a function with arbitrary arguments. Arbitrary arguments are the arguments that can be passed to a function or method with any number of arguments. In other words, arbitrary arguments are the arguments that can be passed to a function or method with any number of arguments.

There are two types of arbitrary arguments in Python. They are:

  • Arbitrary Positional Arguments
  • Arbitrary Keyword Arguments

Arbitrary Positional Arguments

In Python, you can define a function with arbitrary positional arguments. Arbitrary positional arguments are the positional arguments that can be passed to a function or method with any number of arguments. In other words, arbitrary positional arguments are the positional arguments that can be passed to a function or method with any number of arguments.

Syntax
def function_name(*args):
    """Docstring"""
    # Function body
Syntax
def function_name(*args):
    """Docstring"""
    # Function body

In the above syntax, argsargs is the arbitrary positional arguments passed to the function or method call. You can pass any number of arguments to the function or method.

Let’s see an example of a function with arbitrary positional arguments.

arbitrary-positional-arguments.py
def add(*args):
    """Adds two numbers"""
    result = 0
    for num in args:
        result += num
    print(result)
 
add(5, 10)
add(5, 10, 15)
add(5, 10, 15, 20)
arbitrary-positional-arguments.py
def add(*args):
    """Adds two numbers"""
    result = 0
    for num in args:
        result += num
    print(result)
 
add(5, 10)
add(5, 10, 15)
add(5, 10, 15, 20)

Output:

command
C:\Users\Your Name> python arbitrary-positional-arguments.py
15
30
50
command
C:\Users\Your Name> python arbitrary-positional-arguments.py
15
30
50

In the above example, we define a function named addadd that takes arbitrary positional arguments argsargs. We then call the function with two arguments 55 and 1010. The function prints 1515 to the console. We then call the function with three arguments 55, 1010, and 1515. The function prints 3030 to the console. We then call the function with four arguments 55, 1010, 1515, and 2020. The function prints 5050 to the console.

Another example of a function with arbitrary positional arguments.

arbitrary-positional-arguments.py
def interest(*args):
    """Calculates the simple interest"""
    p, r, t = args
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, 5, 1)
interest(1000, 10, 2)
interest(1000, 10, 6)
arbitrary-positional-arguments.py
def interest(*args):
    """Calculates the simple interest"""
    p, r, t = args
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(1000, 5, 1)
interest(1000, 10, 2)
interest(1000, 10, 6)

Output:

command
C:\Users\Your Name> python arbitrary-positional-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0
command
C:\Users\Your Name> python arbitrary-positional-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0

In this example, we are calling the function with arbitrary positional arguments. We are passing the arguments in different orders. In the positional arguments, you have to pass the arguments in the same order as they are defined in the function definition. Here, we are unpacking the arguments into three variables pp, rr, and tt. We are then using these variables to calculate the simple interest.

Arbitrary Keyword Arguments

In Python, you can define a function with arbitrary keyword arguments. Arbitrary keyword arguments are the keyword arguments that can be passed to a function or method with any number of arguments. In other words, arbitrary keyword arguments are the keyword arguments that can be passed to a function or method with any number of arguments.

Syntax
def function_name(**kwargs):
    """Docstring"""
    # Function body
Syntax
def function_name(**kwargs):
    """Docstring"""
    # Function body

In the above syntax, kwargskwargs is the arbitrary keyword arguments passed to the function or method call. You can pass any number of arguments to the function or method.

Let’s see an example of a function with arbitrary keyword arguments.

arbitrary-keyword-arguments.py
def add(**kwargs):
    """Adds two numbers"""
    result = 0
    for key, value in kwargs.items():
        result += value
    print(result)
 
add(x=5, y=10)
add(x=5, y=10, z=15)
add(x=5, y=10, z=15, a=20)
arbitrary-keyword-arguments.py
def add(**kwargs):
    """Adds two numbers"""
    result = 0
    for key, value in kwargs.items():
        result += value
    print(result)
 
add(x=5, y=10)
add(x=5, y=10, z=15)
add(x=5, y=10, z=15, a=20)

Output:

command
C:\Users\Your Name> python arbitrary-keyword-arguments.py
15
30
50
command
C:\Users\Your Name> python arbitrary-keyword-arguments.py
15
30
50

In the above example, we define a function named addadd that takes arbitrary keyword arguments kwargskwargs. We then call the function with two arguments x=5x=5 and y=10y=10. The function prints 1515 to the console. We then call the function with three arguments x=5x=5, y=10y=10, and z=15z=15. The function prints 3030 to the console. We then call the function with four arguments x=5x=5, y=10y=10, z=15z=15, and a=20a=20. The function prints 5050 to the console.

Another example of a function with arbitrary keyword arguments.

arbitrary-keyword-arguments.py
def interest(**kwargs):
    """Calculates the simple interest"""
    p = kwargs["p"]
    r = kwargs["r"]
    t = kwargs["t"]
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(p=1000, r=5, t=1)
interest(p=1000, r=10, t=2)
interest(p=1000, r=10, t=6)
arbitrary-keyword-arguments.py
def interest(**kwargs):
    """Calculates the simple interest"""
    p = kwargs["p"]
    r = kwargs["r"]
    t = kwargs["t"]
    i = (p * r * t) / 100
    print(f"Simple interest is {i}")
 
interest(p=1000, r=5, t=1)
interest(p=1000, r=10, t=2)
interest(p=1000, r=10, t=6)

Output:

command
C:\Users\Your Name> python arbitrary-keyword-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0
command
C:\Users\Your Name> python arbitrary-keyword-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0

In this example, we are calling the function with arbitrary keyword arguments. We are passing the arguments in different orders. In the positional arguments, you have to pass the arguments in the same order as they are defined in the function definition. Here, we are accessing the arguments using the keys pp, rr, and tt. We are then using these variables to calculate the simple interest.

Another example of a function with arbitrary keyword arguments.

arbitrary-keyword-arguments.py
def intro(name, age, **kwargs):
    """Prints the name and age"""
    print(f"Name: {name}")
    print(f"Age: {age}")
    print("Other information:")
    for key, value in kwargs.items():
        print(f"{key}: {value}")
    
intro("John", 25, city="New York", country="USA")
print("-------------------------")
intro("John", 25, city="New York", country="USA", phone="1234567890")
print("-------------------------")
intro("John", 25, city="New York", country="USA", phone="1234567890", email="john@gmail.com")
arbitrary-keyword-arguments.py
def intro(name, age, **kwargs):
    """Prints the name and age"""
    print(f"Name: {name}")
    print(f"Age: {age}")
    print("Other information:")
    for key, value in kwargs.items():
        print(f"{key}: {value}")
    
intro("John", 25, city="New York", country="USA")
print("-------------------------")
intro("John", 25, city="New York", country="USA", phone="1234567890")
print("-------------------------")
intro("John", 25, city="New York", country="USA", phone="1234567890", email="john@gmail.com")

Output:

command
C:\Users\Your Name> python arbitrary-keyword-arguments.py
Name: John
Age: 25
Other information:
city: New York
country: USA
-------------------------
Name: John
Age: 25
Other information:
city: New York
country: USA
phone: 1234567890
-------------------------
Name: John
Age: 25
Other information:
city: New York
country: USA
phone: 1234567890
email: john@gmail.com
command
C:\Users\Your Name> python arbitrary-keyword-arguments.py
Name: John
Age: 25
Other information:
city: New York
country: USA
-------------------------
Name: John
Age: 25
Other information:
city: New York
country: USA
phone: 1234567890
-------------------------
Name: John
Age: 25
Other information:
city: New York
country: USA
phone: 1234567890
email: john@gmail.com

Conclusion

In this tutorial, we learned about the different types of arguments in Python. We also learned how to use them in our Python programs. There are three types of arguments in Python. They are positional arguments, keyword arguments, and default arguments. We also learned about positional only arguments, keyword only arguments, and arbitrary arguments.

As you explore Python, experiment with different types of functions and discover how they contribute to code readability, flexibility, and efficiency. For more insights and practical examples, check out our tutorials on Python Central Hub!

Was this page helpful?

Let us know how we did