Bird
Raised Fist0
Pythonprogramming~10 mins

Set membership testing in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Set membership testing
Start with a set
Check if element in set?
NoReturn False
Yes
Return True
End
The program checks if an element is inside a set and returns True if found, otherwise False.
Execution Sample
Python
my_set = {1, 2, 3}
print(2 in my_set)
print(5 in my_set)
This code tests if 2 and 5 are members of the set {1, 2, 3} and prints the results.
Execution Table
StepExpressionEvaluationResultOutput
1my_set = {1, 2, 3}Create set with elements 1, 2, 3{1, 2, 3}
22 in my_setIs 2 in {1, 2, 3}?TrueTrue
35 in my_setIs 5 in {1, 2, 3}?FalseFalse
💡 All membership tests completed and results printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
my_setundefined{1, 2, 3}{1, 2, 3}{1, 2, 3}
2 in my_setundefinedundefinedTrueTrue
5 in my_setundefinedundefinedundefinedFalse
Key Moments - 2 Insights
Why does '2 in my_set' return True but '5 in my_set' returns False?
Because 2 is an element inside the set {1, 2, 3} (see step 2 in execution_table), but 5 is not (see step 3).
Is the set changed after membership testing?
No, the set remains the same throughout (see variable_tracker for 'my_set' values). Membership testing only checks presence.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the result of '2 in my_set'?
ATrue
BFalse
CError
DNone
💡 Hint
Check the 'Result' column in execution_table row for step 2.
At which step does the membership test return False?
AStep 2
BStep 3
CStep 1
DNo step returns False
💡 Hint
Look at the 'Result' column in execution_table for each step.
If we test '3 in my_set', what would be the expected output?
AError
BFalse
CTrue
DNone
💡 Hint
Refer to variable_tracker to see if 3 is in the set {1, 2, 3}.
Concept Snapshot
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.
Full Transcript
This lesson shows how Python checks if an element is inside a set using the 'in' keyword. We start by creating a set with elements 1, 2, and 3. Then we test if 2 is in the set, which returns True because 2 is present. Next, we test if 5 is in the set, which returns False because 5 is not present. The set itself does not change during these tests. This is a quick way to check membership in a collection.

Practice

(1/5)
1.

Which operator is used to check if an element exists inside a set in Python?

easy
A. contains
B. has
C. exists
D. in

Solution

  1. Step 1: Understand set membership

    In Python, the in keyword checks if an element is present in a set.
  2. Step 2: Identify correct operator

    Other options like has, exists, and contains are not valid Python operators for membership testing.
  3. Final Answer:

    in -> Option D
  4. Quick Check:

    Use in to test membership [OK]
Hint: Remember: use 'in' to check if item is inside a set [OK]
Common Mistakes:
  • Using 'has' instead of 'in'
  • Trying 'exists' keyword
  • Using 'contains' which is not a Python operator
2.

Which of the following is the correct syntax to check if 5 is NOT in the set {1, 2, 3, 4}?

easy
A. 5 not in {1, 2, 3, 4}
B. not 5 in {1, 2, 3, 4}
C. 5 !in {1, 2, 3, 4}
D. 5 notinside {1, 2, 3, 4}

Solution

  1. Step 1: Recall correct syntax for 'not in'

    The correct syntax to check if an element is not in a set is: element not in set.
  2. Step 2: Evaluate each option

    5 not in {1, 2, 3, 4} uses correct syntax. not 5 in {1, 2, 3, 4} is incorrect because 'not' must come after the element. 5 !in {1, 2, 3, 4} uses invalid operator '!in'. 5 notinside {1, 2, 3, 4} uses a non-existent keyword 'notinside'.
  3. Final Answer:

    5 not in {1, 2, 3, 4} -> Option A
  4. Quick Check:

    Use 'not in' with element first [OK]
Hint: Write 'element not in set' to check absence [OK]
Common Mistakes:
  • Using '!in' instead of 'not in'
  • Placing 'not' before element
  • Using invalid keywords like 'notinside'
3.

What will be the output of the following code?

my_set = {10, 20, 30}
print(25 in my_set)
medium
A. True
B. False
C. 25
D. Error

Solution

  1. Step 1: Understand the set contents

    The set my_set contains 10, 20, and 30. It does not contain 25.
  2. Step 2: Evaluate membership test

    The expression 25 in my_set checks if 25 is in the set. Since it is not, the result is False.
  3. Final Answer:

    False -> Option B
  4. Quick Check:

    25 not in set means False [OK]
Hint: If element missing in set, 'in' returns False [OK]
Common Mistakes:
  • Assuming output is the element itself
  • Confusing True/False for membership
  • Expecting an error for missing element
4.

Find the error in this code snippet:

my_set = {1, 2, 3}
if 4 notin my_set:
    print("4 is not in the set")
medium
A. The keyword 'notin' is invalid
B. The set declaration is wrong
C. Missing colon after if statement
D. Print statement syntax error

Solution

  1. Step 1: Check syntax of membership test

    The correct keyword to check absence is not in with a space, not notin.
  2. Step 2: Verify other parts of code

    The set declaration is correct, colon after if is present, and print syntax is valid.
  3. Final Answer:

    The keyword 'notin' is invalid -> Option A
  4. Quick Check:

    Use 'not in' with space, not 'notin' [OK]
Hint: Remember: 'not in' has a space between words [OK]
Common Mistakes:
  • Writing 'notin' as one word
  • Forgetting colon after if
  • Misreading print syntax
5.

Given the list nums = [1, 2, 2, 3, 4, 4, 5], which code snippet correctly prints Found only if 3 is in the unique set of numbers?

A) if 3 in nums:
       print("Found")

B) if 3 in set(nums):
       print("Found")

C) if 3 not in set(nums):
       print("Found")

D) if 3 not in nums:
       print("Found")
hard
A. if 3 in nums: print("Found")
B. if 3 not in nums: print("Found")
C. if 3 in set(nums): print("Found")
D. if 3 not in set(nums): print("Found")

Solution

  1. Step 1: Understand the requirement

    We want to check if 3 is in the unique set of numbers from the list, so duplicates are ignored.
  2. Step 2: Analyze each option

    if 3 in nums: print("Found") checks 3 in the list (with duplicates). if 3 in set(nums): print("Found") converts list to set and checks membership correctly. if 3 not in set(nums): print("Found") prints 'Found' if 3 is NOT in the set, which is wrong. if 3 not in nums: print("Found") checks 3 not in list, also wrong.
  3. Final Answer:

    if 3 in set(nums): print("Found") -> Option C
  4. Quick Check:

    Convert list to set before membership test [OK]
Hint: Convert list to set before membership test for uniqueness [OK]
Common Mistakes:
  • Checking membership directly in list with duplicates
  • Using 'not in' instead of 'in'
  • Confusing list and set membership