Kursinnehåll
Introduction to Python (copy)
Introduction to Python (copy)
Modifying Functions
In programming, functions are dynamic tools that can adapt to different situations and needs. They're not just fixed blocks of code. You can enhance functions to make them more versatile and user-friendly in various contexts.
In this chapter, we'll explore some important techniques for modifying functions, like using default arguments and keyword arguments.
Let's start with a straightforward example of modifying functions to improve their usefulness in our grocery store management system:
Default Arguments
Default arguments are a useful feature in Python that allow you to specify default values for function parameters.
In the apply_discount()
function, the discount
parameter is set to 0.10
by default. This means the function will automatically apply a 10% discount unless otherwise specified. As seen in the default_discount_price
variable, we can call the function with just the price
parameter.
However, if needed, we can override the default value by passing both the price
and a custom discount
(e.g., 0.20
for 20%) as demonstrated with the custom_discount_price
variable.
# Define a function with a default `discount` argument def apply_discount(price, discount=0.10): discounted_price = price * (1 - discount) return discounted_price # Call the function without providing a `discount`, using the default value default_discount_price = apply_discount(100) print(f"Price after applying the default discount: ${default_discount_price}") # Call the function with a custom `discount` value custom_discount_price = apply_discount(100, 0.20) print(f"Price after applying a custom discount: ${custom_discount_price}")
Keyword Arguments
Keyword arguments in Python allow you to pass arguments by explicitly naming each parameter, making your function calls more readable and flexible. This is particularly helpful when a function has multiple parameters or when the order of arguments could be confusing.
In the following example, both price
and discount
are specified, while the tax
parameter remains at its default value, providing flexibility without compromising clarity.
# Function where `tax` has a default value def calculate_total(price, discount, tax=0.05): total = price * (1 + tax) * (1 - discount) return total # Calling the function using keyword arguments total_cost = calculate_total(price=100, discount=0.15) print(f"Total cost after applying discount: ${total_cost}")
Note
The order of parameters doesn't matter when they are passed using keyword arguments.
Swipe to start coding
Create functions to calculate the total cost of a product by applying a discount and tax, using keyword arguments and default values for flexibility.
- Define
apply_discount(price, discount=0.05)
→ Returns the price after applying the discount. - Define
apply_tax(price, tax=0.07)
→ Returns the price after adding the tax. - Define
calculate_total(price, discount=0.05, tax=0.07)
→ Usesapply_discount()
andapply_tax()
to return the total price with both discount and tax applied. - Call
calculate_total(120)
using the default discount and tax. - Call
calculate_total(100, discount=0.10, tax=0.08)
using custom values via keyword arguments.
Output Requirements
- Print the result with default values:
Total cost with default discount and tax: $<total_price_default>
- Print the result with custom values:
Total cost with custom discount and tax: $<total_price_custom>
Note
When defining functions, place required parameters first, followed by parameters with default values.
When calling functions with keyword arguments, positional arguments should come before keyword arguments.
Lösning
Tack för dina kommentarer!
Modifying Functions
In programming, functions are dynamic tools that can adapt to different situations and needs. They're not just fixed blocks of code. You can enhance functions to make them more versatile and user-friendly in various contexts.
In this chapter, we'll explore some important techniques for modifying functions, like using default arguments and keyword arguments.
Let's start with a straightforward example of modifying functions to improve their usefulness in our grocery store management system:
Default Arguments
Default arguments are a useful feature in Python that allow you to specify default values for function parameters.
In the apply_discount()
function, the discount
parameter is set to 0.10
by default. This means the function will automatically apply a 10% discount unless otherwise specified. As seen in the default_discount_price
variable, we can call the function with just the price
parameter.
However, if needed, we can override the default value by passing both the price
and a custom discount
(e.g., 0.20
for 20%) as demonstrated with the custom_discount_price
variable.
# Define a function with a default `discount` argument def apply_discount(price, discount=0.10): discounted_price = price * (1 - discount) return discounted_price # Call the function without providing a `discount`, using the default value default_discount_price = apply_discount(100) print(f"Price after applying the default discount: ${default_discount_price}") # Call the function with a custom `discount` value custom_discount_price = apply_discount(100, 0.20) print(f"Price after applying a custom discount: ${custom_discount_price}")
Keyword Arguments
Keyword arguments in Python allow you to pass arguments by explicitly naming each parameter, making your function calls more readable and flexible. This is particularly helpful when a function has multiple parameters or when the order of arguments could be confusing.
In the following example, both price
and discount
are specified, while the tax
parameter remains at its default value, providing flexibility without compromising clarity.
# Function where `tax` has a default value def calculate_total(price, discount, tax=0.05): total = price * (1 + tax) * (1 - discount) return total # Calling the function using keyword arguments total_cost = calculate_total(price=100, discount=0.15) print(f"Total cost after applying discount: ${total_cost}")
Note
The order of parameters doesn't matter when they are passed using keyword arguments.
Swipe to start coding
Create functions to calculate the total cost of a product by applying a discount and tax, using keyword arguments and default values for flexibility.
- Define
apply_discount(price, discount=0.05)
→ Returns the price after applying the discount. - Define
apply_tax(price, tax=0.07)
→ Returns the price after adding the tax. - Define
calculate_total(price, discount=0.05, tax=0.07)
→ Usesapply_discount()
andapply_tax()
to return the total price with both discount and tax applied. - Call
calculate_total(120)
using the default discount and tax. - Call
calculate_total(100, discount=0.10, tax=0.08)
using custom values via keyword arguments.
Output Requirements
- Print the result with default values:
Total cost with default discount and tax: $<total_price_default>
- Print the result with custom values:
Total cost with custom discount and tax: $<total_price_custom>
Note
When defining functions, place required parameters first, followed by parameters with default values.
When calling functions with keyword arguments, positional arguments should come before keyword arguments.
Lösning
Tack för dina kommentarer!