Python Membership Operators

In the last lesson you learned Assignment Operators. Those are about putting a value into a variable. Now we ask a different question. We already have a value. We want to know one thing. Is it sitting inside this group of things or not? That is exactly what membership operators answer.

๐Ÿค” Why Membership Operators?

Say you have a list of usernames who are allowed into an app. Someone types their name. You need to know one thing fast. Is this name in the allowed list or not?

You could write a loop that walks through every name and compares them one by one. That works. But it is a lot of code for a simple yes-or-no question. Python gives you a shorter way. The membership operators let you ask โ€œis this value inside that group?โ€ in plain words. You get back True or False.

๐Ÿงฉ What Are in and not in?

Python has two membership operators.

  • in asks: is this value somewhere inside the sequence? You get True if it is, and False if it is not.
  • not in is the opposite. It gives True when the value is missing.

A sequence here just means a group of values in order. A string of characters is one. A list of items is another. Think of in like checking a guest list. You stand at the door. You look down the list. You ask one question. Is this name on it? You do not care where on the list it is. You only care yes or no.

Here is the basic shape. We check single characters and whole items.

print("a" in "cat") # is the letter "a" inside the word "cat"?
print("z" in "cat") # is the letter "z" inside the word "cat"?
print("dog" not in "cat") # is "dog" missing from "cat"?

Output

True
False
True

Letโ€™s read those lines. "a" in "cat" is True. The word โ€œcatโ€ really does contain the letter a. "z" in "cat" is False. There is no z anywhere in โ€œcatโ€. And "dog" not in "cat" is True. The word โ€œcatโ€ does not contain โ€œdogโ€, so the โ€œis it missing?โ€ question comes back yes.

The reason this is so useful is simple. Every result is a boolean. It is just True or False. So you can use these operators anywhere you need a yes-or-no answer, like inside an if statement.

๐Ÿ“‹ Checking If an Item Is in a List

Strings are nice for a first look. But the everyday use is with lists. A list is just a group of items in square brackets. We want to know if a certain item is one of them.

This snippet checks whether a fruit is in our basket.

fruits = ["apple", "banana", "mango"]
print("banana" in fruits) # is "banana" one of the items?
print("orange" in fruits) # is "orange" one of the items?
print("orange" not in fruits)

Output

True
False
True

See how it reads almost like English? "banana" in fruits checks the whole word โ€œbananaโ€ against each item in the list. It found a match, so the answer is True. There is no โ€œorangeโ€ in the list, so the second line is False. And asking if it is not in the list comes back True.

Caution

With a list, in matches a whole item, not a piece of it. So "app" in ["apple"] is False. The value โ€œappโ€ is not the same item as โ€œappleโ€. But with a string, "app" in "apple" is True. There it is searching for the letters inside the word. Same operator, but the kind of value changes what counts as a match.

๐Ÿ”‘ A Real Use: An Allowed-Users Check

Now the real example from the start. We have a short list of usernames allowed into an app. A person types a name. We decide whether to let them in.

We take the typed name and ask if it belongs to the allowed group.

allowed_users = ["alex", "riya", "arjun"]
username = "riya"
if username in allowed_users:
print(f"Welcome back, {username}!")
else:
print("Sorry, you are not on the list.")

Output

Welcome back, riya!

Letโ€™s walk through it line by line.

  • allowed_users holds the names we trust.
  • username is the name the person gave us. Here it is "riya".
  • if username in allowed_users: is the check. It asks if the value of username is one of the items in the list. It is, so the answer is True.
  • Because the answer is True, Python runs the first print. If the name had been missing, it would run the else line instead.

That one line, username in allowed_users, replaced a whole loop. That is the point of these operators.

๐Ÿ” Searching for Text Inside a String

One more handy use. With strings, in does not only check single letters. It can search for a whole smaller piece of text inside a bigger one. That smaller piece is called a substring. A substring just means a run of characters that sits inside another string.

This checks whether a word appears inside a sentence.

message = "Your order has shipped"
print("shipped" in message) # does the sentence contain this word?
print("delayed" in message)

Output

True
False

So "shipped" in message is True. Those exact letters, in that order, really are inside the sentence. This is great for a quick check like โ€œdoes this email contain the word refund?โ€ without any heavy text tools.

Tip

Membership checks on strings are case-sensitive. The words "shipped" and "Shipped" are not the same to Python. If you want to ignore case, lower both sides first, like "shipped" in message.lower().

๐Ÿ“Š Quick Reference

Here is the whole idea in one place.

Expression Asks Result
"a" in "cat" Is the letter in the word? True
"z" in "cat" Is the letter in the word? False
"banana" in fruits Is the item in the list? True
"orange" not in fruits Is the item missing? True

โš ๏ธ Common Mistakes

A few things trip people up early on.

  • Mixing up the order. The value you are looking for goes on the left. The group goes on the right. It reads left to right like a sentence.
fruits = ["apple", "banana"]
# โŒ Avoid: this asks if the list is inside the word, which is not what you want
print(fruits in "banana")
# โœ… Good: value first, then the group you are searching
print("banana" in fruits)
  • Forgetting that list matching needs the whole item. A piece of a word will not match a list item.
names = ["alexander"]
# โŒ Avoid: "alex" is not the same item as "alexander"
print("alex" in names) # False
# โœ… Good: search the text inside the string instead
print("alex" in "alexander") # True
  • Forgetting case matters. The values "Riya" and "riya" are different, so a check can come back False when you expected True.

โœ… Best Practices

Small habits that keep these checks clean.

  • Use in instead of writing a loop when all you need is a yes-or-no answer. It is shorter and easier to read.
  • Read the check out loud before you trust it. The line โ€œis username in allowed_usersโ€ should sound like the question you actually mean to ask.
  • When user input is involved, make the case consistent. Lower both the input and the stored values so a capital letter does not cause a wrong answer.
  • Prefer not in over writing not (x in y). Both work, but not in reads the way you think.

๐Ÿงฉ What Youโ€™ve Learned

A quick recap of what you can now do.

  • โœ… Use in to check whether a value is inside a string, list, or other sequence, and get back True or False.
  • โœ… Use not in for the opposite check, when you want to know if a value is missing.
  • โœ… Know that list checks match a whole item, while string checks can find a smaller piece of text inside.
  • โœ… Replace a yes-or-no loop with a single clean membership check, like an allowed-users gate.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does `"a" in "cat"` return?

    Why: The letter "a" really is inside the word "cat", so the result is the boolean True.

  2. 2

    Given `fruits = ["apple", "banana"]`, what is `"app" in fruits`?

    Why: With a list, in matches a whole item, and "app" is not the same item as "apple", so it is False.

  3. 3

    Which operator checks whether a value is missing from a sequence?

    Why: not in returns True when the value is not found inside the sequence.

  4. 4

    What does `"shipped" in "Your order has shipped"` return?

    Why: On a string, in searches for the substring, and those exact letters appear in the sentence, so it is True.

๐Ÿš€ Whatโ€™s Next?

You can now ask whether a value lives inside a group. Next we look at a different question. Are these two things actually the very same object in memory? That is what identity operators are for.

Identity Operators

Share & Connect