Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The script prints [1, 2, 3].

Here's the step-by-step explanation:

  1. l1 = [1, 2, 3] creates a list l1 with elements 1, 2, and 3.
  2. l2 = l1 assigns the reference of l1 to l2. Now, l1 and l2 are pointing to the same list [1, 2, 3].
  3. l1 = l1 + [4] creates a new list by concatenating l1 and [4], and assigns the reference of this new list to l1. Now, l1 is pointing to the list [1, 2, 3, 4], but l2 is still pointing to the original list [1, 2, 3].
  4. print(l2) prints the list that l2 is pointing to, which is [1, 2, 3].

This problem has been solved

Similar Questions

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

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 is the output of below code snippeta = [ [8, 2] , [4, 1, 2, 2] ]b = a[1][2:]print(b)

Which of the following statement will create list?L1=list( )L1=[1,2,3,4]

What do these lines print?>>> a = [1, 2, 3, 4]>>> b = a>>> b

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.