Lesson 9 of 21 · 2 min
Membership Operators — Checking if Something is "In" a Collection
fruits = ["apple", "banana", "mango"] # a list of fruits
letter = "a" # a single letter
name = "Priya" # a name
print("apple" in fruits) # is "apple" in the fruits list? True
print("grape" in fruits) # is "grape" in the fruits list? False
print("grape" not in fruits) # is "grape" NOT in the list? True
print(letter in name) # is "a" a character in "Priya"? True
Output: True False True True
in works on strings (checking for substrings), lists, tuples, sets, and dictionaries (checking keys).
