🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Identity Operators

AI & DATA SCIENCE // identity-operators

Identity operators (is, is not) check whether two names refer to the exact same object in memory, rather than whether their values are equal.

Syntax

a is b
a is not b

Deep Dive Course

`a is b` checks that a and b are literally the same object, comparing their identities rather than just checking that they're equal in value. This distinction matters for mutable objects: two separately created lists with identical contents are equal but not the same object. is is the correct, idiomatic way to check for None, True, and False specifically, since Python guarantees there's only ever one instance of each of those in a running program.

1Understanding Identity Operators

a is b checks that a and b are literally the same object, comparing their identities rather than just checking that they're equal in value. This distinction matters for mutable objects: two separately created lists with identical contents are equal but not the same object. is is the correct, idiomatic way to check for None, True, and False specifically, since Python guarantees there's only ever one instance of each of those in a running program.

💡

Never use is to compare numbers or strings for equality — small integers and short strings happen to be cached and reused by CPython, which can make is appear to 'work' by accident, but that behavior isn't guaranteed and shouldn't be relied on.

editor.html
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
localhost:3000

2Practical Example

Here is a real-world application of Identity Operators showing how it is used in production Python code.

editor.html
value = None
if value is None:
    print("No value provided")
localhost:3000

3Best Practices

Follow these guidelines when working with Identity Operators:

1. Use is None / is not None instead of == for checking None

2. Use ==, not is, to compare the value/content of two objects, like two lists or two custom instances

3. Don't rely on small-integer or string caching behavior with is — it's a CPython implementation detail, not a language guarantee

⚠️

Tip: Never use is to compare numbers or strings for equality — small integers and short strings happen to be cached and reused by CPython, which can make is appear to 'work' by accident, but that behavior isn't guaranteed and shouldn't be relied on.

editor.html
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
localhost:3000

Examples

Example 01Basic Usage
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
Example 02Advanced Example
value = None
if value is None:
    print("No value provided")

Best Practices

  • Use is None / is not None instead of == for checking None
  • Use ==, not is, to compare the value/content of two objects, like two lists or two custom instances
  • Don't rely on small-integer or string caching behavior with is — it's a CPython implementation detail, not a language guarantee

Interview Question

Why can two lists with identical contents be equal but fail an identity check?

Hint: Think about what each operator actually checks.

Equality compares the contents of two objects by calling __eq__, so two separately created lists holding the same elements in the same order are equal. Identity compares whether both names point to the exact same object in memory. Creating two lists with the same literal values allocates two distinct list objects, so they satisfy equality but fail identity, since they don't occupy the same memory location.

Exercises

MediumPractice using Identity Operators in a real scenario.
View Solution
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)

Frequently Asked Questions

Why can two lists with identical contents be equal but fail an identity check?

Equality compares the contents of two objects by calling __eq__, so two separately created lists holding the same elements in the same order are equal. Identity compares whether both names point to the exact same object in memory. Creating two lists with the same literal values allocates two distinct list objects, so they satisfy equality but fail identity, since they don't occupy the same memory location.

Related Functions

comparison-operatorsnonetypeid()