Account
Categories

IS Operator (is) in Python


Definition:

It checks whether two variables refer to the same memory location; if they do, it returns True, otherwise False.

Now you will clearly understand with the help of the diagram.

a (variable) ────────────────┐
                             ↓
                        ┌───────────┐
                        │   1, 2    │  ← (memory / object)
                        └─────↑─────┘
                              │
                            value 
                             ↑
b (variable) ────────────────┘

Syntax:

a is b

Example:

a = [1, 2]
b = a
print(a is b)

Output:

Exercise

a = [1, 2] and b = a. Check whether a is b.