Using Python Optional Arguments When Defining Functions

In this section, you’ll learn how to define a function that takes an optional argument. Functions with optional arguments offer more flexibility in how you can use them. You can call the function with or without the argument, and if there is no argument in the function call, then a default value is used.

Default Values Assigned to Input Parameters

You can modify the function add_item() so that the parameter quantity has a default value:

# optional_params.py

shopping_list = {}

def add_item(item_name, quantity=1):
if item_name in shopping_list.keys():
shopping_list[item_name] += quantity

 

 

 

To finish reading, please visit source site