What is the output of the following Python code and why? a = [1, 2, [3, 4]]; b = list(a); c = a; b[2][0] = 100; print(a == b, a is b, a is c, a)
π‘ Model Answer
The code first creates a list a = [1, 2, [3, 4]]. b = list(a) creates a shallow copy of a, so b is a new list object that contains the same inner list reference. c = a makes c reference the same list as a. The statement b[2][0] = 100 mutates the nested list [3, 4] by setting its first element to 100. Because the nested list is shared between a, b, and c, all three see the change. The comparisons yield: a == b is True because the lists have equal contents; a is b is False because b is a different outer list; a is c is True because c references the same outer list as a. The final value of a is [1, 2, [100, 4]].
This answer was generated by AI for study purposes. Use it as a starting point β personalize it with your own experience.
π€ Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI β Starts at βΉ500