Challenge - 5 Problems
Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of chained comparison
What is the output of this Python code?
Python
x = 5 result = 3 < x <= 5 print(result)
Attempts:
2 left
💡 Hint
Remember that chained comparisons check all parts together.
✗ Incorrect
The expression 3 < x <= 5 checks if x is greater than 3 and less than or equal to 5. Since x is 5, both conditions are true, so the result is True.
❓ Predict Output
intermediate2:00remaining
Output of equality and identity
What is the output of this code snippet?
Python
a = [1, 2, 3] b = a c = a[:] print(a == b, a is b, a == c, a is c)
Attempts:
2 left
💡 Hint
Remember that '==' checks value equality, 'is' checks if two variables point to the same object.
✗ Incorrect
a and b point to the same list, so a is b is True. a and c have the same contents but are different objects, so a == c is True but a is c is False.
❓ Predict Output
advanced2:00remaining
Output of mixed type comparison
What happens when you run this code?
Python
print(5 < '10')
Attempts:
2 left
💡 Hint
Think about comparing different data types in Python 3.
✗ Incorrect
In Python 3, comparing int and str with < raises a TypeError because these types cannot be ordered.
❓ Predict Output
advanced2:00remaining
Output of complex boolean comparison
What is the output of this code?
Python
x = 10 y = 20 print(x < y and y < 30 or x > 15)
Attempts:
2 left
💡 Hint
Remember the order of operations for 'and' and 'or'.
✗ Incorrect
x < y is True, y < 30 is True, so 'x < y and y < 30' is True. The 'or x > 15' part is False, but True or False is True.
❓ Predict Output
expert2:00remaining
Output of comparison with custom class
What is the output of this code?
Python
class Number: def __init__(self, value): self.value = value def __lt__(self, other): return self.value > other.value n1 = Number(5) n2 = Number(3) print(n1 < n2)
Attempts:
2 left
💡 Hint
Look carefully at the __lt__ method implementation.
✗ Incorrect
The __lt__ method is reversed: it returns True if self.value is greater than other.value. Since 5 > 3, n1 < n2 returns True.