beginnerPython
How do the in and not in operators work?
Answer
Clear, interview-ready explanation
The and in operators are membership operators.
not in
-
returnsinwhen a value exists in a sequence or collection.True -
returnsnot inwhen a value does not exist in it.True
They can be used with strings, lists, tuples, sets, dictionaries, and other iterable objects.
languages = ["Python", "PHP", "JavaScript"]
print("Python" in languages)
print("Java" not in languages)
Output:
True
True