Membership Operators

Membership operators in Python are used to test if a value or variable is found in a sequence (such as a string, list, tuple, etc.).

in: Returns True if the value is found in the sequence.

Syntax: value in sequence

not in: Returns True if the value is not found in the sequence.

Syntax: value not in sequence

Python program that demonstrates the use of Membership operators:

# Define a list and a string
fruits = ['apple', 'banana', 'cherry']
text = 'Hello, World!'

# Using the 'in' operator
print('apple' in fruits)  # True
print('grape' in fruits)  # False
print('World' in text)    # True
print('world' in text)    # False

# Using the 'not in' operator
print('apple' not in fruits)  # False
print('grape' not in fruits)  # True
print('World' not in text)    # False
print('world' not in text)    # True

Output

True
False
True
False
False
True
False
True
What Are People Saying About Us

Read These Life Changing Stories From People Just Like You!