Object Equality and Identity

The equality operator x == y tests the values of x and y for equality. In the case of lists and tuples, all the elements are compared and evaluated as true if they’re of equal value. For dictionaries, a true value is returned only if x and y have the same set of keys and all the objects with the same key have equal values.

The identity operators x is y and x is not y test two objects to see whether they refer to the same object in memory. In general, it may be the case that x == y , but x is not y .

Comparison between objects of non-compatible types such as a file and a floating-point number may be allowed, but the outcome is arbitrary and may not make any sense. In addition, comparison between incompatible ...

Get Python Essential Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.