Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Keyword Arguments

00:00 In the last lesson, we looked at how to use positional arguments to provide values to the parameters of a function. In this lesson, we’re going to take a look at using keyword arguments to provide values to those same parameters. When you call a function, instead of just providing a value you, can use the notation <name>=<value>, where <name> is the name of the parameter and the <value> is the expression or value you would like that specific parameter to have. In doing this, you no longer need to maintain the order of the parameters that were given in the header of the function definition.

00:40 Since you’re specifying each one by name, Python will know which argument to use for each parameter. So, let’s use the same function from the last lesson and now let’s take a look at using keyword arguments to specify values.

00:58 So again, I begin with the function call, but now I want to say qty=6, I want to say item='bananas', and I want to say price=1.74.

01:16 So I’m being very explicit about which value I want for each parameter. And if I do that, I don’t necessarily need to keep the parameters in the same order.

01:33 I am again specifying what I want the value of price to be, I’m specifying what I want the value of qty (quantity) to be, and I’m specifying what I want the value of item to be.

01:43 So when we make this function call, the appropriate values are used for their respective parameters and the function behaves the way it should. You still have to be mindful of the number of parameters a function is expecting.

02:00 You have to be mindful of the actual names of the parameters. So, for example, if I say qty=6, item is 'bananas', and I say cost=1.74, I’m going to get an error message because there is no parameter cost.

02:22 There’s a parameter price, and in our minds, price and cost are roughly the same thing, but Python just sees this as an identifier and it looks at the name cost and it doesn’t have a parameter named cost up here, so it doesn’t know what to do with that. And again, if you leave out a parameter,

02:45 if I don’t specify a value for price, I’ll get that same error message that price was not provided a value.

02:55 You can mix the use of positional arguments and keyword arguments. However, when you do so, the positional arguments must come first.

03:09 So I could say 6 is going to be the value for qty, and then I could say price=1.74, and item='bananas',

03:23 and everything works! And again, the order in which I would say the values for price and item, using keyword arguments, would not matter.

03:33 Once I use a keyword argument,

03:38 I then cannot go back and use a positional argument. So once I start using keyword arguments, I must do that to completion. We get an error that we can’t call it that way, so if I want to do this correctly, I have to say price= and then it’s going to let me do that.

04:02 Once you have the notion of keyword arguments as a mechanism for specifying parameter values, you as the author of a function can start being a bit more creative.

04:16 One of the things that you can do is to define default values for some of your parameters, and we’ll take a look at that next.

Become a Member to join the conversation.