Python Membership Operators
Table of Contents
In the last lesson, we learned assignment operators. Now let’s check whether values exist inside strings, lists, and other collections.
After learning Python Assignment Operators, we can update variables efficiently. Now let’s check whether values appear inside strings, lists, and other collections. In this tutorial, we’ll learn about Python membership operators with easy examples.
Let’s Check If Something Is Inside Something Else
Have we ever checked if a name is on a guest list? Or if a word is in a sentence? Or if a number is in a list of numbers?
That is what membership operators are for.
They check if a value is present inside another value. And they give us a simple answer: True or False.
Python has two membership operators:
in— checks if something is insidenot in— checks if something is not inside
Let’s learn them.
Python in Operator
So what does in do? It checks if a value exists inside a sequence.
print("a" in "apple") # Trueprint("z" in "apple") # FalseBecause “a” is inside “apple”. But “z” is not.
We can use it with strings, lists, tuples, dictionaries, and sets. Let’s see examples.
With Strings
message = "Hello, world!"print("world" in message) # Trueprint("python" in message) # FalseWith Lists
numbers = [1, 2, 3, 4, 5]print(3 in numbers) # Trueprint(10 in numbers) # FalseWith Tuples
colors = ("red", "green", "blue")print("green" in colors) # Trueprint("yellow" in colors) # FalseWith Dictionaries
For dictionaries, in checks the keys, not the values.
person = {"name": "Alex", "age": 25}print("name" in person) # Trueprint("Alex" in person) # FalseIf we want to check values, we use .values().
print("Alex" in person.values()) # TrueWith Sets
fruits = {"apple", "banana", "orange"}print("banana" in fruits) # Trueprint("grape" in fruits) # FalsePython not in Operator
So what does not in do? It’s the opposite. It checks if a value is not inside.
print("z" not in "apple") # Trueprint("a" not in "apple") # FalseBecause “z” is not inside “apple”. So not in gives True.
Real-Life Example: Class List
Let’s check if a student is in a class list.
class_list = ["Alex", "Sam", "John", "Emma"]
student = input("Enter student name: ")
if student in class_list: print(f"{student} is in the class")else: print(f"{student} is not in the class")If we enter Sam, the output is:
Sam is in the classIf we enter David, the output is:
David is not in the classThat is how membership operators work. Simple.
Another Example: Checking a Word in a Sentence
sentence = "The quick brown fox jumps over the lazy dog"
word = input("Enter a word to search: ")
if word in sentence: print(f"'{word}' was found in the sentence")else: print(f"'{word}' was not found")If we enter fox, it finds it. If we enter cat, it does not.
Case Sensitivity
Remember, membership checks are case sensitive for strings.
message = "Hello World"print("hello" in message) # Falseprint("Hello" in message) # TrueIf we want to ignore case, we convert both to lowercase first.
print("hello" in message.lower()) # TrueUsing in with Numbers
We cannot use in to check if a digit is inside a number. Because numbers are not sequences.
number = 12345print(3 in number) # Error!So what do we do? We convert the number to a string first.
number = 12345print("3" in str(number)) # TrueBut this is not common. Usually we check membership in strings or collections.
Membership in if Statements
Membership operators are often used in if statements.
banned_users = ["Sam", "John"]
user = "Alex"
if user not in banned_users: print("You can enter")else: print("You are banned")Output
You can enterCommon Mistakes with Python Membership Operators
Mistake 1: Using in with a Number
Here’s one. We write 3 in 12345. And we get an error.
print(3 in 12345) # Error!Why? Because 12345 is a number, not a sequence. We can’t check inside a number.
So what do we do? We convert it to a string first.
print("3" in str(12345)) # TrueNow it works.
Mistake 2: Forgetting Case Sensitivity
Let’s say we write "hello" in "Hello World". What do we get? False.
print("hello" in "Hello World") # FalseWhy? Because “hello” and “Hello” are different. Uppercase H vs lowercase h.
So what do we do? We use .lower() or .upper().
print("hello" in "Hello World".lower()) # TrueNow both are lowercase. So it works.
Mistake 3: Checking Dictionary Values Without .values()
Here’s a tricky one. We write "Alex" in person. And we get False.
person = {"name": "Alex"}print("Alex" in person) # FalseWhy? Because in checks keys, not values. And “Alex” is a value. The key is “name”.
So what do we do? We use .values().
print("Alex" in person.values()) # TrueNow it checks the values. And it finds “Alex”.
Mistake 4: Using in on an Empty Sequence
Let’s say we write "anything" in empty_list. What do we get? False.
empty_list = []print("anything" in empty_list) # FalseThat is correct. It returns False. No mistake there. Just remember that an empty sequence contains nothing.
Next, we will learn about Identity Operators so we can continue step by step.