Complete the code to check if 'apple' is in the list.
fruits = ['apple', 'banana', 'cherry'] if 'apple' [1] fruits: print('Found apple!')
The in operator checks if an item exists in a list.
Complete the code to check if 'dog' is NOT in the list.
animals = ['cat', 'bird', 'fish'] if 'dog' [1] animals: print('Dog is not here!')
The not in operator checks if an item does not exist in a list.
Fix the error in the code to check membership correctly.
colors = ['red', 'green', 'blue'] if 'yellow' [1] colors: print('Yellow is in the list') else: print('Yellow is not in the list')
The in operator is correct because the if block prints 'Yellow is in the list' when the item is present, and else when not.
Fill both blanks to create a dictionary of words and their lengths only if length is greater than 3.
words = ['sun', 'moon', 'star', 'sky'] lengths = {word: [1] for word in words if len(word) [2] 3}
We use len(word) to get the length and > to check if length is greater than 3.
Fill all three blanks to create a dictionary with uppercase keys and values only if value is not in the list.
values = ['a', 'b', 'c'] result = { [1]: [2] for [3] in values if [2] not in ['b', 'c']}
We use v.upper() for keys, v for values, and iterate with v over values.