Identity Operator in PythonLast Updated : 12 Jun 2025 In Python, identity operators are special operators used to compare the memory locations of two objects. Instead of comparing the values held by the variables, they check whether two variables refer to the exact same object in memory. Python provides two identity operators:
These are particularly useful in situations where it matters whether two variables point to the same object, not just equivalent values. Python 'is' operatorThe 'is' operator in Python checks whether two variables refer to the same object in memory. Here is a simple example of Python 'is' operator: ExampleExecute NowOutput: True Explanation: In the above example, we have used the 'is' operator to check if x and y refer to the exact same object in the memory. Note: Python reuses small integer objects (from -5 to 256) to save memory. So, variables with the same value in this range may point to the same object.Let us now see another example: ExampleExecute NowOutput: a is b: True x is y: False list1 == list2: True list1 is list2: False list1 is list3: True s1 is s2: True s1 == s3: True s1 is s3: False val is None: True dict1 is dict2: False Explanation: The 'is' operator checks if two variables point to the same object in memory. For small numbers and simple strings, Python often reuses the same object, so it can return True. But for larger numbers, lists, dictionaries, or newly created strings, even if the values are the same, they are usually stored separately, so it returns False. It's also commonly used to check if something is None, like val is None. Python 'is not' OperatorThe 'is not' operator in Python is used to check if two variables do NOT refer to the same object in memory. Here is an example of Python 'is not' operator: ExampleExecute NowOutput: True Explanation: In this example, we used the 'is not' operator to check if both variables pointing to the same object. Since both variables are referring to the different objects in memory, the output returns as True. Let us see another example: ExampleExecute NowOutput: a is not b: False x is not y: True list1 is not list2: True list1 != list2: False list1 is not list3: False str1 is not str2: False str1 is not str3: True value is not None: False car1 is not car2: True car1 is not car3: False Explanation: In Python, the expression x is not y checks if x and y are not the same object in memory. It returns True if they are two separate objects, even if they look the same or have the same value. Note: Even if two things look the same, they might still be different objects.Difference Between 'is' and '=='The following table shows the difference between 'is' and '==':
Let us understand the difference between the two with the help of an example: ExampleExecute NowOutput: var_1 == var_2 => True var_1 == var_3 => True var_1 is var_2 => False var_1 is var_3 => True Explanation: In this example, we can clearly see the difference between the '==' and 'is' operators. ConclusionIn Python, the 'is' and 'is not' operators are used to check if two variables point to the same object in memory. They don't compare values like '==' does. Use is when you want to see if two things are exactly the same object, and use is not to check if they are different objects. This is especially helpful when checking for None or when working with mutable data like lists and dictionaries. Identity Operator in Python MCQs1. Which of the following is an identity operator in Python?
Answer: b) is 2. What does the 'is' operator check for in Python?
Answer: c) Identity (memory location) of objects 3. What will be the output of the following code?
Answer: a) True 4. What will be the output of the following code?
Answer: b) False 5. Which of the following identity operators returns True if two variables do not refer to the same object?
Answer: c) is not Next TopicFactorial-program-in-python |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India