• Register
150 points
4

Python is a high level and most popular programming language nowadays. So in this tutorial, we will discuss about difference between two operators in Python. If you are a new coder in Python then, don’t worry. On this page, you will learn all about the operators and comparison between ‘is’ and ‘==’ in python.

Table of Content:

What are operators in Python?

What's the 'is' operator in Python?

What is ‘==’ operators in Python?

What are operators in Python?

Operators are just like the symbols and these symbols make some operations in-between values. You have been also learn about the operators in another programming language like C. So operators are like symbols that's make some operations if we use in the between values.

Python has different types of operators like logical, arithmetic, assignment etc. 

What is the ‘is’ operator in Python and what’s works?

In Python ‘is’ operator check the two values, list are the same items, or not and it does not work like comparison operator. So here is a code example.

Code:

a = [1,2,3]
b = [1,2,3]
if a is b:
	print("True")
else:
	print("False")
	
print(id(a))
print(id(b))

Output: 

>>> False

>>> 140439785435648

>>> 140439784059776

here you can see the output of this program is ‘False’ because both list or variable stored in different memory locations. And let see another one code example:

Code:

a = [1,2,3]
b = a
if a is b:
    print("True")
else:
    print("False")

Output: 

>>> True

Now, here you will see the output comes ‘True’ because a stored in one or same memory location and b is also referring to 'a' so if we will see the memory location id of a then,

Code:

a = [1,2,3]
b = a

print(id(a))
print(id(b))

Output:

>>> 140515703648704

>>> 140515703648704

What is ‘==’ operators in Python?

Now, here we will know all about the == or equal operator. This operator used to compare two values or items in the python programming language. So here we will compare two lists and will check that both are equal or not. Let’s see...

Code:

a = [1,2,3]
b = [1,2,3]
if a == b:
   print("True")
else:
   print("False")

print(id(a))
print(id(b))

Output:

>>> True

>>> 139760564695552

>>> 139760563319488

Now, here you can see in this program we got ‘True’ but equal operator does not matter in memory location. So the memory location of these lists; a, b are different. I hope you will be got the difference between in ‘is’ and ”==’’ operator.

150 points
4