1.

How can you differentiate between “is” and “==” operators in Python?

Answer»

“is” operator is used for the purpose of REFERENCE equality to check whether the two references or variables are pointing to the same OBJECT or not. ACCORDINGLY, it returns value as true or false.

“==” operator is used for the purpose of value equality to check whether the two variables are having same value or not. Accordingly, it returns value as true or false.

We can take any example with the HELP of two lists X and Y.

X = [1,2,3,4,5]

Y = [1,2,3,4,5]

Z = Y

  1. X == Y will give the RESULT as true because the List X contains same values as List Y. So, the above statement turns out to be true.
  2. X is Y will give the result as false because even though the values contained in the two lists are same only but the referred objects are different.
  3. Z is Y give the result as true because the referred object is same only for both the lists.


Discussion

No Comment Found