This Membership Operators lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
in asks whether a value is contained in a string, list, tuple, set, or the keys of a dict. not in is the opposite.
For a dict, in checks keys. It does not search the values.
Example 1: Start here
A set answers in quickly even when it holds many values.
skills = ["Python", "SQL"] # a list; the values stay in this order
print("SQL" in skills) # in is True when the value is found
learner = {"name": "Meera"} # a dictionary; each key has one value
print("name" in learner) # in is True when the value is found
Example 2: A letter in a word
in asks whether the smaller piece appears inside the larger one.
print("t" in "python") # in is True when the value is found
print("z" in "python") # in is True when the value is found
Example 3: An item in a list
Membership works on lists as well as text.
skills = ["Python", "SQL", "Git"] # a list; the values stay in this order
print("SQL" in skills) # in is True when the value is found
Example 4: not in
not in is True when the item is missing.
print("Java" not in ["Python", "SQL"]) # in is True when the value is found
