+91 81900 98289

Identity Operators

This Identity Operators lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

is asks whether two names point at the same object. == asks whether the values are equal.

Use is None and is not None. Use == for numbers, text, and lists.

Example 1: Start here

Two equal lists are usually two objects, so is is false even when == is true.

left = None # no value yet print(left is None) # is checks the same object, not just equal contents print([1] == [1]) # a comparison is True or False print([1] is [1]) # is checks the same object, not just equal contents

Example 2: Same object

is asks if two names point at the same object. None is one shared object.

a = None # no value yet b = None # no value yet print(a is b) # is checks the same object, not just equal contents

Example 3: Equal is not always is

== compares the contents. is compares identity. Two new lists are equal but not the same object.

print([1, 2] == [1, 2]) # a comparison is True or False print([1, 2] is [1, 2]) # is checks the same object, not just equal contents

Example 4: is not

is not is the opposite test. Use it when you want to be sure a value was set.

city = "Chennai" # text print(city is not None) # is checks the same object, not just equal contents
Two equal lists are usually two objects, so is is false even when == is true.
Call Now 81900 98289