0
0
Pythonprogramming~10 mins

Membership operators (in, not in) in Python - Step-by-Step Execution

Choose your learning style9 modes available
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.