Let's talk about 'args' and 'kwargs'.
'args' are arguments, and 'kwargs' are key word arguments.
Keyword is one word I think, keyword arguments.
So what you're going to see in the while, in other people's
code is something like, 'def', you've got a 'func_name' in here,
some function name,
it's going to take some sort of parameter,
let's say it takes a 'name', and then has '*args' and then '**kwargs'.
Now, '*args' can technically be named anything, but it is denoted
by a single asterisk.
This means that there are going to be other arguments in
this function. And keyword arguments,
again, this could be called anything else, but by convention,
we call this 'kwargs',
and this one is 'args'.
This one is denoted with two asterisks, '**' before it.
So let's go ahead and explore some 'args' first.
So let's go ahead and just say '*args',
and in our function, we're simply going to loop through these 'args'.
But first, I want to actually print out what 'args' is.
'print(args)', and notice that there is asterisk in here.
This is simply saying that, "Hey, there's going to be other
arguments in here, and use 'args' as the variable name in here.
So 'print(args)'.
Let's also 'print(type(args))'.
What is this going to be?
And let's run 'def func_name' here.
'(1, 5, 8, 'Python', Coding')'. I don't know.
I'm just filling with some random stuff in here.
And we can see when we print the arguments
this is a tuple.
And so the 'args' are a tuple, means they cannot be changed.
We can't add more values to the arguments.
We simply cannot do that because tuples don't allow us to do that.
They're immutable. Which means we're stuck with the
values that come in here.
Doesn't mean we have to use them,
but we cannot change 'args' as a variable name inside of this
function. Once it is set, it is set in stone.
So let's see how we can access these arguments here.
'def', we're going to call this one 'print_args', and it's going
to take any number of arguments,
and it could be literally any number of arguments.
It could be 1, it could be 0,
it could be a 1000. 'for arg in args', we could simply print
the argument value, whatever that is going to be.
And so if we say 'print_args('Computer', 'Coffee', 'Cup', 'Monitor',
'Lamp')', these are literally just things that I can see
on my desk right now.
This function is going to go through all of these arguments,
and just going to print them out for us.
Go through each 'arg' one by 1.
Now that is a standard argument,
but we also have these things called 'kwargs',
keyword arguments.
Keyword arguments are like what we saw in the last video
where the 'role' was 'Student' by default.
Let's go ahead and create an example here.
'def my_func', and it's going to take not 'args', but just 'kwargs',
so '**kwargs', and let's print out what 'kwargs' is going to be,
and let's also print out the 'type(kwargs)',
so we know what we're working with.
[no audio]
and 'my_func', and
this is going to take keyword argument,
that's what 'kwargs' is,
keyword arguments. 'name = 'Kalob', 'drink' = 'Coffee', is equal
'hobby =', not 'Planting', 'Gardening', something like
that, and that is not going to work because I typoed the
function name. There we go.
So this came back as a dictionary. 'print(type(
kwargs))', came back as a dictionary, and we get a key:value,
key:value, key:value. So that's interesting.
Now we have something to work with.
We know about tuples, we know about dictionaries,
we have something to work with here.
So let's go ahead and take this exact same example,
but let's loop through every key:value, and just print it back.
So we could say, 'for key, value in kwargs.items'.
We know it's a dictionary, and we can use '.items' on it.
'print("Key:', is going to be whatever the 'key' is going to be,
let's go ahead put an arrow in here or not an arrow,
let's do a few tabs, and then the "Value:" is going to be whatever
the 'value' is going to be.
And let's take this exact example again, and we can see 'key'
is equal to 'name', the 'value' is 'Kalob'; 'drink', 'Coffee'; 'hobby', 'Gardening'.
Now, at this point in time, this is actually super useless
because you're never really going to do this in real Python
programming. But let's say you're going to order food from
a food ordering company. I don't know,
"Uber Eats", "SkipTheDishes", "DoorDash", whatever company,
they're going to have some sort of function or a series of
functions that's going to accept an order.
So let's create an 'order()' function in here, and it needs to
take your 'name' because they need to know who to deliver to.
We could also say, it needs to take your address, but that's
getting a little too complicated, a little too much information,
you'll just watch me type for too long.
So it's going to take someone's 'name', and maybe some other notes,
some other keyword 'args', or just 'args', not keyword 'args',
and then it's also going to take some 'kwargs'.
And this is going to say 'print("Hello", )'.
This is an f-string.
So let's go ahead and throw an 'f"' at the beginning of that.
The other 'args' in here could be different dishes that you want.
So we could say 'for arg in args: print(
"\t You ordered, You ordered an ", and that's an f-string as well.
Now, to make this a little simpler, we could actually change this
from 'args' to 'dishes',
[no audio]
and let's call this 'dish' and "You ordered a ".
And we could also check to see if someone's address,
this is a good example, if someone's address is in the keyword
'args', so we can say 'if kwargs.get("address")
print("\t
We are delivering to )'.
And let's reassign this.
That's also an f-string,
so boop little 'f"' there.
And let's create 'address = kwargs.get('address')'.
And this is only going to show up if address is actually
in one of the keyword 'args'.
So let's hit 'Enter' there, and let's create an 'order'.
An 'order' is going to be 'for', "Zephyr",
he's going to order "tacos" and also "cat food", because he's
a cat, and his address is a keyword argument.
His address is going to be, "123 Meow Lane"
And look at that it says, "Hello Zephyr
"You ordered tacos". "You ordered an tacos".
"You ordered tacos". "You ordered cat food". And, "We are going to
deliver to 123 Meow Lane".
Let's go ahead and rerun this without an 'address'.
Let's see what happens.
And let's also get rid of 'an', because that's just bad grammar.
There were no keyword arguments provided.
And 'address', where we saw 'address =', does not exist.
So it's skipped over this because it didn't exist.
So the 'it' statement was never triggered.
In fact, we can actually do something a little more explicit
here and say, 'print("You', all caps, let's yell at the person
"YOU DID NOT PROVIDE AN ADDRESS WOW
CMON")'. Let's rerun that one.
Let's rerun that one.
It says, "YOU DID NOT PROVIDE AN ADDRESS WOW"
'address',
and this could be anything at this point, anything.
[no audio]
So this is a little bit of an example, a fairly good example
that I think a lot of people can relate to these days,
where your ordering food,
you need a name because your account has a name,
they need to know what dishes you're ordering, which could
be any number of arguments and also keyword arguments,
things like your address or any special delivery note could
be a keyword argument.
So why is this important?
Because in the wild, especially in web frameworks like Django
or Flask, you're going to see a lot of function parameters
where you just have a regular argument,
and then you've got unlimited arguments, and then you have
keyword args, and you're going to see these working together
all the time.