Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.
The Difference Between Python 'is' vs '==' Operator [Easy Guide]
Table of Contents
Python is a general-purpose, high-level, interpreted programming language. Gan Guido Rossum designed this language and released Python 0.9.0 in 1991. Python is well-known for its code readability and easy syntax, which uses simple English keywords and eliminates the use of semicolons and curly brackets.
There are several scenarios where you will be required to compare two objects within your code. Also, in Python, comparing objects and variables is a common task. Python provides two different ways to compare objects and variables. The first is the equality operator (==), and the second is the (is) operator.
This article will walk you through the key differences between Python “is” and “==”, along with their examples. Also, it discusses ‘is’ and ‘==’ separately to better understand.
What Is the “is” Keyword?
The “is” keyword is used to verify whether the two variables belong to the same object. If both the variables belong to the same object, it returns TRUE, otherwise FALSE.
For example, we have two variables, “a” and “b”, and both have the same value as “4”, as shown below. If we use the “is” operator on both the variables and they have the same value, it must return true.
In Python, the variables that have the same values are all assigned the same object ID in the memory. Here’s an example of how the “is” keyword works:
a = 4 b = 4 print(a is b) print(id(a)) print(id(b))
True 1644840201 1642650272
The “is” operator will only work well when the variables are assigned with some values. Consider another example where we have two empty lists, as shown below. In Python, the two different lists will refer to two different objects with different memory locations, making both lists distinct.
l1 =  l2 =  print(l1 is l2) print(id(l1)) print(id(l2))
False 2286738320968 2241222725312
You can see that the above output displays different values for both the list (l1 and l2) variables even though they are empty lists. Therefore, the output is false. To overcome this confusion, we jump to the (==) operator.
What Is the “==” Operator?
There is a comparison operator (==) in Python used to measure the Python equality of two objects. It is also known as the equality operator (==).
The above example, where we have compared the two empty lists using the (is) operator, did not provide the expected results. We will now try that example with the (==) operator:
list1=  list2 =  print(list1 == list2)
The (==) operator will return the true value, as it will consider the object id while comparing.
The Difference Between Python “is” vs “==” Operator
Here’s the main difference between python “==” vs “is:”
The “is” keyword is used to compare the variables and string whether they are pointing to the same object or not. If both the variables (var1 and var2) refer to the same object, they will have the same ID.
The “==” operator will compare both the variables whether their values refer to the same object or not.
Both operators are used for comparing; however, their purpose is different. Thus, they are used in different scenarios.
In Python, everything is an object and is assigned some memory.
- Identity operators: The “is” and “is not” keywords are called identity operators that compare objects based on their identity.
- Equality operator: The “==” and “!=” are called equality operators that compare the objects based on their values. It will call the _eq_() class method of the object on the left of the operator and check for equality.
a is None
In this case, the interpreter will compare for its identity only and cannot be overruled. This means it will check if “a” refers to the None object and nothing else.
But with the following example:
a == None
The “==” operator will call the class a._eq_() method, which might work differently, depending on the interpreter, and provide a different result.
class B: def __eq__(self, other): return True a = B() print(var_a == 1) print(var_a == None) print(var_a is None)
True True False
The Python interpreter will intern the smaller numbers at fixed memory locations. It means that the interpreter will substantiate these values only once. It will refer to its memory address whenever the object is referenced. The range of these numbers might vary depending on your interpreter ( -5 to +256).
Working with Small Integers (Example)
var_a = 256 var_b = 256 print(id(var_a)) print(id(var_b))
Due to interning, we get the same memory location for 256.
But if we use the integer greater than the range, then the output may vary.
var_a = 257 var_b = 257 print(id(var_a)) print(id(var_b))
In the following example, we have used “var_a” to hold a list while “var_b” is a variable with the “a” value. Here, “var_a” and “var_b” both refer to the same object in the memory. You can use the “id” function to check it. Both variables will have the same id thus, a is b returns true.
var_a = [1, 2, 3] var_b = var_a
Var_a is var_b
var_a == var_b
Now, we will create a copy of the var_a variable using the slice operator. It will create a new object within the memory having a new address; thus, it will have a new ID.
var_c = var_a[:]
var_a is var_c
var_a == var_c
Tus var_a and var_c both refer to different objects in the memory.
Is There a Difference Between == and Is?
The (==) operator is an equality operator used to compare two objects to determine whether they are equal or not. On the flip side, the Python “is” operator is an identity operator that compare objects based on their identity.
What Is the ‘is’ Keyword in Python?
The “is” and “is not” keywords are called identity operators that check if the objects are exactly the same or not.
What Does == Mean in Python?
You can use this operator to compare the value of two objects to determine whether they are equal or not. In Python, it is also referred to as the equality operator.
What Is the Difference between Equality and Identity Operators?
The “is” keyword is used to compare the variables and string whether they are pointing to the same object or not. If both the variables (var1 and var2) refer to the same object, they will have the same ID. However, the “==” operator will compare the values of both variables.
How Do You Write Equal in Python?
We can write “equal” in Python using the (==) operator.
What is the Difference Between = and == in Python?
“=” assigns a value to a variable, while “==” verifies the Python equality of two different variables.
We hope that this article has helped you understand how to use Python == vs is. Both operators have different usage but are used for the main purpose of comparison. The “is” keyword is called the identity operator that will check if the two objects are the same or not. On the contrary, == in Python works as an equality operator to determine whether they are equal or not. python == vs is
Now that you know what is vs == is, Python learning awaits you. Interested in expanding your Python knowledge? Check out our Python learning guide.
Checkout These Best Python Courses!
See why millions of people turn to Udemy’s real-world experts to learn Python. Learn at your own pace with hands-on exercises and quizzes