Python functions can have:
- No arguments
- One argument
- Multiple arguments
- Variable-length arguments
- Arbitrary keyword arguments (
**kwargs
)
Key takeaways:
Positional and keyword arguments are the two sorts of arguments that Python functions can take in varying numbers.
Positional arguments require the arguments to be passed in a specific order, and are accessed using
*args
in the function.Keyword arguments are passed with a keyword and value (e.g.,
keyword=value
) and can be accessed using**kwargs
.
A variable, value, or object sent as input to a function or method is called an argument. These arguments are passed to a function through parameters, which are placeholders defined in the function signature.
In Python, a function can accept a variable number of arguments in two primary ways:
Positional arguments: These are arguments that must be passed to a function in the correct order, matching the parameters defined in the function signature. The order in which you pass the arguments is important.
Keyword arguments: These are arguments passed to the function by explicitly specifying the parameter name along with its value. The order of keyword arguments doesn’t matter, but each keyword must correspond to a named parameter in the function.
Arguments that must be presented in the correct order or position are known as positional arguments. When the function is invoked, the first positional argument must always be listed first. It is necessary to list the second positional argument second, the third positional argument third, and so on.
def positional_args(*argv):for arg in argv:print (arg)positional_args ('Hello', 'Welcome', 'to', 'GITAM')
Line 1: The code defines a function positional_args
that accepts a variable number of arguments using the *argv
syntax, which collects all passed arguments into a tuple called argv
.
Lines 2–3: Inside the function, a for
loop iterates through each element in the argv
tuple, and prints each argument using the print()
function.
Line 5: When the function is called with the arguments 'Hello'
, 'Welcome'
, 'to'
, and 'GITAM'
, it will print each of these arguments on a new line.
def positional_args (arg1, *argv):print ("First argument :", arg1)for arg in argv:print("Next argument through *argv :", arg)positional_args ('Hello', 'Welcome', 'to', 'GITAM')
Line 1: The function positional_args
is defined to accept a mix of a single positional argument (arg1
) and a variable number of additional positional arguments (*argv
). The first argument passed to the function is assigned to arg1
, while any remaining arguments are packed into a tuple named argv
using the *argv
syntax.
Lines 2–4: Inside the function, the first print()
statement displays the value of arg1
, which is the first argument passed to the function. Then, a for
loop iterates over the tuple argv
, printing each of the remaining arguments with the prefix "Next argument through *argv :"
.
Line 6: When calling positional_args
with the arguments 'Hello'
, 'Welcome'
, 'to'
, and 'GITAM'
, the function will:
Print the first argument (arg1
), which is 'Hello'
.
Loop through the remaining arguments ('Welcome'
, 'to'
, and 'GITAM'
) and print them one by one.
A keyword argument is an argument passed to a function with a name-value pair, allowing arguments to be provided in any order. The typical form is: function(keyword = value)
. Where value is the value or object supplied as that keyword, function is the name of the function, and keyword is the keyword argument.
def key_args(**kwargs):for key, value in kwargs.items():print ("%s == %s" %(key, value))key_args (first ='GITAM', mid ='for', last='GITAM')
Line 1: The function key_args
uses the **kwargs
syntax to accept a variable number of keyword arguments. These arguments are passed as key-value pairs and are collected into a dictionary called kwargs
.
Lines 2–4: Inside the function, a for
loop iterates through the kwargs
dictionary using the .items()
method, which provides both the key and its corresponding value. The print()
function is then used to format and display the key-value pairs in the form key == value
.
Line 5: When calling key_args
with the arguments first='GITAM'
, mid='for'
, and last='GITAM'
, these key-value pairs are passed as keyword arguments to the function. The function will iterate through the dictionary kwargs
, printing each key and value pair in the format "key == value"
.
def key_args (arg1, **kwargs):for key, value in kwargs.items():print ("%s == %s" %(key, value))key_args ("Hi", first ='GITAM', mid ='for', last='GITAM')
Line 1: The function key_args
is defined to accept a regular positional argument (arg1
) followed by a variable number of keyword arguments, which are captured using the **kwargs
syntax. The **kwargs
syntax collects the keyword arguments passed to the function into a dictionary, where the keys are the argument names, and the values are the corresponding argument values.
Lines 2–3: Within the function, a for
loop iterates over the kwargs
dictionary using the .items()
method, which returns each key-value pair. The print()
function is used to display each key and its associated value in the format key == value
.
Line 5: When calling key_args
with the arguments "Hi"
, first='GITAM'
, mid='for'
, and last='GITAM'
, the first argument "Hi"
is assigned to arg1
, while the remaining keyword arguments (first
, mid
, and last
) are captured by **kwargs
.
*args
vs. **kwargs
In Python, *args
and **kwargs
are used in function definitions to handle a variable number of arguments flexibly.
*args
(Non-keyword arguments):
*args
allows a function to accept any number of positional (unnamed) arguments.
The arguments are collected into a tuple inside the function.
**kwargs
(keyword arguments):
**kwargs
allows a function to accept any number of keyword (named) arguments.
The arguments are stored as a dictionary inside the function.
def var_arg(*args,**kwargs):print("args: ", args)print("kwargs: ", kwargs)var_arg ('GITAM','for','GITAM',first="GITAM",mid="for",last="GITAM")
Line 1: The function var_arg(*args, **kwargs)
demonstrates the use of both positional arguments (*args
) and keyword arguments (**kwargs
) in a single function. The function accepts any number of positional arguments, which are stored as a tuple in the variable args
, and any number of keyword arguments, which are stored as a dictionary in the variable kwargs
.
Lines 2–5: When the function is called with var_arg('GITAM', 'for', 'GITAM', first="GITAM", mid="for", last="GITAM")
, the positional arguments 'GITAM', 'for', 'GITAM'
are collected in args
, and the keyword arguments first="GITAM", mid="for", last="GITAM"
are collected in kwargs
. The function then prints the args
tuple and the kwargs
dictionary.
*args
and **kwargs
Here is a summary of the key differences:
Feature |
(Positional) |
(Keyword) |
Type | Tuple | Dictionary |
Input Format |
|
|
Use Case | When the number of positional arguments is unknown | When the number of named arguments is unknown |
Python provides flexibility in handling functions with varying numbers of arguments through the use of positional and keyword arguments. This allows for more versatile and dynamic function calls.
Master the art of writing clean code with “Clean Code in Python.” From Python basics to advanced concepts like decorators and async programming, gain the skills to build efficient, maintainable applications.
Haven’t found what you were looking for? Contact Us