Python Programming - Variables - Discussion

Discussion Forum : Variables - General Questions (Q.No. 17)
17.
What is the value of y after executing the following code:
x = [1, 2, 3]
y = x
x = [4, 5, 6]
print(y)
[4, 5, 6]
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
An error is raised
Answer: Option
Explanation:

In this code, the variable x is initially assigned a list containing the values 1, 2, and 3.

The variable y is then assigned the same list that x holds a reference to.

However, when x is reassigned a new list containing the values 4, 5, and 6, y still holds a reference to the original list [1, 2, 3].

So when y is printed to the console, it prints [1, 2, 3].

Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.