Skip to main content

Membership Operators

A membership operator tells you whether a sequence appears in an object:

Membership Operators Table

main.py
# Operator | Description                                                                      | Example
# in       | Returns True if a sequence with the specified value is present in the object     | x in y
# not in   | Returns True if a sequence with the specified value is not present in the object | x not in y

Examples

Example

See if "banana" is one of the items in a list:

Prompt
Write a Python program that creates a list of fruits and checks if "banana" is present in the list using the "in" operator. Print the result.
Output: complete Python program

Example

See if "pineapple" is missing from a list:

Prompt
Write a Python program that creates a list of fruits and checks if "pineapple" is NOT present in the list using the "not in" operator. Print the result.
Output: complete Python program

Membership in Strings

Strings work with membership operators too:

Example

Prompt
Write a Python program that demonstrates membership operators with strings. Check if "H" is in the text, if "hello" is in the text, and if "z" is not in the text. Print the results.
Output: complete Python program