In this session we will talk about the arguments Asterix and double asterisks keywords.
So what is Asterix eggs.
This is a non key word argument that is used to parse multiple argument values to a function.
So let's understand this using examples so here to help define the function at which takes two parameters
A and B and within the function body B are printing the sum of the these two parameters and b here is
the function call we have parsed to argument and coming to the function at which I have defined here.
This function takes in three parameters A B and C and the function body here also does the same.
That means it adds all the three arguments A B and C and displays the output.
And here is a function called so in the first function call to see.
Here we have parsed two arguments in the second function call.
We have parsed three arguments but these functions are doing pretty much the same.
They're displaying the sum of the argument that are received through the function called
now.
How would you define a function that takes in variable number of arguments and execute these same set
of steps irrespective of the number of argument it a C say for example.
In this particular function call we are passing in two arguments in this we are passing five and here
we are passing seven arguments.
So how do you define your function such that it takes an variable number of arguments C in Python allows
us to parse variable number of arguments to a function using Asterix X so when we are not sure about
the number of arguments you pass to a function we can use this as tricks args
remember does not necessary to write start args as the parameter only the Asterix is mandatory.
You could also write Asterix that are Asterix X as your parameter writings Asterix ARGs and double asterisks
keywords is just a convention
D.
Asterix ARGs that we have defined here is also known as non keyword argument.
So when we make a function call to such function the arguments that we pass in this example one comma
to add parsed as a tuple to the function and these parsed argument make tuple inside the function with
the same name as the parameter
excluding the Asterix the variable that we associate with the Asterix becomes an item about so you can
either it or the elements in your tuple
so I have defined of function addition which has a non keyword arguments so here I have defined the
function addition which has the non keyword argument.
That means we can pass variable number of argument to this function addition and in the function body
we're returning the sum of the arguments pass to this function.
So we have a function call here through which we are passing five arguments so let's execute this piece
of code so here we have these some of these arguments.
Now let us pass a different set of arguments say one comma for comma 5 and then execute.
Here we have these sum so as you can see the same function can be used irrespective of the number of
arguments pass to the function the the arguments are parsed as a tuple and these parsed arguments make
a tuple inside the function with the same name as the parameter excluding the asterisks.
So let's see what is being assigned to this variable args.
After we make a function call and also let us see what is the type.
The data type of this non keyword ID argument and then execute this function line of code as you can
see a tuple has been assigned to this non keyword ID argument ARGs and then this is the data type of
the variable args it is a tuple and then we are returning the some of the arguments
so when we execute this line of code with these three set of arguments.
So here the tuple containing these three elements has been assigned to the non keyword argument dogs.
It is of the type tuple and then we have these some of the arguments in the output
byte and passes variable and non keyword arguments to a function using Asterix ARGs that we have just
talked about.
If we want to parse keyword arguments to a function such as like this here we have a bit of key and
value key and value if you want to pass an argument in this form in the form of a key and a value you
can use the non key worded argument.
Double asterisk K works and this allows us to pass where you blend of keyword arguments to a function
and B use a double asterisks before the parameter name to denote this type of argument and the arguments
are parsed as a dictionary and these arguments make a dictionary inside the function with the same name
as the parameter excluding the double Asterix.
So in case of the non keyword ID argument the arguments are parsed as a tuple a comma B comma C and
then within the function that assigned to the non keyword ID argument.
But coming to K walks the key value pairs are a sign in the form of a dictionary.
This is how they are to present to
and then we remove the double asterisks assigned to the keyword argument.
K Boggs again the double asterisks K Boggs is not necessity.
Only the double Asterix is necessary.
We can also write this as double Asterix word or double asterisks X..
Writing a single Asterix out and double asterisk K was is just a convention.
Here is an example for the keyword arguments.
So the function sample one printout the key word id argument.
So in the function call we have passed a pair of key while you pairs and d argument.
The key value arguments are parsed as a dictionary to the function and the arguments make a dictionary
inside the function with the same name as the parameter name.
Excluding the double asterisks so after calling the function within the function body we have displayed
debatable K bugs.
Let us check the data type of the variable K args so it is of the type.
Dictionary keyword argument is a dictionary so we can perform the various operations that can be performed
on a dictionary.
So letters display the items in this dictionary.
So for that K logs dart items so here we have a list of key value pairs.
In the output as you can see up until now we have used the asterisks args or the double asterisks K
bugs in the parameter of a function but we can also pass the Asterix args as an argument in the function
call.
So in this example I have assigned a tuple to this variable variable 1 and parse this variable at as
an argument to the function called so when we do this the Asterix here unbanked the items in the Japan
and then passes it to the function sample.
The Asterix unpacks these elements in the eye trouble such as a tuple or or list and then these values
sent as positional arguments to the function being called so the elements in the tuples unpacked and
sent to this function.
The value 1 is assigned to the parameter e in the function definition while you do is assigned to B
then value 3 is assigned to see or hear to have defined a function at and an array double bad which
is a tuple containing these elements and to the function call we have passed a non keyword argument
Asterix Ward and this Asterix unbanked D element in the tuple into positional arguments and these positional
arguments are parsed to this.
Function and each of these elements D positional arguments are assigned to the parameters here A B and
C so we have displayed these the values of each of these elements in the tuple as you can see
the parameters have been mapped to the positional arguments and then finally using the return statement
we are displaying the sum of the argument passed through the function call similarly we can pass key
word argument in the function call like this using double asterisks sign and the double asterisks unpacks
the elements in the dictionary.
That is the key value pairs in the dictionary as argument and it as keyword argument to the function
that is being called
we can use the key word a non keyword argument in combination with the normal argument.
Also known as formal arguments.
So here as you can see in the function call we have a normal argument as your first argument and then
we have a non key worded argument.
So when we make a function call the Asterix here that you see unpacks the elements in the tuple
in due positional parameters
and then all these arguments are parsed to the function so the argument Apple is assigned to the parameter
a the value 5 is assigned to B and so on so in this example here I have parsed a formal argument as
the first positional parameter in the function call and then we have the non keyword argument parse
through the Asterix using the asterisks and then we have two key value pairs as argument.
And finally we have the key word arguments pass through using the double asterisks sign.
So when we make a function call argument one is parsed as the first positional parameter and then we
have the asterisks which which unpacks these elements in the tuple into positional parameters and then
we have two key value bits that are parsed in the function call after the non keyword argument and then
we have the keyword arguments pass using did double asterisks sign and this double asterisks sign unpacks
the items in the dictionary.
Here so when we execute this in the value one has been mapped to this parameter a and then we have the
elements and the trooper it to unpack then map it to the parameters B and C and so on.
And then finally we are printing each of the arguments the first one is d form argument then we have
the non keyword argument and then we have the key value pairs.
And finally we have the keyword arguments displayed in the output men passing arguments to a function
we need to follow this order then passing the arguments.
So first we need to list all the formal arguments of the normal argument and then we have the non key
worded arguments.
And finally D key worded arguments and this brings us to the end of the topic keyword and non keyword
arguments.