0
0
Pythonprogramming~10 mins

Identity operators (is, is not) in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Identity operators (is, is not)
Start
Evaluate x and y
Check: x is y?
YesExecute 'is' branch
No
Check: x is not y?
YesExecute 'is not' branch
No
End
The program compares two variables to see if they point to the same object using 'is' or 'is not' and executes code accordingly.
Execution Sample
Python
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)
print(a is c)
print(a is not c)
This code checks if variables refer to the same object using 'is' and 'is not' operators.
Execution Table
StepExpressionEvaluationResultOutput
1a = [1, 2, 3]Create list objecta points to list1
2b = aAssign b to same object as ab points to list1
3c = [1, 2, 3]Create new list objectc points to list2
4a is bCompare if a and b point to same objectTrueTrue
5a is cCompare if a and c point to same objectFalseFalse
6a is not cCompare if a and c do NOT point to same objectTrueTrue
💡 All expressions evaluated; program ends after printing results.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aundefinedlist1list1list1list1
bundefinedundefinedlist1list1list1
cundefinedundefinedundefinedlist2list2
Key Moments - 2 Insights
Why does 'a is b' return True but 'a is c' return False even though a and c have the same list values?
'a is b' is True because b points to the exact same list object as a (see step 2 and 4). 'a is c' is False because c is a new list object with the same values but different identity (step 3 and 5).
What does 'is not' check compared to 'is'?
'is not' checks if two variables do NOT point to the same object. In step 6, 'a is not c' is True because a and c are different objects.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of 'a is b' at step 4?
AError
BFalse
CTrue
DNone
💡 Hint
Check the 'Result' column in row for step 4 in the execution_table.
At which step does the variable 'c' get assigned a new list object?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Expression' and 'Evaluation' columns for when 'c' is assigned in the execution_table.
If we changed 'b = a' to 'b = [1, 2, 3]', what would 'a is b' evaluate to at step 4?
AFalse
BTrue
CError
DNone
💡 Hint
Refer to variable_tracker to see that 'b' would point to a new object, so 'a is b' would be False.
Concept Snapshot
Identity operators check if two variables point to the same object.
Use 'is' to test if they are the same object.
Use 'is not' to test if they are different objects.
They compare object identity, not values.
Example: a is b returns True if a and b point to the same object.
Full Transcript
This lesson shows how Python's identity operators 'is' and 'is not' work by comparing if two variables point to the same object in memory. We assign lists to variables and check if they are identical objects. The 'is' operator returns True if both variables point to the same object, and False otherwise. The 'is not' operator returns True if they point to different objects. We traced each step, showing variable assignments and comparisons, and explained why two lists with the same values can be different objects. This helps beginners understand the difference between object identity and equality.