Identity operators (is, is not) in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use identity operators like is and is not, we check if two things are the exact same object.
We want to see how long it takes to do this check as the data size changes.
Analyze the time complexity of the following code snippet.
my_list = [1, 2, 3, 4, 5]
other_list = my_list
if my_list is other_list:
print("They are the same object")
else:
print("They are different objects")
This code checks if two variables point to the exact same list object.
Look for loops or repeated checks.
- Primary operation: Single identity check using
is - How many times: Exactly once, no loops or recursion
Checking if two variables are the same object takes the same time no matter how big the objects are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same even if the data grows.
Time Complexity: O(1)
This means the check takes the same short time no matter how big the data is.
[X] Wrong: "Checking if two lists are the same with is takes longer if the lists are bigger."
[OK] Correct: The is operator only checks if both variables point to the same object, not their contents, so size does not affect the time.
Understanding identity checks helps you write clear and efficient code, and shows you know how Python handles objects behind the scenes.
What if we changed is to ==? How would the time complexity change?
Practice
is operator check in Python?Solution
Step 1: Understand the
Theisoperatorisoperator checks whether two variables refer to the exact same object in memory.Step 2: Differentiate from equality
Unlike==, which checks if values are equal,ischecks identity, meaning the same object.Final Answer:
If two variables point to the same object -> Option CQuick Check:
ischecks object identity = C [OK]
is means same object, not just equal value [OK]- Confusing
iswith== - Thinking
ischecks value equality - Assuming
isworks like type comparison
a is not the same object as b?Solution
Step 1: Identify the correct identity operator
The operator to check if two variables are not the same object isis not.Step 2: Check syntax correctness
a is not bis the correct syntax; other options are either value comparison or invalid syntax.Final Answer:
a is not b -> Option BQuick Check:
Correct syntax for identity not equal = D [OK]
is not to check different objects, not != [OK]- Using
!=instead ofis not - Writing
not iswhich is invalid - Using JavaScript style
!==in Python
list1 = [1, 2, 3] list2 = list1 list3 = [1, 2, 3] print(list1 is list2) print(list1 is list3)
Solution
Step 1: Analyze
Sincelist1 is list2list2 = list1, both point to the same object, so this isTrue.Step 2: Analyze
list1 is list3list3is a new list with the same values but a different object, so this isFalse.Final Answer:
True\nFalse -> Option AQuick Check:
Same object check = True, different object = False [OK]
is True; identical values but new object means False [OK]- Assuming identical lists are the same object
- Confusing
iswith== - Ignoring that assignment copies reference, not value
a = None
if a is not None:
print("Value exists")
else
print("No value")Solution
Step 1: Check syntax of if-else statement
Theelseline is missing a colon at the end, which is required in Python.Step 2: Verify other parts
The use ofis notwithNoneis correct, andNoneis a keyword, so no quotes needed. Indentation looks fine.Final Answer:
Missing colon after else -> Option AQuick Check:
Colon needed after else = A [OK]
- Forgetting colon after else
- Using quotes around None
- Misusing
is notoperator
x is exactly None before processing it. Which is the best way to do this?Solution
Step 1: Understand the difference between
==andisfor NoneNoneis a singleton object in Python. The recommended way to check for it is usingisbecause it checks identity.Step 2: Choose the correct condition for checking if
xis Noneif x is None:correctly checks ifxpoints to theNoneobject.Final Answer:
if x is None: -> Option DQuick Check:
Useis Noneto check for None [OK]
is None to check for None [OK]- Using
== Noneinstead ofis None - Using
is not Nonewhen checking for None - Confusing identity with equality for None
