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:
l1 = [1, 2, 3]creates a listl1with elements 1, 2, and 3.l2 = l1assigns the reference ofl1tol2. Now,l1andl2are pointing to the same list[1, 2, 3].l1 = l1 + [4]creates a new list by concatenatingl1and[4], and assigns the reference of this new list tol1. Now,l1is pointing to the list[1, 2, 3, 4], butl2is still pointing to the original list[1, 2, 3].print(l2)prints the list thatl2is pointing to, which is[1, 2, 3].
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.