Explain difference between == 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

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements