Concept Flow - Set membership testing
Start with a set
Check if element in set?
No→Return False
Yes
Return True
End
The program checks if an element is inside a set and returns True if found, otherwise False.
my_set = {1, 2, 3}
print(2 in my_set)
print(5 in my_set)| Step | Expression | Evaluation | Result | Output |
|---|---|---|---|---|
| 1 | my_set = {1, 2, 3} | Create set with elements 1, 2, 3 | {1, 2, 3} | |
| 2 | 2 in my_set | Is 2 in {1, 2, 3}? | True | True |
| 3 | 5 in my_set | Is 5 in {1, 2, 3}? | False | False |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 |
|---|---|---|---|---|
| my_set | undefined | {1, 2, 3} | {1, 2, 3} | {1, 2, 3} |
| 2 in my_set | undefined | undefined | True | True |
| 5 in my_set | undefined | undefined | undefined | False |
Set membership testing syntax: element in set Returns True if element is in the set, else False. Sets are unordered collections of unique elements. Membership testing is fast and simple. Use it to check presence without changing the set.