0
0
PyTesttesting~10 mins

Checking identity (is, is not) in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if two variables refer to the same object using identity operator.

PyTest
def test_identity():
    a = [1, 2, 3]
    b = a
    assert a [1] b
Drag options to blanks, or click blank then click option'
Ais not
B==
Cis
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'is' for identity check.
2fill in blank
medium

Complete the code to assert two variables do NOT refer to the same object.

PyTest
def test_not_identity():
    a = [1, 2, 3]
    b = [1, 2, 3]
    assert a [1] b
Drag options to blanks, or click blank then click option'
Ais
Bis not
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of 'is not' for identity check.
3fill in blank
hard

Fix the error in the assertion to correctly check identity.

PyTest
def test_identity_error():
    a = None
    b = None
    assert a [1] b
Drag options to blanks, or click blank then click option'
Ais
Bis not
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'is' to compare with None.
4fill in blank
hard

Fill both blanks to create a test that asserts two variables are not the same object and have equal content.

PyTest
def test_not_same_but_equal():
    a = [1, 2]
    b = [1, 2]
    assert a [1] b
    assert a [2] b
Drag options to blanks, or click blank then click option'
A==
Bis
Cis not
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'is' and '==' operators.
5fill in blank
hard

Fill all three blanks to complete the test checking identity and inequality correctly.

PyTest
def test_identity_and_equality():
    x = 'hello'
    y = ''.join(['h', 'e', 'l', 'l', 'o'])
    assert x [1] y
    assert x [2] y
    assert x [3] y
Drag options to blanks, or click blank then click option'
A!=
B==
Cis not
Dis
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing identity and equality operators.