Pre-requisite : Functions in Python
In Python, Parameter refers to the information passed to the function. Parameters are also known as arguments.
Typically, Parameters are of two types – Formal Parameters, Actual Parameters
Formal Parameters are the parameters which are specified during the definition of the function.
Consider the following code :
def sum(a, b):
return a + b
In the above code, ‘a’ & ‘b’ are acting as formal parameters.
Actual Parameters are the parameters which are specified during the function call. Actual Parameters are actually of four types :
- Positional Parameters
- Keyword Parameters
- Default Parameters
- Variable length Parameters
Let us explore them one by one
Positional Parameters/ Arguments
Positional Parameters are the parameters that are passed to the function in the correct positional order. For example, consider following code :
def result(name, marks):
# some cool code
result("Geek", 95)
Above function expects you to provide two parameters during the invocation(function call). During, first parameter should be a string and second parameter should be an integer.
Below code will work and produce output
def result(name, marks):
print("Name of student : ", name)
cgpa = marks/10
print("CGPA is : ", cgpa)
# calling above function with name as "Geek" and marks as 95.
result("Geek",95)
Output :
Name of student : Geek
CGPA is : 9.5
In the above code, parameter name has the value “Geek” and marks has 95 as value.
Now, let’s do some experiment, let’s swap both parameters
def result(name, marks):
print("Name of student : ", name)
cgpa = marks/10
print("CGPA is : ", cgpa)
# calling above function with name as 95 and marks as "Geek".
result(95,"Geek")
Output :
TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’
So, position matters.
Keyword Parameters/ Arguments
Positional Parameters are the parameters that are are capable of identifying the parameters with their specified name. For example, consider following code :
def result(name, marks):
# some cool code
result(name = "Geek", marks = 95)
Above function expects you to provide two parameters during the invocation(function call). In Keyword parameters, maintaining order is not mandatory as we are already specifying the correct parameter with its name.
Both code will work and produce output
def result(name, marks):
print("Name of student : ", name)
cgpa = marks/10
print("CGPA is : ", cgpa)
# calling above function with name as "Geek" and marks as 95.
result(name = "Geek", marks = 95)
Output :
Name of student : Geek
CGPA is : 9.5
In the above code, parameter name has the value “Geek” and marks has 95 as value.
Now, let’s do some experiment, let’s swap both parameters
def result(name, marks):
print("Name of student : ", name)
cgpa = marks/10
print("CGPA is : ", cgpa)
# calling above function
result(marks = 95,name = "Geek")
Output :
Name of student : Geek
CGPA is : 9.5
Default Parameters/ Arguments
Default Parameters are the parameters in which we can specify the default value for the parameters in the function definition. For example, consider following code :
def result(name, marks = 95):
# some cool code
result("Geek")
Default Parameter is a parameter that assumes a default value if a value is not specified during function invocation. Consider the following code :
def result(name, marks = 95):
print("Name of student : ", name)
cgpa = marks/10
print("CGPA is : ", cgpa)
# calling above function
result("Geek") # Invocation 1
result("Geek", 90) # Invocation 2
In the above code example, we have called same function twice.
Invocation 1 : result() is invoked with “Geek” as the first parameter. In this case, value for second parameter(marks) is not passed. But, in the function definition, value for variable ‘marks’ is defined as 95.
So, for Invocation 1, output would be :
Name of student : Geek
CGPA is : 9.5
Invocation 2 : result() is invoked with “Geek” as the first parameter and 90 as the second parameter. But, in the function definition, value for variable ‘marks’ is defined as 95.
So, in the case where no value is specified for a parameter, default value is considered or else actual value is given more priority over default value.
So, for Invocation 2, output would be :
Name of student : Geek
CGPA is : 9.0
Variable Length Parameters/ Arguments – *args & **kwargs
There might be the cases where even developer is not clear of the number of values a function may receive. In all such cases, we can not specify the number of parameters in the function definition. To encounter this issue, variable length parameters comes into picture.
def result(farg, *args):
# some cool code
result(1, "Geek")
farg represents the formal argument and *args represents the variable length parameters. We can pass 1 or more values to *args and it will store them in tuple (stores a group of elements).
Let’s understand this with the help of code example :
def square_of_numbers(farg, *args):
print("Type of arg is ", type(args))
print("farg is : ", farg)
for each_arg in args:
print("args is : ", each_arg)
square_of_numbers(1, 5, 10, 15)
O/P :
Type of arg is <class ‘tuple’>
farg is : 1
args is : 5
args is : 10
args is : 15
In the above program, we have 1 formal argument and 3 variable length parameters.
Please note that while invoking a function, formal parameters are mandatory meanwhile it is optional to provide variable length parameters.
**kwargs : In Python, **kwargs represents the keyword variable arguments or parameters. This argument represents the dictionary object. Dictionary in Python stores the data in the form of key-value pairs.
A Dictionary is more like a set of key-value pairs enclosed under ‘{}’.
Example
d = {
"name": "Apurv",
"marks" : "95",
"grade" : "A+",
"result" : "PASS"
}
# traversing a dictionary
for key, val in d.items(): # items() will give pairs of items
print("Key : {0}, Value : {1}".format(key, val))
O/P
Key : name, Value : Apurv
Key : marks, Value : 95
Key : grade, Value : A+
Key : result, Value : PASS
Note ! Key or value can be of any type.
A keyword variable argument can accept any number of values provided in the form of key-value pairs.
multiply(num1, num2 = 20)
In the above code, num1 is a formal argument and num2 is a keyword variable argument with ‘num2′ as a key and ’20’ as value. Its representation could be imagined as :
{ "num2" : 20 }
One more example,
multiply(rollno, name = "Apurv", marks = 99, result = "PASS")
In the above code, rollno is a formal parameter and name, marks, & result are keyword variable parameters.
Program : To check the implementation of **kwargs.
def display(rollno, **kwargs):
for key, val in kwargs.items():
print("{0} : {1}".format(key, val))
display(1, name = 'Apurv', marks = 95, grade = 'A+', result = 'PASS')
O/P :
name : Apurv
marks : 95
grade : A+
result : PASS
Well, that’s all in this lesson.

thank’s