What is == in python?

In Python and many other programming languages, a single equal mark is used to assign a value to a variable, whereas two consecutive equal marks is used to check whether 2 expressions give the same value .

= is an assignment operator

== is an equality operator

(x==y) is False because we assigned different values to x and y.

(y==z) is True because we assign equal values to y and z.



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:

Example:

a = 4
b = 4
print(a is b)
print(id(a))
print(id(b))

Output:

True

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. 

Example:

l1 = []
l2 = []
print(l1 is l2)
print(id(l1))
print(id(l2))

Output:

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: 

Example:

list1= []
list2 = []
print(list1 == list2)

Output:

True

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. 

For example:

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.

Example: 

class B:
 def __eq__(self, other):
 return True
a = B()
print(var_a == 1)
print(var_a == None)
print(var_a is None)

Output: 

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)

Example: 

var_a = 256
var_b = 256
print(id(var_a))
print(id(var_b))

Output:

140733843347264
140733843347264

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. 

For example:

var_a = 257
var_b = 257
print(id(var_a))
print(id(var_b))

Output:

1407339734472
1947175682672

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​
id(var_a)

Output:

140545546785872

id(var_b)

Output:

140545546785872
Var_a is var_b

Output:

True
var_a == var_b

Output:

True

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[:]
id(var_a)

Output:

140545546785872
id(var_c)

Output:

140549878587008
var_a is var_c

Output:

False
var_a == var_c

Output:

True

Tus var_a and var_c both refer to different objects in the memory.

Python 'is' vs '==' Operator: Head-to-Head Comparison

Parameters

is

==

What it compares

Checks if two objects are same or not

Checks it the value of two objects is same or not

Comparison

Compares variables and strings

Compares only variables

Type of Operator

Identity operator

Equality operator

Conclusion 

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

What is == in python?

Frequently Asked Questions

1. 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. 

2. 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. 

3. 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. 

4. 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.

5. How Do You Write Equal in Python?

We can write “equal” in Python using the (==) operator. 

6. What is the Difference Between = and == in Python?

“=” assigns a value to a variable, while “==” verifies the Python equality of two different variables. 

Related reading:

  • Best Python Books
  • Best Python Compilers
  • Best Python Interpreters
  • How to Run a Python Script
  • Python for Data Science
  • Python vs PHP
  • Python vs Java

What is == in Python example?

Comparison operators are used to compare values. It returns either True or False according to the condition. ... Comparison operators..

What does double == mean in Python?

Difference between == and = in Python. In Python and many other programming languages, a single equal mark is used to assign a value to a variable, whereas two consecutive equal marks is used to check whether 2 expressions give the same value .

What does == represent?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .