Knowee
Questions
Features
Study Tools

What does this script print?l1 = [1, 2, 3]l2 = l1l1.append(4)print(l2)

Question

What does this script print?l1 = [1, 2, 3]l2 = l1l1.append(4)print(l2)

🧐 Not the exact question you are looking for?Go ask a question

Solution

The script prints: [1, 2, 3, 4]

Here's the step by step explanation:

  1. l1 = [1, 2, 3] : This line creates a list l1 with elements 1, 2, and 3.

  2. l2 = l1 : This line creates a new reference l2 that points to the same list as l1. They are not two different lists, but two names pointing to the same list.

  3. l1.append(4) : This line appends the integer 4 to the end of the list pointed to by l1. Since l2 points to the same list, the change is also reflected when we access the list using l2.

  4. print(l2) : This line prints the list pointed to by l2, which is now [1, 2, 3, 4] due to the append operation on l1.

This problem has been solved

Similar Questions

What is the output of this code ( In Python )? l1 = [1, [2, 3], 4] l2 = l1.copy() l2[1][1]=7 print(l1, l2)

What do these 3 lines print?>>> l1 = [1, 2, 3]>>> l2 = [1, 2, 3] >>> print(l1 is l2)

what is the output of the following code?b = [1, 2, 3]for a in b:       b.append(a)print(b)

What will the output of the following code be?l = [32, 34, 12, 27, 33]l.append([14, 19])print(len(l))

What do these lines print?>>> a = [1, 2, 3, 4]>>> a.append(5)>>> len(a)

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.