Concept Flow - Membership operators (in, not in)
Start
Check if element in collection
Return True
End
The program checks if an element exists inside a collection and returns True if found, otherwise False.
fruits = ['apple', 'banana', 'cherry'] print('banana' in fruits) print('grape' not in fruits)
| Step | Expression | Evaluation | Result | Output |
|---|---|---|---|---|
| 1 | 'banana' in fruits | 'banana' found in ['apple', 'banana', 'cherry'] | True | True |
| 2 | 'grape' not in fruits | 'grape' not found in ['apple', 'banana', 'cherry'] | True | True |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| fruits | ['apple', 'banana', 'cherry'] | ['apple', 'banana', 'cherry'] | ['apple', 'banana', 'cherry'] | ['apple', 'banana', 'cherry'] |
Membership operators check if an element is inside a collection. Use 'in' to test presence (returns True if found). Use 'not in' to test absence (returns True if not found). Commonly used with lists, strings, sets. Example: 'x in list' or 'x not in list'.