Python Programming - Variables - Discussion

Discussion Forum : Variables - General Questions (Q.No. 11)
11.
What is the value of x after executing the following code:
x = [1, 2, 3]
y = x
x[0] = 4
print(y)
[4, 2, 3]
[1, 2, 3]
[4, 2, 2]
An error is raised
Answer: Option
Explanation:
In Python, when a list is assigned to a variable, the variable holds a reference to the list rather than a copy of the list. In this case, the variable y holds a reference to the same list as x. When x[0] is changed to 4, the list is modified and both x and y reflect this change.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.