PyPyPath

Module 3: Operators in Python

Membership Operators

Membership operators check whether something is inside a collection: in and not in. You will use these with lists, strings, and more.

About 6 minutes

Example code
fruits = ["apple", "banana", "mango"]
print("banana" in fruits)      # True
print("grape" not in fruits)   # True

word = "Python"
print("P" in word)  # True
in checks inside a container

["apple", "banana"]

list

"banana" in list?

True

Practice

Make a list of three colors. Check if 'blue' is in the list.

Key takeaways

  • in returns True if the item exists inside the collection.
  • not in is the opposite.
  • Works on lists, strings, and other sequences.

Quick check: Membership Operators

Question 1 of 2

"a" in "cat" returns?