PCEP-30-02 · Question #81
What is the expected output of the following code? list1 = [3, 7, 23, 42] list2 = [3, 7, 23, 42] print(list1 is list2) print(list1 == list2)
The correct answer is C. 1|False 2|True. Option C is correct because Python creates two separate list objects in memory when you write list1 = [3, 7, 23, 42] and list2 = [3, 7, 23, 42], so is (which checks object identity - whether both names point to the exact same memory address) returns False; meanwhile == (which…
Question
Options
- A1|False 2|False
- B1|True 2|True
- C1|False 2|True
- D1|True 2|False
How the community answered
(24 responses)- A4% (1)
- B13% (3)
- C75% (18)
- D8% (2)
Explanation
Option C is correct because Python creates two separate list objects in memory when you write list1 = [3, 7, 23, 42] and list2 = [3, 7, 23, 42], so is (which checks object identity - whether both names point to the exact same memory address) returns False; meanwhile == (which checks value equality - whether the contents match) returns True since both lists contain identical elements.
- A (False/False) is wrong because
==compares values, not identity, so two lists with the same elements always returnTruefrom==. - B (True/True) is wrong because
iswould only beTrueiflist2 = list1(an alias), not when each is independently created. - D (True/False) has both answers backwards - it confuses what
isand==each test.
Memory tip: Think of is as asking "are these the same person?" and == as asking "do these people look identical?" - twins look the same (== is True) but are not the same person (is is False).
Community Discussion
No community discussion yet for this question.