Bird
Raised Fist0
Pythonprogramming~10 mins

Membership operators (in, not in) 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 - 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.
Execution Sample
Python
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits)
print('grape' not in fruits)
This code checks if 'banana' is in the list fruits and if 'grape' is not in the list, printing True or False.
Execution Table
StepExpressionEvaluationResultOutput
1'banana' in fruits'banana' found in ['apple', 'banana', 'cherry']TrueTrue
2'grape' not in fruits'grape' not found in ['apple', 'banana', 'cherry']TrueTrue
💡 Both membership checks completed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
fruits['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']
Key Moments - 2 Insights
Why does 'banana' in fruits return True but 'grape' not in fruits also return True?
'banana' in fruits returns True because 'banana' is inside the list (see Step 1). 'grape' not in fruits returns True because 'grape' is not in the list (see Step 2). Both check presence but one tests for existence, the other for absence.
Does 'in' check for exact matches or partial matches?
'in' checks for exact matches of elements in the collection. It does not check substrings or partial matches. For example, 'app' in fruits would be False because 'app' is not an element (not shown in table but important).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of the expression 'banana' in fruits at Step 1?
AFalse
BError
CTrue
DNone
💡 Hint
Check the 'Result' and 'Output' columns in Step 1 of the execution table.
At which step does the program confirm that an element is NOT in the list?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at the 'Expression' column to find the 'not in' operator usage.
If we change 'banana' to 'grape' in the first expression, what would be the output at Step 1?
AFalse
BError
CTrue
DNone
💡 Hint
Refer to the variable_tracker and execution_table to see if 'grape' is in the list.
Concept Snapshot
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'.
Full Transcript
This lesson shows how Python checks if an item is inside a collection using membership operators 'in' and 'not in'. The code example uses a list of fruits and checks if 'banana' is in the list and if 'grape' is not in the list. The execution table traces each check step-by-step, showing the condition, evaluation, and output. Variables remain unchanged during these checks. Key moments clarify why 'in' and 'not in' return True in different cases and emphasize that 'in' checks for exact matches only. The quiz tests understanding of outputs and operator behavior. The snapshot summarizes the concept for quick review.

Practice

(1/5)
1. What does the in operator do in Python?
easy
A. Creates a new collection
B. Adds a value to a collection
C. Removes a value from a collection
D. Checks if a value exists inside a collection

Solution

  1. Step 1: Understand the purpose of in

    The in operator is used to check if a value is present inside a collection like a list, string, or tuple.
  2. Step 2: Compare with other options

    Options A, B, and C describe actions unrelated to membership checking.
  3. Final Answer:

    Checks if a value exists inside a collection -> Option D
  4. Quick Check:

    in means membership check [OK]
Hint: Remember: in means 'is inside' [OK]
Common Mistakes:
  • Confusing in with adding or removing items
  • Thinking in changes the collection
  • Mixing in with comparison operators
2. Which of the following is the correct syntax to check if the string 'apple' is NOT in the list fruits?
easy
A. if 'apple' not in fruits:
B. if 'apple' not fruits:
C. if not 'apple' fruits:
D. if 'apple' !in fruits:

Solution

  1. Step 1: Recall correct syntax for not in

    The correct syntax to check absence is value not in collection.
  2. Step 2: Evaluate each option

    if 'apple' not in fruits: matches correct syntax. if 'apple' not fruits: misses in. if not 'apple' fruits: misses in. if 'apple' !in fruits: uses invalid operator !in.
  3. Final Answer:

    if 'apple' not in fruits: -> Option A
  4. Quick Check:

    Use not in as two words [OK]
Hint: Use not in as two words, no symbols [OK]
Common Mistakes:
  • Writing !in instead of not in
  • Omitting in after not
  • Using not 'value' in which is valid but less clear
3. What is the output of this code?
letters = ['a', 'b', 'c']
print('d' in letters)
print('a' not in letters)
medium
A. True\nTrue
B. False\nTrue
C. False\nFalse
D. True\nFalse

Solution

  1. Step 1: Check if 'd' is in the list

    'd' is not in ['a', 'b', 'c'], so 'd' in letters is False.
  2. Step 2: Check if 'a' is not in the list

    'a' is in the list, so 'a' not in letters is False, so print('a' not in letters) prints False.
  3. Final Answer:

    False False is incorrect because the second print outputs False, but the first print outputs False, so the correct output is False False which matches False\nFalse.
  4. Quick Check:

    'd' in letters = False, 'a' not in letters = False [OK]
Hint: Check each membership separately [OK]
Common Mistakes:
  • Assuming 'd' is in the list
  • Mixing up in and not in results
  • Confusing True/False outputs
4. Find the error in this code snippet:
items = ['pen', 'pencil', 'eraser']
if 'pen' not items:
    print('Pen is missing')
medium
A. Incorrect print statement syntax
B. Wrong list name
C. Missing in after not
D. No error, code is correct

Solution

  1. Step 1: Check the membership syntax

    The correct syntax for checking absence is value not in collection. The code misses in after not.
  2. Step 2: Verify other parts

    List name and print statement are correct. The only error is missing in.
  3. Final Answer:

    Missing in after not -> Option C
  4. Quick Check:

    Use not in together [OK]
Hint: Always write not in together [OK]
Common Mistakes:
  • Writing not items instead of not in items
  • Forgetting in keyword
  • Assuming not alone checks membership
5. You have a list words = ['cat', 'dog', 'bird']. You want to print all words that are NOT in the string text = 'I have a dog and a cat'. Which code correctly does this?
hard
A. for w in words: if w not in text: print(w)
B. for w in words: if w in text: print(w)
C. for w in words: if not w text: print(w)
D. for w in words: if w not text: print(w)

Solution

  1. Step 1: Understand the goal

    We want to print words from the list that are NOT found inside the string text.
  2. Step 2: Check each option's logic

    for w in words: if w not in text: print(w) uses if w not in text, which correctly checks absence. for w in words: if w in text: print(w) prints words that ARE in text, opposite of goal. for w in words: if not w text: print(w) has syntax error, missing in after w. for w in words: if w not text: print(w) has syntax error missing in.
  3. Final Answer:

    for w in words: if w not in text: print(w) -> Option A
  4. Quick Check:

    Use not in for absence check [OK]
Hint: Use if w not in text to find missing words [OK]
Common Mistakes:
  • Omitting in keyword in various positions
  • Forgetting in keyword
  • Printing words that are present instead of absent