Skip to content

Types of 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, ...)

In the above syntax, argument1, argument2, argument3, 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)

Output:

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

In the above example, we define a function named add that takes two parameters x and y and prints their sum. We then call the function with two arguments 5 and 10. The function prints 15 to the console.

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

  1. Default Arguments
  2. Positional Arguments
  3. Keyword 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

In the above syntax, argument1 is a positional argument and argument2 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)

Output:

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

In the above example, we define a function named add that takes two parameters x and y. The default value of y is 10. We then call the function with one argument 5. The function prints 15 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)

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

In the above example, we define a function named interest that takes three parameters p, r, and t. The default value of r is 5 and the default value of t is 1. We then call the function with one argument 1000. The function prints 50.0 to the console. We then call the function with two arguments 1000 and 10. The function prints 100.0 to the console. We then call the function with three arguments 1000, 10, and 2. The function prints 200.0 to the console. In this function, if you don’t pass the value for r and t, the default values are used.

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, ...)

In the above syntax, value1, value2, value3, 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)

Output:

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

In the above example, we define a function named add that takes two parameters x and y. We then call the function with two positional arguments 5 and 10. The function prints 15 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)

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

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 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, ...)

In the above syntax, argument1, argument2, argument3, and so on are the arguments passed to the function or method call. value1, value2, value3, 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)

Output:

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

In the above example, we define a function named add that takes two parameters x and y. We then call the function with two keyword arguments x=5 and y=10. The function prints 15 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)

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

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.

sketch A default argument is evaluated once, at definition time p5.js
The list in def bad(x, acc=[]) is created when the function is DEFINED, not on each call -- so every call that omits the argument shares one list, and it keeps growing. You can see the accumulation in the function itself: after three calls, bad.__defaults__ holds the filled list. The fix is a None sentinel, which makes a fresh list inside the body where it belongs.

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.

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

In the above syntax, argument1, argument2, argument3, 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)

Output:

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

In the above example, we define a function named add that takes two positional only arguments x and y. We then call the function with two positional arguments 5 and 10. The function prints 15 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)

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

In this example, we declare p as a positional only argument and r and t 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.

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

In the above syntax, argument1, argument2, argument3, 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)

Output:

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

In the above example, we define a function named add that takes two keyword only arguments x and y. We then call the function with two keyword arguments x=5 and y=10. The function prints 15 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)

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

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.

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

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

In the above syntax, args 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)

Output:

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

In the above example, we define a function named add that takes arbitrary positional arguments args. We then call the function with two arguments 5 and 10. The function prints 15 to the console. We then call the function with three arguments 5, 10, and 15. The function prints 30 to the console. We then call the function with four arguments 5, 10, 15, and 20. The function prints 50 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)

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

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 p, r, and t. We are then using these variables to calculate the simple interest.

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

In the above syntax, kwargs 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)

Output:

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

In the above example, we define a function named add that takes arbitrary keyword arguments kwargs. We then call the function with two arguments x=5 and y=10. The function prints 15 to the console. We then call the function with three arguments x=5, y=10, and z=15. The function prints 30 to the console. We then call the function with four arguments x=5, y=10, z=15, and a=20. The function prints 50 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)

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

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 p, r, and t. 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")

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

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!


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading