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
Recall & Review
beginner
What does the in keyword do when used with a set in Python?
It checks if a value is present in the set and returns True if it is, otherwise False.
Click to reveal answer
beginner
How do you check if an element is NOT in a set?
Use the not in keyword. For example, if element not in my_set: checks if element is not a member of my_set.
Click to reveal answer
intermediate
Why is set membership testing faster than list membership testing?
Sets use a hash table internally, so checking membership is on average very fast (constant time), while lists check each item one by one (linear time).
Click to reveal answer
beginner
What will be the output of this code?
my_set = {1, 2, 3}
print(2 in my_set)
print(5 in my_set)
True
False
Because 2 is in the set but 5 is not.
Click to reveal answer
beginner
Can you use the in keyword to check membership in other data types besides sets?
Yes! You can use in with lists, tuples, strings, dictionaries (checks keys), and more.
Click to reveal answer
What does element in my_set return if element is not in my_set?
ARaises an error
BFalse
CNone
DTrue
✗ Incorrect
The expression returns False if the element is not found in the set.
Which data structure generally provides the fastest membership testing?
ASet
BTuple
CList
DString
✗ Incorrect
Sets use hash tables internally, making membership testing very fast.
What keyword checks if an item is NOT in a set?
Anot in
Bnot
Cout
Dno in
✗ Incorrect
The 'not in' keyword checks if an item is absent from a set.
What will print('a' in {'a', 'b', 'c'}) output?
AError
BFalse
CTrue
DNone
✗ Incorrect
The letter 'a' is in the set, so it prints True.
Which of these is NOT a valid use of in for membership testing?
AChecking if a key is in a dictionary
BChecking if a value is in a list
CChecking if a character is in a string
DChecking if a number is in an integer
✗ Incorrect
You cannot check membership inside an integer because it is not a collection.
Explain how to check if an item is in a set and why this is efficient.
Think about how sets store items differently than lists.
You got /3 concepts.
Describe the difference between using 'in' and 'not in' with sets.
One checks if something is there, the other checks if it is missing.
You got /3 concepts.
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
Step 1: Understand set membership
In Python, the in keyword checks if an element is present in a set.
Step 2: Identify correct operator
Other options like has, exists, and contains are not valid Python operators for membership testing.
Final Answer:
in -> Option D
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
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.
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'.
Final Answer:
5 not in {1, 2, 3, 4} -> Option A
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
Step 1: Understand the set contents
The set my_set contains 10, 20, and 30. It does not contain 25.
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.
Final Answer:
False -> Option B
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
Step 1: Check syntax of membership test
The correct keyword to check absence is not in with a space, not notin.
Step 2: Verify other parts of code
The set declaration is correct, colon after if is present, and print syntax is valid.
Final Answer:
The keyword 'notin' is invalid -> Option A
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
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.
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.
Final Answer:
if 3 in set(nums):
print("Found") -> Option C
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