0
0
Pythonprogramming~20 mins

Comparison operators in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained comparison
What is the output of this Python code?
Python
x = 5
result = 3 < x <= 5
print(result)
ATrue
BTypeError
CSyntaxError
DFalse
Attempts:
2 left
💡 Hint
Remember that chained comparisons check all parts together.
Predict Output
intermediate
2: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)
AFalse False False False
BTrue False True False
CFalse True False True
DTrue True True False
Attempts:
2 left
💡 Hint
Remember that '==' checks value equality, 'is' checks if two variables point to the same object.
Predict Output
advanced
2:00remaining
Output of mixed type comparison
What happens when you run this code?
Python
print(5 < '10')
AFalse
BTypeError
CSyntaxError
DTrue
Attempts:
2 left
💡 Hint
Think about comparing different data types in Python 3.
Predict Output
advanced
2: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)
ASyntaxError
BFalse
CTrue
DTypeError
Attempts:
2 left
💡 Hint
Remember the order of operations for 'and' and 'or'.
Predict Output
expert
2: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)
ATrue
BFalse
CTypeError
DAttributeError
Attempts:
2 left
💡 Hint
Look carefully at the __lt__ method implementation.