Data Structures & AlgorithmsHardAmazon · Microsoft OA

Edge case: identity versus equality in Python.

Edge case: identity versus equality in Python.
a = [1, 2]; b = [1, 2]; c = a
print(a == b, a is b, a is c, [] is [])
What is printed?
  1. A. True True True True
  2. B. True False True True
  3. C. True False True False
  4. D. False False True False

Correct answer: C. True False True False

== compares values (equal lists), while is compares object identity: a and b are different objects, c is another name for a, and each [] literal creates a new list.

More Coding Interview Problems questions

Exams that ask this topic