A

Admin • 832.27K Points
Coach

Q. What will this code print?

Code:
a = [1, 2, 3]
b = a[:]
a.append(4)
print(b)
  • (A) [1, 2, 3, 4]
  • (B) [1, 2, 3]
  • (C) [4]
  • (D) Error
  • Correct Answer - Option(B)
  • Views: 18
  • Filed under category Python
  • Hashtags:

Explanation by: Admin

Using slicing (`[:]`) creates a shallow copy of the list.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.