Arguments and parameters in Python
Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two.
Parameters:
While defining a function, we put some referral names that are taken as input for the function. These referral names are known as parameters. This can be explained by the following example:
def add(integer1, integer2):
print(integer1 +integer2)
Here, integer1 and integer2 are the parameters given to the function add()
Arguments:
Arguments are the inputs that are passed into a function when it is called. This can be easily explained by the following example.
def add(integer1, integer2):
print(integer1 +integer2)
add(3, 4)
Here, integer1 and integer2 are the parameters, and 3 and 4 are the arguments.
SYNTAX:
def function(parameter1, parameter2):
pass
function(argument1, argument2)
Example:
def add(parameter1, parameter2):
print(parameter1 + parameter2)
add("concat", "enation")
OUTPUT :
concatenation
Process finished with exit code 0
There are five types of parameters and two types of arguments.
Types of arguments:
The two types of arguments are:
- Keyword arguments
- Positional arguments
Keyword arguments:
The arguments that are inputted by specifying their respective parameter names are known as keyword arguments.
Syntax :
def function(parameter1, parameter2):
pass
function(parameter1=argument1, parameter2=argument2)
( or )
def function(parameter1, parameter2):
pass
function(parameter2=argument1, parameter1=argument2)
This can be easily understood by the following example :
def employee(name, address):
print(f" {name} {address} ")
employee(name=" Rishika ", address=" Warangal ")
OUTPUT
Rishika Warangal
Process finished with exit code 0
(or)
def employee(name, address):
print(f" {name} {address} ")
employee(address=" Warangal ", name=" Rishika ")
OUTPUT
Rishika Warangal
Process finished with exit code 0
Example:
def division(dividend, divisor):
print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")
division(dividend=10, divisor=2)
OUTPUT:
The quotient of 10 divided by 2 is equal to = 5.0
Process finished with exit code 0
(or)
def division(dividend, divisor):
print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")
division(dividend=10, divisor=2)
OUTPUT:
The quotient of 10 divided by 2 is equal to = 5.0
Process finished with exit code 0
Positional arguments:
These arguments are inputted according to their positions. No assignment operators are used to input these arguments.
SYNTAX:
def function(parameter1, parameter2):
pass
function(argument1, argument2)
This can be explained by the following example.
def employee(name, address):
print(f" {name} {address} ")
employee(" Rishika ", " Warangal ")
OUTPUT
Rishika Warangal
Process finished with exit code 0
(and)
def employee(name, address):
print(f" {name} {address} ")
employee(" Warangal ", " Rishika ")
OUTPUT
Warangal Rishika
Process finished with exit code 0
Example:
def division(dividend, divisor):
print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")
division(10, 2)
OUTPUT:
The quotient of 10 divided by 2 is equal to = 5.0
Process finished with exit code 0
(or)
def division(dividend, divisor):
print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")
division(2, 10)
OUTPUT:
The quotient of 10 divided by 2 is equal to = 2.0
Process finished with exit code 0
Types of parameters:
The five types of parameters are:
- Positional or Keyword Parameters
- Positional only Parameters
- Keyword only parameters
- Var positional parameters
- Var keyword parameters
Positional or Keyword parameters:
These indicate the functions which have both positional as well as keyword parameters. The keyword parameters are declared after all the positional arguments.
Example:
def function( a , b , c = 29 ):
print(f" a = { a } , b = { b } , c = { c } " )
function( 10 , 20 )
function( 10 , 20 , c = 30 )
OUTPUT:
a = 10 , b = 20 , c = 29
a = 10 , b = 20 , c = 30
Process finished with exit code 0
Positional only parameters:
These indicate the functions that contain only positional parameters. In such functions, any arguments need not be forced to be passed in by keywords.
Example:
def function( a , b , c ):
print(f" a = { a } , b = { b } , c = { c } " )
function( 10 , 20 , 30 )
function( 5 , 6 , 3 )
OUTPUT:
a = 10 , b = 20 , c = 30
a = 5 , b = 6 , c = 3
Process finished with exit code 0
Keyword-only parameters:
These indicate the functions that contain only keyword parameters. To such functions, arguments can only be passed in them to the keywords of the parameters. You can create keyword-only parameters using the ' * ' symbol. All the parameters that come after the ' * ' symbol in a function definition are treated as keyword-only parameters.
Example:
def function( a , b , * , c , d ):
print(f" a = { a } , b = { b } , c = { c } , d = { d } " )
function( 10 , 20 , c = 30 , d = 40 )
OUTPUT:
a = 10 , b = 20 , c = 30 , d = 40
Process finished with exit code 0
Example:
def function( a , b , * , c , d ):
print(f" a = { a } , b = { b } , c = { c } , d = { d } " )
function( 10, 20, c=30 )
Output:
Traceback (most recent call last):
File "xxx\main.py", line 4, in <module>
function( 10, 20, c=30 )
TypeError: function() missing 1 required keyword-only argument: 'd'
Process finished with exit code 1
Var positional parameters:
When you require to pass an arbitrary number of arguments into a function, then its parameters are defined as var positional parameters. Such types of parameters are defined as *args, i.e., all the parameters passed under such conditions can be of any number and are not mandatory like other ones. Only arguments of positional parameters can be passed in for var positional parameters
SYNTAX:
def function( parameter1 , parameter2 , *args ):
pass
Example:
def my_fun( a , b , *args ):
print( f" a = { a } , b = { b } , c = { args } " )
my_fun( 5 , 6 , 7 , 8 , 9 , 0 )
my_fun( " A " , " B " , " C " , " D " , " E " , " F " )
OUTPUT:
a = 5 , b = 6 , c = (7, 8, 9, 0)
a = A , b = B , c = (' C ', ' D ', ' E ', ' F ')
Process finished with exit code 0
Example:
The built-in function max() in Python contains var keyword parameters.
Var Keyword parameters:
Var keyword parameters are similar to var positional parameters, except that we can pass only keyword arguments into the functions declared with such parameters.
In simple words, we can say that, when you require to pass an arbitrary number of keyword arguments into a function, then its parameters are defined as var positional parameters. Such types of parameters are defined as **kwargs, i.e., all the parameters passed under such conditions can be of any number and are not mandatory like other ones but have to be keyword arguments.
SYNTAX:
def function( parameter1 , parameter2 , **kwargs ):
pass
Example:
def my_fun( a , b , **kwargs ):
print( f" a = { a } , b = { b } , kwargs = { kwargs } " )
my_fun( 5 , 6 , c = 7 , d = 8 , e = 9 , f = 0 )
OUTPUT:
a = 5, b = 6, kwargs = {'c': 7, 'd': 8, 'e': 9, 'f': 0}
Process finished with exit code 0
Example:
The built-in function dict() in Python contains var keyword parameters.