Identity Checks
Part of the Logic & Flow section of Coddy's Python journey — lesson 26 of 78.
In Python, the is and is not operators are used to check the identity of objects. Unlike the == operator, which checks if two variables have the same value, the is operator checks if two variables refer to the exact same object in memory.
Basic usage of is:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # Output: True
print(a is c) # Output: FalseIn this example, a and b refer to the same list object, so a is b returns True. However, a and c are different list objects with the same values, so a is c returns False.
Basic Usage of is not:
x = 5
y = 5
z = 10
print(x is not y) # Output: False
print(x is not z) # Output: TrueHere, x and y refer to the same integer object (due to Python's optimizations for small integers), so x is not y returns False. x and z are different objects, so x is not z returns True.
Identity checks are particularly useful when you want to determine if two variables are pointing to the same object in memory, rather than just having the same value.
Challenge
EasyCreate two variables, list1 and list2, where:
list1refers to a list[1, 2, 3].list2is assigned to the same object.
Use the is operator to check if list1 and list2 refer to the same object.
Use the is not operator to check if list1 and list2 refer to different objects.
Print the results of the checks.
Cheat sheet
The is and is not operators check object identity in memory, not just value equality like ==.
Using is to check if variables refer to the same object:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # Output: True
print(a is c) # Output: FalseUsing is not to check if variables refer to different objects:
x = 5
y = 5
z = 10
print(x is not y) # Output: False
print(x is not z) # Output: TrueIdentity checks determine if two variables point to the same object in memory, not just whether they have the same value.
Try it yourself
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter