Different Types of Arguments
Python offers several ways to pass arguments into a function, giving you flexibility in how a function can be called. Understanding each type helps you write functions that are both powerful and easy to use.
Positional Arguments
Values are matched to parameters based purely on their order in the function call.
def divide(a, b):
return a / b
divide(10, 2) # a = 10, b = 2
Keyword Arguments
Values are passed using the parameter's name, so the order in which you pass them no longer matters.
divide(b=2, a=10) # same result as above
Default Arguments
As covered earlier, these use a fallback value when no argument is explicitly supplied for that parameter.
Arbitrary Arguments: *args and **kwargs
*args lets a function accept any number of positional arguments as a tuple, while **kwargs lets it accept any number of keyword arguments as a dictionary.
def total(*args):
return sum(args)
total(1, 2, 3, 4) # 10
def show_details(**kwargs):
for key, value in kwargs.items():
print(key, ":", value)
show_details(name="Abhay", course="Data Science")
Coming Up Next
With argument types covered, the next section looks at how variables behave differently depending on where they are defined, starting with global variables.