Complete the code to check if 'apple' is in the fruits list.
def test_apple_in_fruits(): fruits = ['apple', 'banana', 'cherry'] assert 'apple' [1] fruits
The in keyword checks if an item is present in a list. Here, 'apple' should be in the fruits list, so in is correct.
Complete the code to check that 'grape' is not in the fruits list.
def test_grape_not_in_fruits(): fruits = ['apple', 'banana', 'cherry'] assert 'grape' [1] fruits
The not in keyword checks that an item is not present in a list. Since 'grape' is not in fruits, not in is correct.
Fix the error in the assertion to check if 'banana' is in the fruits list.
def test_banana_in_fruits(): fruits = ['apple', 'banana', 'cherry'] assert 'banana' [1] fruits
The assertion should check if 'banana' is present in the list using in. Using not in or other operators is incorrect here.
Fill both blanks to create a test that checks if 'orange' is not in the fruits list and 'cherry' is in the fruits list.
def test_fruits_membership(): fruits = ['apple', 'banana', 'cherry'] assert 'orange' [1] fruits assert 'cherry' [2] fruits
The first assertion checks that 'orange' is not in the list, so not in is correct. The second checks that 'cherry' is in the list, so in is correct.
Fill all three blanks to create a dictionary comprehension that includes only fruits that are in the allowed list.
def test_allowed_fruits(): fruits = ['apple', 'banana', 'cherry', 'date'] allowed = ['banana', 'date', 'fig'] filtered = {fruit: True for fruit in fruits if fruit [1] allowed and fruit [2] 'apple' and fruit [3] 'fig'} assert 'banana' in filtered assert 'apple' not in filtered assert 'fig' not in filtered
not in instead of != for string comparisons.The comprehension includes fruits that are in the allowed list (in), but excludes 'apple' and 'fig' by checking they are not equal to those strings using !=. Using not in would be incorrect here because 'apple' and 'fig' are strings, not collections.