Challenge - 5 Problems
Master of Comparing Values
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this pytest assertion?
Consider the following pytest test function. What will be the result of running this test?
PyTest
def test_compare_values(): a = [1, 2, 3] b = [1, 2, 3] assert a == b
Attempts:
2 left
💡 Hint
In Python, equality (==) compares values, not object identity.
✗ Incorrect
The assertion compares the values inside the lists. Since both lists contain the same elements in the same order, the test passes.
❓ assertion
intermediate1:30remaining
Which assertion correctly checks inequality of two variables?
You want to write a pytest assertion to confirm that variable x is NOT equal to variable y. Which option is correct?
Attempts:
2 left
💡 Hint
Use the standard Python operator for inequality.
✗ Incorrect
The correct operator for inequality in Python is '!='. Option B has invalid syntax, C is outdated and invalid in Python 3, and D is logically correct but less direct.
🔧 Debug
advanced2:30remaining
Why does this pytest assertion fail unexpectedly?
Examine the test below. Why does the assertion fail even though the printed values look equal?
PyTest
def test_string_compare(): s1 = 'Test\n' s2 = 'Test\r\n' print(repr(s1)) print(repr(s2)) assert s1 == s2
Attempts:
2 left
💡 Hint
Look carefully at the escape characters in the strings.
✗ Incorrect
The string s1 ends with a newline '\n', while s2 ends with carriage return and newline '\r\n'. These are different characters, so the strings are not equal and the assertion fails.
🧠 Conceptual
advanced2:00remaining
What is the difference between '==' and 'is' in Python testing?
In pytest assertions, what is the key difference between using '==' and 'is' when comparing two variables?
Attempts:
2 left
💡 Hint
Think about comparing two different lists with the same content.
✗ Incorrect
'==' compares if the values are equal, while 'is' checks if both variables refer to the exact same object in memory.
❓ framework
expert3:00remaining
Which pytest assertion will fail when comparing floating point numbers due to precision issues?
Given the following test, which assertion will fail due to floating point precision problems?
PyTest
def test_float_comparison(): a = 0.1 + 0.2 b = 0.3 # Which assertion fails?
Attempts:
2 left
💡 Hint
Floating point arithmetic can cause small precision errors.
✗ Incorrect
Due to floating point precision, 0.1 + 0.2 is not exactly equal to 0.3, so 'assert a == b' fails. The other assertions handle precision properly.