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.
function_name(argument1, argument2, argument3, ...)
function_name(argument1, argument2, argument3, ...)
In the above syntax, argument1
argument1
, argument2
argument2
, argument3
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.
def add(x, y):
"""Adds two numbers"""
print(x + y)
add(5, 10)
def add(x, y):
"""Adds two numbers"""
print(x + y)
add(5, 10)
Output:
C:\Users\Your Name> python arguments.py
15
C:\Users\Your Name> python arguments.py
15
In the above example, we define a function named add
add
that takes two parameters x
x
and y
y
and prints their sum. We then call the function with two arguments 5
5
and 10
10
. The function prints 15
15
to the console.
Types of Arguments in Python
In Python, there are three types of arguments. They are:
- Default Arguments
- Positional Arguments
- 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.
def function_name(argument1, argument2=default_value):
"""Docstring"""
# Function body
def function_name(argument1, argument2=default_value):
"""Docstring"""
# Function body
In the above syntax, argument1
argument1
is a positional argument and argument2
argument2
is a default argument.
Letβs see an example of a function with a default argument.
def add(x, y=10):
"""Adds two numbers"""
print(x + y)
add(5)
def add(x, y=10):
"""Adds two numbers"""
print(x + y)
add(5)
Output:
C:\Users\Your Name> python default-arguments.py
15
C:\Users\Your Name> python default-arguments.py
15
In the above example, we define a function named add
add
that takes two parameters x
x
and y
y
. The default value of y
y
is 10
10
. We then call the function with one argument 5
5
. The function prints 15
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.
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)
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:
C:\Users\Your Name> python default-arguments.py
Simple interest is 50.0
Simple interest is 100.0
Simple interest is 200.0
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
interest
that takes three parameters p
p
, r
r
, and t
t
. The default value of r
r
is 5
5
and the default value of t
t
is 1
1
. We then call the function with one argument 1000
1000
. The function prints 50.0
50.0
to the console. We then call the function with two arguments 1000
1000
and 10
10
. The function prints 100.0
100.0
to the console. We then call the function with three arguments 1000
1000
, 10
10
, and 2
2
. The function prints 200.0
200.0
to the console. In this function, if you donβt pass the value for r
r
and t
t
, 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.
function_name(value1, value2, value3, ...)
function_name(value1, value2, value3, ...)
In the above syntax, value1
value1
, value2
value2
, value3
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.
def add(x, y):
"""Adds two numbers"""
print(x + y)
add(5, 10)
def add(x, y):
"""Adds two numbers"""
print(x + y)
add(5, 10)
Output:
C:\Users\Your Name> python positional-arguments.py
15
C:\Users\Your Name> python positional-arguments.py
15
In the above example, we define a function named add
add
that takes two parameters x
x
and y
y
. We then call the function with two positional arguments 5
5
and 10
10
. The function prints 15
15
to the console.
Another example of a function with positional arguments.
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)
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:
C:\Users\Your Name> python positional-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0
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.
function_name(argument1=value1, argument2=value2, argument3=value3, ...)
function_name(argument1=value1, argument2=value2, argument3=value3, ...)
In the above syntax, argument1
argument1
, argument2
argument2
, argument3
argument3
, and so on are the arguments passed to the function or method call. value1
value1
, value2
value2
, value3
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.
def add(x, y):
"""Adds two numbers"""
print(x + y)
add(x=5, y=10)
def add(x, y):
"""Adds two numbers"""
print(x + y)
add(x=5, y=10)
Output:
C:\Users\Your Name> python keyword-arguments.py
15
C:\Users\Your Name> python keyword-arguments.py
15
In the above example, we define a function named add
add
that takes two parameters x
x
and y
y
. We then call the function with two keyword arguments x=5
x=5
and y=10
y=10
. The function prints 15
15
to the console.
Another example of a function with keyword arguments.
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)
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:
C:\Users\Your Name> python keyword-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0
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 Arguments | Positional 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.
def function_name(argument1, argument2, argument3, /):
"""Docstring"""
# Function body
def function_name(argument1, argument2, argument3, /):
"""Docstring"""
# Function body
In the above syntax, argument1
argument1
, argument2
argument2
, argument3
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.
def add(x, y, /):
"""Adds two numbers"""
print(x + y)
add(5, 10)
def add(x, y, /):
"""Adds two numbers"""
print(x + y)
add(5, 10)
Output:
C:\Users\Your Name> python positional-only-arguments.py
15
C:\Users\Your Name> python positional-only-arguments.py
15
In the above example, we define a function named add
add
that takes two positional only arguments x
x
and y
y
. We then call the function with two positional arguments 5
5
and 10
10
. The function prints 15
15
to the console.
Another example of a function with positional only arguments.
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)
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:
C:\Users\Your Name> python positional-only-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0
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
p
as a positional only argument and r
r
and t
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.
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.
def function_name(*, argument1, argument2, argument3, ...):
"""Docstring"""
# Function body
def function_name(*, argument1, argument2, argument3, ...):
"""Docstring"""
# Function body
In the above syntax, argument1
argument1
, argument2
argument2
, argument3
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.
def add(*, x, y):
"""Adds two numbers"""
print(x + y)
add(x=5, y=10)
def add(*, x, y):
"""Adds two numbers"""
print(x + y)
add(x=5, y=10)
Output:
C:\Users\Your Name> python keyword-only-arguments.py
15
C:\Users\Your Name> python keyword-only-arguments.py
15
In the above example, we define a function named add
add
that takes two keyword only arguments x
x
and y
y
. We then call the function with two keyword arguments x=5
x=5
and y=10
y=10
. The function prints 15
15
to the console.
Another example of a function with keyword only arguments.
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)
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:
C:\Users\Your Name> python keyword-only-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 200.0
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.
def function_name(*args):
"""Docstring"""
# Function body
def function_name(*args):
"""Docstring"""
# Function body
In the above syntax, args
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.
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)
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:
C:\Users\Your Name> python arbitrary-positional-arguments.py
15
30
50
C:\Users\Your Name> python arbitrary-positional-arguments.py
15
30
50
In the above example, we define a function named add
add
that takes arbitrary positional arguments args
args
. We then call the function with two arguments 5
5
and 10
10
. The function prints 15
15
to the console. We then call the function with three arguments 5
5
, 10
10
, and 15
15
. The function prints 30
30
to the console. We then call the function with four arguments 5
5
, 10
10
, 15
15
, and 20
20
. The function prints 50
50
to the console.
Another example of a function with arbitrary positional arguments.
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)
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:
C:\Users\Your Name> python arbitrary-positional-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0
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
p
, r
r
, and t
t
. 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.
def function_name(**kwargs):
"""Docstring"""
# Function body
def function_name(**kwargs):
"""Docstring"""
# Function body
In the above syntax, kwargs
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.
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)
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:
C:\Users\Your Name> python arbitrary-keyword-arguments.py
15
30
50
C:\Users\Your Name> python arbitrary-keyword-arguments.py
15
30
50
In the above example, we define a function named add
add
that takes arbitrary keyword arguments kwargs
kwargs
. We then call the function with two arguments x=5
x=5
and y=10
y=10
. The function prints 15
15
to the console. We then call the function with three arguments x=5
x=5
, y=10
y=10
, and z=15
z=15
. The function prints 30
30
to the console. We then call the function with four arguments x=5
x=5
, y=10
y=10
, z=15
z=15
, and a=20
a=20
. The function prints 50
50
to the console.
Another example of a function with arbitrary keyword arguments.
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)
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:
C:\Users\Your Name> python arbitrary-keyword-arguments.py
Simple interest is 50.0
Simple interest is 200.0
Simple interest is 600.0
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
p
, r
r
, and t
t
. We are then using these variables to calculate the simple interest.
Another example of a function with arbitrary keyword arguments.
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")
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:
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
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