Welcome back. I want us to now talk about keyword arguments.
All
Python functions,
they basically allow us to pass arguments using parameter names,
parameter names are what we refer to as keywords.
They'll make a code more readable and flexible,
especially when we're dealing with many parameters.
Now, so far, we've been dealing with parameters like AB XY.
But let me show you something else. OK?
I'm gonna create a new function
and let's call this function a describe
underscore person. OK?
So this function is going to describe somebody's name,
age and let's say city. All right. So
let me add the brackets in there. And now in here
we can add the keyword argument. So I'm gonna say name
age
and then let's say city.
OK?
And now for the actual function itself. Oh, sorry, I forgot to add the colon.
And now the actual function itself. What's it gonna do?
We wanted to print. OK.
Uh John is 30 years old and lives in London just as an example, right? So
I'm gonna say print
brackets and now the F string,
OK? And now
cos and now curly braces
name
space is
Calibri
again,
I will now call the age parameter
bracket, years
old.
And I'll let Python finish the rest for me
and there it is.
So basically, the function will say, OK, Alex is 100 years old
and lives in New York City just as an example.
So check this out. I'm going to call the function.
So you can see right now, I'm going to call Alice.
Thank you Python for providing this suggestion. So
I'm gonna say
Alice is 25 years old
and lives in New York.
So if I run the function right now, you can see it says Alice is 25 years old
and lives in New York pretty straightforward, right?
However,
check this out,
I can also call the function by providing
the values for the parameters themselves. Look at this, OK,
I can say describe person
and now in brackets
instead of saying, OK, Alex 30 London
instead I can say age
equals
to 30
and now comma
and then I can say the name
equals
Bob
City is San Francisco.
Did you see what I did? I did right now,
instead of providing the actual values for the arguments directly.
I just said, hey, I'm going to pass the values into the keyword argument.
So age will be 30. Name is Bob City is San Francisco.
And of course,
please pay attention to the fact that Bob and San Francisco are strings.
That's why they have the double codes while 30 is an integer. It's not a string.
That's why there are no codes.
So
if I run the function,
you can see right now it says Bob is 30 years old and lives in San Francisco.
Despite the fact
that here age came first, we didn't say name equals Bob and then age
or
no.
That's because Python
is simply going to use what we have described in the actual function itself.
It's not going to matter the order.
All the arguments.
Python doesn't care about that. All Python cares about is OK,
you've given me
the keyword arguments fine. And now this is the order.
It's going to look at the function that says, OK,
print the name first and then is eight years old and it lives in city.
So these are all referred to as the keyword arguments.
It's not likely we're gonna walk with them later on in this course.
But nevertheless, this is something that you should be at least be aware of
when you're walking with Python. So thank you for watching the video.
I will see you in the next class.