14.5k views
1 vote
What would be the value of the variable list after the execution of the following code?

list = [1, 2, 3, 4]
list[3] = 10
a.[1, 2, 3, 10]
b.[1, 2, 10, 4]
c.[1, 10, 10, 10]

1 Answer

7 votes

Answer: a) [1, 2, 3, 10]

Step-by-step explanation:

Lists have indexes. Basically, starting from the first element of a list, you count up from 0. So the first element in the list has an index of 0. The second element has an index of 1. And so on.

list = [1, 2, 3, 4] defines a list with four elements. list[3] = 10 assigns the third index to a value of 10. We need to know which element has an index of 10, so we count up. 1 has an index of 0. 2 has an index of 1. 3 has an index of 2. And 4 has an index of 3. So whatever value is in the third index is replaced by a new value of 10.

Therefore the new list will look like [1, 2, 3, 10].

User Craig W
by
7.4k points