Function arguments are the values that are passed to a function when the function is called. Arguments allow the same function to work with different data.
An argument is a value passed to a function when calling it.
def greet(name):
print("Hello", name)
greet("Amit")
Here, name is the parameter and "Amit" is the argument.
| Term | Meaning |
|---|---|
| Parameter | Variable written in the function definition |
| Argument | Actual value passed to the function |
def add(a, b):
return a + b
add(10, 20)
Here a and b are parameters, while 10 and 20 are arguments.
Positional arguments are matched with parameters according to their position.
def student(name, age):
print("Name:", name)
print("Age:", age)
student("Rahul", 21)
The order of positional arguments is important.
def student(name, age):
print(name)
print(age)
student("Amit", 20)
If the values are passed in the wrong order, the function may receive unexpected values.
def add(a, b, c):
return a + b + c
result = add(10, 20, 30)
print(result)
Keyword arguments are passed using the parameter name.
def student(name, age):
print("Name:", name)
print("Age:", age)
student(age=21, name="Priya")
The order of keyword arguments does not need to match the parameter order.
You can combine positional and keyword arguments, but positional arguments must come before keyword arguments.
def student(name, age, course):
print(name, age, course)
student("Amit", age=20, course="Python")
A positional argument cannot normally be placed after a keyword argument.
def student(name, age):
print(name, age)
student(name="Amit", 20)
A parameter can have a default value. If the caller does not provide a value, Python uses the default.
def greet(name="Student"):
print("Hello", name)
greet()
greet("Amit")
def student(name="Unknown", course="Python"):
print("Name:", name)
print("Course:", course)
student()
A default value can be replaced by providing an argument.
def greet(name="Student"):
print("Hello", name)
greet("Rahul")
A parameter without a default value is required unless the function is designed to receive it through another mechanism.
def add(a, b):
return a + b
print(add(10, 20))
Calling add() without the required arguments causes a TypeError.
def add(a, b):
return a + b
add(10)
Use *args when you want a function to accept a variable number of positional arguments.
def add(*numbers):
print(numbers)
add(10, 20, 30, 40)
Inside the function, args is a tuple containing the positional arguments.
def add(*numbers):
total = 0
for number in numbers:
total += number
return total
print(add(10, 20))
print(add(10, 20, 30))
print(add(1, 2, 3, 4, 5))
Use **kwargs when you want a function to accept a variable number of keyword arguments.
def student(**details):
print(details)
student(name="Amit", age=20, course="Python")
Inside the function, kwargs is a dictionary.
def student(**details):
for key, value in details.items():
print(key, ":", value)
student(name="Rahul", age=21, city="Patna")
A regular parameter can be used before *args.
def student(name, *subjects):
print("Name:", name)
for subject in subjects:
print("Subject:", subject)
student("Amit", "Python", "SQL", "HTML")
def student(*subjects, **details):
print("Subjects:", subjects)
print("Details:", details)
student(
"Python",
"SQL",
name="Amit",
age=20
)
The * operator can unpack the elements of a list or tuple into positional arguments.
def add(a, b, c):
return a + b + c
numbers = [10, 20, 30]
print(add(*numbers))
The ** operator can unpack a dictionary into keyword arguments.
def student(name, age):
print("Name:", name)
print("Age:", age)
data = {
"name": "Priya",
"age": 22
}
student(**data)
Parameters placed after * must be passed using their parameter names.
def student(name, *, age):
print(name, age)
student("Amit", age=20)
Here, age is a keyword-only argument.
Parameters placed before / can be defined as positional-only parameters.
def add(a, b, /):
return a + b
print(add(10, 20))
The parameters before / cannot be passed using keyword syntax.
A function definition can use different types of parameters, but they must follow Python's parameter ordering rules.
def example(a, b=10, *args, **kwargs):
print(a)
print(b)
print(args)
print(kwargs)
example(1, 20, 30, 40, name="Amit")
def calculate_total(*prices):
total = 0
for price in prices:
total += price
return total
print("Total:", calculate_total(100, 200, 150))
Question: Which syntax is used to accept a variable number of positional arguments?