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)
Solution
The script prints: [1, 2, 3, 4]
Here's the step by step explanation:
-
l1 = [1, 2, 3]: This line creates a listl1with elements 1, 2, and 3. -
l2 = l1: This line creates a new referencel2that points to the same list asl1. They are not two different lists, but two names pointing to the same list. -
l1.append(4): This line appends the integer 4 to the end of the list pointed to byl1. Sincel2points to the same list, the change is also reflected when we access the list usingl2. -
print(l2): This line prints the list pointed to byl2, which is now [1, 2, 3, 4] due to the append operation onl1.
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)
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.