Question 20 (Beginner):
What is the output of this Python code?
A)
B)
C)
D) Raises an error
#Python #Lists #Variables #Beginner
✅ By: https://t.me/DataScienceQ
✅ **Correct answer: B) `[1, 2, 3, 4]`**
*Explanation:
- `y = x` creates a reference to the same list object
- Modifying `y` affects `x` because they point to the same memory location
- To create an independent copy, use or *
What is the output of this Python code?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
A)
[1, 2, 3]
B)
[1, 2, 3, 4]
C)
[4, 3, 2, 1]
D) Raises an error
#Python #Lists #Variables #Beginner
✅ By: https://t.me/DataScienceQ
*Explanation:
- `y = x` creates a reference to the same list object
- Modifying `y` affects `x` because they point to the same memory location
- To create an independent copy, use
y = x.copy()
y = list(x)