   Identity Operators: "Is" and "Is not": examples and exercises

If you're new to programming, you might have heard the term "identity operators," but don't know exactly what they are or how to use them. In this article, we'll explore identity operators in Python, specifically "is" and "is not." We'll go through examples and exercises to help you understand these concepts better.

## What are Identity Operators?

Identity operators are used to compare the memory locations of two objects in Python. They check if the objects have the same identity, meaning they refer to the same location in memory. The two identity operators are "is" and "is not."

The "is" operator returns True if the two objects being compared have the same identity, while the "is not" operator returns True if they do not have the same identity.

## Examples of Identity Operators

Let's take a look at some examples to understand how identity operators work.

Example 1:

``````x = 5
y = 5
print(x is y)``````

Output: `True`

In this example, we declare two variables x and y and assign them both the value of 5. When we print out whether x is y using the "is" operator, we get True because x and y have the same identity since they refer to the same location in memory.

Example 2:

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

Output: `False`

In this example, we declare two variables a and b and assign them both the list [1, 2, 3]. When we print out whether a is b using the "is" operator, we get False because a and b do not have the same identity since they refer to different locations in memory.

Example 3:

``````c = None
d = None
print(c is d)``````

Output: `True`

In this example, we declare two variables c and d and assign them both the value of None. When we print out whether c is d using the "is" operator, we get True because both variables refer to the same location in memory since None is a singleton object in Python.

## Exercises on Identity Operators

Let's try some exercises to practice using identity operators in Python.

Exercise 1:

Predict the output of the following code:

``````x = "Hello"
y = "Hello"
print(x is y)``````

Exercise 2:

Fill in the blank to make the following code return `True`:

``````a = [1, 2, 3]
b = [1, 2, 3]
print(a _______ b)``````

Exercise 3:

Predict the output of the following code:

``````e = 5
f = None
print(e is not f)``````

## Conclusion

Identity operators are used to compare the memory locations of two objects in Python. They check if the objects have the same identity, meaning they refer to the same location in memory.

The two identity operators are "is" and "is not." We explored examples and exercises to help you understand these concepts better. Practice using identity operators to become more comfortable with them and improve your Python skills! Top Tutorials