Bird
Raised Fist0
Pythonprogramming~10 mins

Identity operators (is, is not) 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 - 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.

Practice

(1/5)
1. What does the is operator check in Python?
easy
A. If two variables are of the same type
B. If two variables have the same value
C. If two variables point to the same object
D. If two variables are both numbers

Solution

  1. Step 1: Understand the is operator

    The is operator checks whether two variables refer to the exact same object in memory.
  2. Step 2: Differentiate from equality

    Unlike ==, which checks if values are equal, is checks identity, meaning the same object.
  3. Final Answer:

    If two variables point to the same object -> Option C
  4. Quick Check:

    is checks object identity = C [OK]
Hint: Remember: is means same object, not just equal value [OK]
Common Mistakes:
  • Confusing is with ==
  • Thinking is checks value equality
  • Assuming is works like type comparison
2. Which of the following is the correct syntax to check if variable a is not the same object as b?
easy
A. a != b
B. a is not b
C. a not is b
D. a !== b

Solution

  1. Step 1: Identify the correct identity operator

    The operator to check if two variables are not the same object is is not.
  2. Step 2: Check syntax correctness

    a is not b is the correct syntax; other options are either value comparison or invalid syntax.
  3. Final Answer:

    a is not b -> Option B
  4. Quick Check:

    Correct syntax for identity not equal = D [OK]
Hint: Use is not to check different objects, not != [OK]
Common Mistakes:
  • Using != instead of is not
  • Writing not is which is invalid
  • Using JavaScript style !== in Python
3. What will be the output of this code?
list1 = [1, 2, 3]
list2 = list1
list3 = [1, 2, 3]
print(list1 is list2)
print(list1 is list3)
medium
A. True\nFalse
B. True\nTrue
C. False\nTrue
D. False\nFalse

Solution

  1. Step 1: Analyze list1 is list2

    Since list2 = list1, both point to the same object, so this is True.
  2. Step 2: Analyze list1 is list3

    list3 is a new list with the same values but a different object, so this is False.
  3. Final Answer:

    True\nFalse -> Option A
  4. Quick Check:

    Same object check = True, different object = False [OK]
Hint: Same variable means is True; identical values but new object means False [OK]
Common Mistakes:
  • Assuming identical lists are the same object
  • Confusing is with ==
  • Ignoring that assignment copies reference, not value
4. Find the error in this code snippet:
a = None
if a is not None:
    print("Value exists")
else
    print("No value")
medium
A. Missing colon after else
B. Wrong use of is not
C. None should be in quotes
D. Indentation error

Solution

  1. Step 1: Check syntax of if-else statement

    The else line is missing a colon at the end, which is required in Python.
  2. Step 2: Verify other parts

    The use of is not with None is correct, and None is a keyword, so no quotes needed. Indentation looks fine.
  3. Final Answer:

    Missing colon after else -> Option A
  4. Quick Check:

    Colon needed after else = A [OK]
Hint: Always put a colon after else in Python [OK]
Common Mistakes:
  • Forgetting colon after else
  • Using quotes around None
  • Misusing is not operator
5. You want to check if a variable x is exactly None before processing it. Which is the best way to do this?
hard
A. if x is not None:
B. if x == None:
C. if x != None:
D. if x is None:

Solution

  1. Step 1: Understand the difference between == and is for None

    None is a singleton object in Python. The recommended way to check for it is using is because it checks identity.
  2. Step 2: Choose the correct condition for checking if x is None

    if x is None: correctly checks if x points to the None object.
  3. Final Answer:

    if x is None: -> Option D
  4. Quick Check:

    Use is None to check for None [OK]
Hint: Always use is None to check for None [OK]
Common Mistakes:
  • Using == None instead of is None
  • Using is not None when checking for None
  • Confusing identity with equality for None