KnowledgeBoat Logo
|

Informatics Practices

Consider the following code:

>>>A = [10, 20, 30, 40]
>>>B = A.copy()
>>>A[2] = 50
>>>B

Which of the following will be the elements of list A and B?

  1. A = [10, 20, 50, 40]
    B = [10, 20, 30, 40]
  2. A = [10, 20, 30, 40]
    B = [10, 20, 50, 40]
  3. A = [10, 20, 30, 40, 50]
    B = [10, 20, 50, 40]
  4. A = [10, 20, 40]
    B = [10, 20, 50, 40]

Python List Manipulation

3 Likes

Answer

A=[10, 20, 50, 40]
B=[10, 20, 30, 40]

Reason — The code initializes list A with elements [10, 20, 30, 40]. It then creates a copy of list A named list B using the copy() method. Subsequently, the third element of list A is modified to 50 using A[2] = 50. At this point, B is still [10, 20, 30, 40], because it's a separate list from A. The copy() method does not modify the original list or the modifications made in the new list will not be reflected to the base list.

Answered By

3 Likes


Related Questions