Types of Arguments in Python
Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma. There are many types of arguments in Python .
In this example, we will create a simple function in Python to check whether the number passed as an argument to the function positive, negative or zero.
def checkSign(a):
if a > 0:
print("positive")
elif a < 0:
print("negative")
else:
print("zero")
# call the function
checkSign(10)
checkSign(-5)
checkSign(0)
Output
positive negative zero
Types of Arguments in Python
Python provides various argument types to pass values to functions, making them more flexible and reusable. Understanding these types can simplify your code and improve readability. we have the following function argument types in Python:
- Default argument
- Keyword arguments (named arguments)
- Positional arguments
- Arbitrary arguments (variable-length arguments *args and **kwargs)
- Lambda Function Arguments
Let’s discuss each type in detail.
Default Arguments
Default Arguments is a parameter that have a predefined value if no value is passed during the function call. This following example illustrates Default arguments to write functions in Python.
def calculate_area(length, width=5):
area = length * width
print(f"Area of rectangle: {area}")
# Driver code (We call calculate_area() with only
# the length argument)
calculate_area(10)
# We can also pass a custom width
calculate_area(10, 8)
Output
Area of rectangle: 50 Area of rectangle: 80
Keyword arguments
Keyword arguments are passed by naming the parameters when calling the function. This lets us provide the arguments in any order, making the code more readable and flexible.
def fun(name, course):
print(name,course)
# Positional arguments
fun(course="DSA",name="gfg")
fun(name="gfg",course="DSA")
Output
gfg DSA gfg DSA
Positional arguments
Positional arguments in Python are values that we pass to a function in a specific order. The order in which we pass the arguments matters.
This following example illustrates Positional arguments to write functions in Python.
def productInfo(product, price):
print("Product:", product)
print("Price: $", price)
# Correct order of arguments
print("Case-1:")
productInfo("Laptop", 1200)
# Incorrect order of arguments
print("\nCase-2:")
productInfo(1200, "Laptop")
Output
Case-1: Product: Laptop Price: $ 1200 Case-2: Product: 1200 Price: $ Laptop
Arbitrary arguments (variable-length arguments *args and **kwargs)
In Python Arbitrary arguments allow us to pass a number of arguments to a function. This is useful when we don't know in advance how many arguments we will need to pass. There are two types of arbitrary arguments:
- *args in Python (Non-Keyword Arguments): Collects extra positional arguments passed to a function into a tuple.
- **kwargs in Python (Keyword Arguments): Collects extra keyword arguments passed to a function into a dictionary.
Example 1 : Handling Variable Arguments in Functions
# Python program to illustrate
# *args for variable number of arguments
def myFun(*argv):
for arg in argv:
print(arg)
# Driver code with different arguments
myFun('Python', 'is', 'amazing')
Output
Python is amazing
Example 2: Handling Arbitrary Keyword in Functions
# Python program to illustrate
# **kwargs for variable number of keyword arguments
def fun(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
# Driver code
fun(course="DSA", platform="GeeksforGeeks", difficulty="easy")
Output
course: DSA platform: GeeksforGeeks difficulty: easy
Lambda Function Arguments
Lambda functions work like regular functions, taking arguments to perform task in one simple expression. we can pass any number of arguments. Here are the common ways to pass arguments in lambda function:
- Single Argument
- Multiple Arguments
Example 1: Passing single argument
# Lambda function with one argument
square = lambda x: x ** 2
print(square(5))
Output
25
Example 2: Passing multiple arguments
# Lambda function with two arguments
add = lambda a, b: a + b
print(add(3, 4))
Output
7