Python allows you to pass arguments to functions in different ways. Default arguments provide a value automatically when an argument is not supplied, while keyword arguments let you specify which parameter should receive a value.
Default Arguments
A default argument is a parameter that already has a value in the function definition. If you do not provide a value for that parameter when calling the function, Python uses the default value.
def greet(name, message="Hello"):
print(message, name)
greet("Abhishek")Output:
Hello AbhishekHere, message has the default value "Hello". Since no value is provided for it, Python uses the default.
You can also replace the default value by passing your own argument:
def greet(name, message="Hello"):
print(message, name)
greet("Abhishek", "Good Morning")Output:
Good Morning AbhishekDefault arguments are useful when a function has a commonly used value that does not need to be specified every time.
For example:
def calculate_price(price, tax=18):
return price + (price * tax / 100)
print(calculate_price(1000))
print(calculate_price(1000, 5))Output:
1180.0
1050.0In the first call, the default tax of 18 is used. In the second call, 5 replaces the default value.
Keyword Arguments
Keyword arguments are passed using the parameter name followed by = and its value.
def student(name, age):
print("Name:", name)
print("Age:", age)
student(name="Rahul", age=21)Output:
Name: Rahul
Age: 21One advantage of keyword arguments is that the values do not have to be passed in the same order as the parameters.
def student(name, age, course):
print(name, age, course)
student(course="Python", name="Rahul", age=21)Output:
Rahul 21 PythonPython matches each value with its parameter name.
Combining Positional and Keyword Arguments
You can use positional and keyword arguments in the same function call.
def student(name, age, course):
print(name, age, course)
student("Rahul", age=21, course="Python")Output:
Rahul 21 PythonHere, "Rahul" is a positional argument, while age=21 and course="Python" are keyword arguments.
However, positional arguments must come before keyword arguments.
Correct:
student("Rahul", age=21, course="Python")Incorrect:
student(name="Rahul", 21, course="Python")The second example causes a SyntaxError because a positional argument cannot be placed after a keyword argument.
Argument Order
The order of arguments matters when using positional arguments because Python assigns them according to their position.
def display(name, age):
print(name, age)
display("Rahul", 21)Here, "Rahul" goes to name and 21 goes to age.
With keyword arguments, the order can be changed:
display(age=21, name="Rahul")Output:
Rahul 21When combining both types, follow this order:
Positional arguments → Keyword arguments
For example:
def employee(name, department, salary):
print(name, department, salary)
employee("Amit", salary=30000, department="IT")Output:
Amit IT 30000Here, "Amit" is passed positionally, while salary and department are passed using their parameter names.
Using default and keyword arguments makes Python functions more flexible and easier to call, especially when some values are optional or when a function has several parameters.