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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Assigning Default Values

00:00 Default Values Assigned to Input Parameters. You can modify the function add_item() so that the parameter quantity has a default value.

00:35 In the function signature, you’ve added the default value 1 to the parameter quantity, but this doesn’t mean that the value of quantity will always be 1.

00:46 If you pass an argument corresponding to quantity when you call the function, then that argument will be used as the value for the parameter. However, if you don’t pass any argument, then the default value will be used.

00:59 1 in this case. Parameters with default values can’t be followed by regular parameters. You’ll see more about the order in which you can define parameters later on in this course. The function add_item() now has one required parameter and one optional parameter.

01:18 In the code example just seen, you call add_item() twice. Your first function call has a single argument, which corresponds to the required parameter item_name. In this case, quantity defaults to 1. Your second function call has two arguments, so the default value isn’t used in this case.

01:41 You can see the output on-screen.

01:47 You can also pass required and optional arguments into a function as keyword arguments. Keyword arguments can also be referred to as named arguments. You can now revisit the first function you defined in this course and refactor it so that it also accepts a default argument.

02:31 Now when you use show_list(), you can call it with no input arguments or pass a Boolean value as a flag argument. If you don’t pass any arguments when calling the function, then the shopping list is displayed by showing each item’s name and quantity.

02:45 The function will display the same output if you pass True as an argument when calling it. However, if you use show_list(False), only the item names are displayed.

03:04 You should avoid using flags in cases where the value of the flag alters the function’s behavior significantly. A function should only be responsible for one thing. If you want to flag to push the function into an alternative path, you may consider writing a separate function instead.

03:22 In the next section of the course, you’ll look at common values which are used as defaults.

Become a Member to join the conversation.