Get startedGet started for free

Default and keyword arguments

1. Default and keyword arguments

Now we know how to build custom functions, let's look at how to extend their capabilities!

2. Average

Recall our average function. We briefly discussed that the variable, `values`, provided inside parentheses, was an argument.

3. Arguments

In a Python function or method, an argument refers to a value or data structure that is provided. There are two types of arguments: positional and keyword arguments.

4. Positional arguments

We've been using positional arguments already, where we provide arguments in order, separating each by a comma. An example is `round()`, where we provide the value to be rounded as the first positional argument, then add a comma, followed by the number of digits to round to as the second positional argument.

5. Keyword arguments

In contrast, keyword arguments require us to define each argument's value using its name. They help us understand what a function is doing and keep track when a function has many arguments. Here, we call the `round()` function again, this time providing the `number` keyword argument and setting it equal to pi with 10 decimal places.

6. Keyword arguments

We follow with a comma, then set the `ndigits` keyword argument equal to two. We get the same result!

7. Identifying keyword arguments

But how do we know what these keywords are? We can use the `help()` function to get this information! Let's look at the output of calling `help()` on the `round()` function.

8. Keyword arguments

We can see the first argument is called `number` and the second is called `ndigits`. Notice that `ndigits` is set equal to `None` within the output.

9. Default arguments

In Python, `None` means no value, or empty. This is an example of a default argument, which is a way of assigning a default value to an argument. We have been overwriting this argument's value from `None` to two, but the help output tells us that if `ndigits` is omitted, the result will be rounded to zero decimal places, resulting in an integer. Default arguments are helpful as they force us to think about how people will use our functions. If we expect an argument to have a specific value most of the time, we can set it as a default argument.

10. Adding an argument

With this new knowledge, let's modify our previous average function.

11. Adding an argument

We add a keyword argument called `rounded`, with a default boolean value of `False`.

12. Adding an argument

Inside the function, we check if `rounded` has been changed to `True`. If so, we calculate the average, round it to two decimal places, and return this value.

13. Adding an argument

Otherwise, we calculate the average and return it without any rounding.

14. Using the modified average() function

Using our preparation times data from earlier, let's test our function.

15. Using the modified average() function

We can call the function using positional arguments, providing `preparation_times`, then the boolean value of `False`, so the results are not rounded. Alternatively, as the default value of `rounded` is `False`, we can omit the `rounded` argument, and the results will be the same!

16. Using the modified average() function

Likewise, we can use keyword arguments. Here, we set `values` equal to `preparation_times` and `rounded` equal to `True`. This returns the average time value rounded to two decimal places.

17. Let's practice!

Now you can see the flexibility and potential of custom functions!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.