What do these 3 lines print?>>> l1 = [1, 2, 3]>>> l2 = [1, 2, 3] >>> print(l1 is l2)
Question
What do these 3 lines print?>>> l1 = [1, 2, 3]>>> l2 = [1, 2, 3] >>> print(l1 is l2)
Solution
The output will be False.
Here's the step-by-step explanation:
l1 = [1, 2, 3]creates a list with the elements 1, 2, and 3 and assigns it to the variablel1.l2 = [1, 2, 3]creates another list with the elements 1, 2, and 3 and assigns it to the variablel2.print(l1 is l2)checks ifl1andl2are the same object. In Python,ischecks for identity, not equality. That means it's checking ifl1andl2are the exact same object, not just if they have the same content.
Even though l1 and l2 have the same content, they are not the same object. They are two separate lists that happen to have the same elements. Therefore, l1 is l2 is False, and that's what gets printed.
Similar Questions
What does this script print?l1 = [1, 2, 3]l2 = l1l1 = l1 + [4]print(l2)
What do these lines print?>>> a = [1, 2, 3, 4]>>> a[1:3][1, 2, 3][1, 2][2, 3]
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 lines print?>>> a = [1, 2, 3, 4]>>> b = a>>> a[2] = 10>>> b
What do these lines print?>>> a = [1, 2, 3, 4]>>> a[-3]-3[4, 3]2
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.