Python Membership Operators

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 inside
  • not 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") # True
print("z" in "apple") # False

Because “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) # True
print("python" in message) # False

With Lists

numbers = [1, 2, 3, 4, 5]
print(3 in numbers) # True
print(10 in numbers) # False

With Tuples

colors = ("red", "green", "blue")
print("green" in colors) # True
print("yellow" in colors) # False

With Dictionaries

For dictionaries, in checks the keys, not the values.

person = {"name": "Alex", "age": 25}
print("name" in person) # True
print("Alex" in person) # False

If we want to check values, we use .values().

print("Alex" in person.values()) # True

With Sets

fruits = {"apple", "banana", "orange"}
print("banana" in fruits) # True
print("grape" in fruits) # False

Python 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") # True
print("a" not in "apple") # False

Because “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 class

If we enter David, the output is:

David is not in the class

That 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) # False
print("Hello" in message) # True

If we want to ignore case, we convert both to lowercase first.

print("hello" in message.lower()) # True

Using in with Numbers

We cannot use in to check if a digit is inside a number. Because numbers are not sequences.

number = 12345
print(3 in number) # Error!

So what do we do? We convert the number to a string first.

number = 12345
print("3" in str(number)) # True

But 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 enter

Common 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)) # True

Now 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") # False

Why? 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()) # True

Now 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) # False

Why? 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()) # True

Now 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) # False

That 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.