16. What does this dictionary equality print?
d1 = {"a": 1, "b": 2}
d2 = {"b": 2, "a": 1}
print(d1 == d2)
print(d1 is d2)
Output:
True
False
Dicts compare by key-value pairs, and order does not matter — so d1 == d2 is True even though the insertion order differs. They are separate objects, so is is False. (Since Python 3.7 dicts preserve insertion order, but equality never depended on it.)
17. What does this nested dict equality print?
a = {"x": [1, 2], "y": {"z": 3}}
b = {"x": [1, 2], "y": {"z": 3}}
print(a == b)
Output:
True
== on dicts recurses into the values: the lists [1, 2] and nested dict {"z": 3} are compared by value too. Any nesting depth of equal-comparable containers compares correctly.
18. What does this NaN comparison print?
import math
nan = float("nan")
print(nan == nan)
print(math.isnan(nan))
Output:
False
True
NaN is not equal to itself — IEEE 754 defines NaN != NaN. Python follows this, so nan == nan is False. To test for NaN you must use math.isnan(nan), which is True. This trips up anyone who assumes a value always equals itself.
19. What does this signed-zero comparison print?
print(0.0 == -0.0)
print(1e309)
print(-1e309)
Output:
True
inf
-inf
0.0 == -0.0 is True (they compare equal by value), but they are distinct in IEEE 754 arithmetic. And a float literal that overflows does not raise an error — it becomes inf (or -inf for a negative overflow). So 1e309 prints inf and -1e309 prints -inf. (Careful: in Python, 1 / 0.0 raises ZeroDivisionError rather than producing inf — division by zero is an error even though the representation allows infinity.)
20. What does this string-intern comparison print?
s1 = "interview"
s2 = "inter" + "view"
s3 = "".join(["inter", "view"])
print(s1 == s2)
print(s1 is s2)
print(s1 is s3)
Output:
True
True
False
s2 is a compile-time concatenation of literals, so the compiler folds it to the same interned object as s1 — both == and is are True. s3 is built at runtime by join, producing a new object — == is still True, but is is False. The lesson: use == for strings, never is.
21. What does this comparison-in-condition print?
x = 5
if 3 < x < 7:
print("range")
if x == 5 or x == 6:
print("either")
if x == 5 and x == 6:
print("both")
Output:
range
either
3 < x < 7 is True for x = 5. x == 5 or x == 6 short-circuits on the first True. x == 5 and x == 6 is False (x cannot be both), so "both" never prints.
22. What does this mixed list comparison print?
a = [1, 2]
b = [1, 2]
print(a == b)
print(a + [] == b)
print(a + [] is b)
Output:
True
True
False
a + [] builds a new list with the same contents, so it is equal to b by value but a different object. a == b is True (same values), and a + [] is b is False (different objects). Another reminder that == is value, is is identity.
23. What does this set comparison print?
s1 = {1, 2, 3}
s2 = {3, 2, 1}
s3 = {1, 2, 4}
print(s1 == s2)
print(s1 == s3)
print(s1 < {1, 2, 3, 4})
Output:
True
False
True
Sets are unordered and compare by membership, so s1 == s2 is True. s1 < {1, 2, 3, 4} is a proper subset check: s1 is a strict subset of the larger set, so True. s1 == s3 is False because 3 != 4.
24. What does this list of dicts comparison print?
users = [{"id": 1, "name": "A"}, {"id": 2, "name": "B"}]
copy = [{"name": "A", "id": 1}, {"name": "B", "id": 2}]
print(users == copy)
Output:
True
Lists compare element-by-element; each dict compares by key-value pairs regardless of key order. So users == copy is True even though copy writes its dict keys in a different order.
25. What does this any/all comparison print?
nums = [0, 1, 2]
print(any(nums))
print(all(nums))
print(any([False, True, False]))
Output:
True
False
True
any returns True if any element is truthy — 0 is falsy but 1 and 2 are truthy, so True. all returns True only if every element is truthy, and 0 is falsy, so False. any([False, True, False]) is True.
26. What does this string-with-whitespace comparison print?
print("hello" == " hello")
print("hello" == "hello ")
print("hello".strip() == "hello")
print("hello" < " hello")
Output:
False
False
True
False
== compares the exact character sequences — spaces count. "hello" == " hello" is False. .strip() removes surrounding whitespace, so "hello".strip() == "hello" is True. "hello" < " hello" is False — the space is code point 32, which is less than 'h' (104), so " hello" is actually the smaller string; "hello" comes after it.
27. What does this generator-vs-list comparison print?
g = (x * x for x in range(3))
lst = [0, 1, 4]
print(list(g) == lst)
print(g == lst)
Output:
True
False
list(g) materializes the generator into [0, 1, 4], which equals lst by value. But the generator object itself never equals a list — g == lst is False (different types, and generators are not comparable to other objects by content).
28. What does this exception-on-compare print?
try:
print(1 < "1")
except TypeError as e:
print("TypeError")
Output:
TypeError
In Python 3, comparing an int to a str with < raises TypeError — the ordering between unrelated types is undefined, so Python refuses. (Python 2 allowed arbitrary ordering — another 2-to-3 migration gotcha.)
29. What does this dict.keys() membership print?
d = {"a": 1, "b": 2}
print("a" in d)
print("a" in d.keys())
print(1 in d.values())
print(("a", 1) in d.items())
Output:
True
True
True
True
in on a dict checks keys; d.keys() also reports keys; d.values() reports values; and d.items() returns (key, value) pairs. So ("a", 1) in d.items() is True. This tests whether you know the four in behaviors on dict views.
30. What does this deep vs shallow comparison print?
import copy
a = [[1, 2], [3, 4]]
b = copy.copy(a)
c = copy.deepcopy(a)
print(a == b)
print(a == c)
print(a[0] is b[0])
print(a[0] is c[0])
Output:
True
True
True
False
copy.copy (shallow) and copy.deepcopy both produce lists equal to a by value. But shallow copy shares the inner list objects — a[0] is b[0] is True — while deep copy builds entirely new inner lists — a[0] is c[0] is False. Mutating a shared inner list via the shallow copy would affect the original.
Premium Content
Unlock Comparison Questions - Part 2 and all premium lessons with a subscription.
From ₹199.99/year — See plans