Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn List Behavior in Functions | Functions
Introduction to Python (generated task tests)

Swipe to show menu

book
List Behavior in Functions

Let's explore working with lists inside functions. While it's similar to working with numbers, there are a few important details to be aware of.

Lists are mutable, meaning that any changes made to a list inside a function will also affect the original list outside the function if passed directly.

To prevent this, you can use the copy() method with any mutable data type. By doing so, you create a shallow copy of the list, allowing you to modify a duplicate instead of the original. This ensures that changes made within the function don't impact the original list.

1234567891011121314
def add_strawberry(original_list): list_copy = original_list.copy() # Create a copy of the original list list_copy.append("Strawberry") # Modify the copied list return list_copy # Original list fruits = ["Apple", "Banana", "Cherry"] # Call the function new_fruits = add_strawberry(fruits) # Check the results print("Original list:", fruits) # ['Apple', 'Banana', 'Cherry'] print("Modified list:", new_fruits) # ['Apple', 'Banana', 'Cherry', 'Strawberry']
copy

In this example, the original fruits list remains unchanged because copy() was used. Without copy(), modifying the list inside the function would have changed the original fruits list as well.

Task

Swipe to start coding

Create a function to apply a 10% discount to product prices over $2.00, without changing the original list.

  • Define a function apply_discount(prices) that takes a list of prices.
  • Inside the function, make a copy of prices and assign it to prices_copy.
  • Use a for loop with index iteration (range(len(prices_copy))) to go through the copied list.
  • If a price is greater than 2.00, apply a 10% discount.
  • Return the updated prices_copy list.

Output Requirements

  • The function should return the new list with discounted prices.
  • Print the result using:
    Updated product prices: <$updated_prices>

Note

Use index-based iteration to ensure the list is modified correctly: for index in range(len(prices)): modifies elements directly, unlike for price in prices:.

Requirements checklist

  1. Call apply_discount with a list of prices containing values both above and below 2.00, and check that only prices greater than 2.00 are reduced by 10%, while others remain unchanged (use a tolerance for floating point comparison).
  2. Check that the returned list from apply_discount is a new list and not the same object as the input list.
  3. Check that the original input list is not modified after calling apply_discount.
  4. Call apply_discount with an empty list and check that it returns an empty list.
  5. Call apply_discount with a list where all prices are less than or equal to 2.00 and check that the returned list is identical to the input list.
  6. Call apply_discount with a list where all prices are greater than 2.00 and check that all values are reduced by 10% (within a tolerance).
  7. Check that after running the code, the printed output contains the string "Updated product prices:" followed by the correct updated prices list.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 2.17

book
List Behavior in Functions

Let's explore working with lists inside functions. While it's similar to working with numbers, there are a few important details to be aware of.

Lists are mutable, meaning that any changes made to a list inside a function will also affect the original list outside the function if passed directly.

To prevent this, you can use the copy() method with any mutable data type. By doing so, you create a shallow copy of the list, allowing you to modify a duplicate instead of the original. This ensures that changes made within the function don't impact the original list.

1234567891011121314
def add_strawberry(original_list): list_copy = original_list.copy() # Create a copy of the original list list_copy.append("Strawberry") # Modify the copied list return list_copy # Original list fruits = ["Apple", "Banana", "Cherry"] # Call the function new_fruits = add_strawberry(fruits) # Check the results print("Original list:", fruits) # ['Apple', 'Banana', 'Cherry'] print("Modified list:", new_fruits) # ['Apple', 'Banana', 'Cherry', 'Strawberry']
copy

In this example, the original fruits list remains unchanged because copy() was used. Without copy(), modifying the list inside the function would have changed the original fruits list as well.

Task

Swipe to start coding

Create a function to apply a 10% discount to product prices over $2.00, without changing the original list.

  • Define a function apply_discount(prices) that takes a list of prices.
  • Inside the function, make a copy of prices and assign it to prices_copy.
  • Use a for loop with index iteration (range(len(prices_copy))) to go through the copied list.
  • If a price is greater than 2.00, apply a 10% discount.
  • Return the updated prices_copy list.

Output Requirements

  • The function should return the new list with discounted prices.
  • Print the result using:
    Updated product prices: <$updated_prices>

Note

Use index-based iteration to ensure the list is modified correctly: for index in range(len(prices)): modifies elements directly, unlike for price in prices:.

Requirements checklist

  1. Call apply_discount with a list of prices containing values both above and below 2.00, and check that only prices greater than 2.00 are reduced by 10%, while others remain unchanged (use a tolerance for floating point comparison).
  2. Check that the returned list from apply_discount is a new list and not the same object as the input list.
  3. Check that the original input list is not modified after calling apply_discount.
  4. Call apply_discount with an empty list and check that it returns an empty list.
  5. Call apply_discount with a list where all prices are less than or equal to 2.00 and check that the returned list is identical to the input list.
  6. Call apply_discount with a list where all prices are greater than 2.00 and check that all values are reduced by 10% (within a tolerance).
  7. Check that after running the code, the printed output contains the string "Updated product prices:" followed by the correct updated prices list.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

close

Awesome!

Completion rate improved to 2.17

Swipe to show menu

some-alt