TL ; DR:
x in my_list is True when the list holds x, and False when it doesn't. Use it as the condition of an if.
colors = ["red", "green", "blue"]
print("red" in colors) # True
print("pink" in colors) # False
if "pink" not in colors:
print("No pink yet")
Full lesson:
Often you need to know if a list holds some value. Is this name on the guest list? Is this item in the cart?
x in my_list answers that. It compares each item with x, from the first one on. It gives True as soon as one matches, or False if none does.
Use x not in my_list for the opposite. It gives True when x is missing.
The answer is a bool, so it fits right into an if:
cart = ["milk", "bread"]
if "milk" in cart:
print("Milk is in the cart")
It compares values, not indexes. So 2 in ["a", "b", "c"] is False, even though index 2 exists.
Text must match exactly. So "Red" in colors is False, because the list has "red".
Your task
bag holds what is packed for a hike.
If "water" is in bag, print Water is packed. If "tent" is not in bag, print Pack a tent.
Expected output:
Water is packed
Pack a tentFree plan: 30 tutor messages. PRO is unlimited.
