Difference between == and operator in Python

Python Methods and Functions

# python3 code for
# illustrate
# the difference between
# == and is an operator
# [] empty list

list1 = []

list2 = []

list3 = list1

 

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:

 True False True 
  • Output of the first condition if "True" since list1 and list2 are empty lists.
  • Second, if the condition shows False, because the two empty lists are in different memory locations. Therefore, list1 and list2 refer to different objects. We can check this with the id () function in python, which returns the "identity" of the object.
  • Outputting the third if True because list1 and list3 point to the same object.

list1 = []

list2 = []

 

print ( id (list1) )

print ( id (list2))

Output:

 139877155242696 139877155253640 

This indicates that list1 and list2 are different objects.