Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Difference Between 'and' and 'is' Operator in Python



== operator

== operator compares the operands by checking the equality of values of objects.

is operator

is operator compares the operands by checking the objects to be the same or not.

Example

Following is the program in Python to showcase the difference.

 Live Demo

list1 = [1]
list2 = [1]
list3 = list1

print(id(list1))
print(id(list2))

if (list1 == list2):
   print("True")
else:
   print("False")

if (list1 is list2):
   print("True")
else:
   print("False")

if (list1 is list3):
   print("True")
else:
   print("False")

Output

140380664377096
140380664376904
True
False
True
Updated on: 2020-04-15T08:24:35+05:30

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements