Difference between == and is operator in python.

PythonServer Side ProgrammingProgramming

is and equals(==) operators are mostly same but they are not same. is operator defines if both the variables point to the same object whereas the == sign checks if the values for the two variables are the same.

Example Code

# Python program to  
# illustrate the  
# difference between 
# == and is operator 
# [] is an 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
raja
Published on 25-Sep-2018 15:42:12
Advertisements