PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 33
Q611 - Operators
What is the expected output of the following code?
- list1 = ['Peter', 'Paul', 'Mary', 'Jane']
- list2 = ['Peter', 'Paul', 'Mary', 'Jane']
- print(list1 is list2)
- print(list1 == list2)
- list1 = list2
- print(list1 is list2)
- print(list1 == list2)
-
A
- False
- True
- True
- True
-
B
- False
- True
- True
- False
-
C
- False
- False
- True
- True
-
D
- False
- True
- False
- True
Reveal correct answer
Correct answer: A
Explanation
Topics: list equal to operator identity operator
Try it yourself:
- list1 = ['Peter', 'Paul', 'Mary', 'Jane']
- list2 = ['Peter', 'Paul', 'Mary', 'Jane']
- print(list1 is list2) # False
- print(list1 == list2) # True
- print(id(list1)) # e.g. 140539383900864
- print(id(list2)) # e.g. 140539383947452 (a different number)
- list1 = list2
- print(list1 is list2) # True
- print(list1 == list2) # True
- print(id(list1)) # e.g. 140539652049216
- print(id(list2)) # e.g. 140539652049216 (the same number)
Explanation:
A list is a mutable data type.
The second list will be a different object, although the values are the same.
When list2 gets assigned to list1 Python creates a reference.
From now on the two lists have the same identity and the same values.
(There are similar questions Q433 and Q514.)
Q611 (Please refer to this number, if you want to write me about this question.)
A. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. The '==' operator, on the other hand, checks if the values of the two lists are the same, so it will return True for this case. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, and 'list1 == list2' will also be True.
B. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. The '==' operator checks if the values of the two lists are the same, so it will return True for this case. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, but 'list1 == list2' will be False because 'list1' and 'list2' are now the same list object.
C. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. Both 'list1' and 'list2' have the same values, so 'list1 == list2' will be True. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, and 'list1 == list2' will also be True.
D. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. The '==' operator checks if the values of the two lists are the same, so it will return True for this case. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, but 'list1 == list2' will be False because 'list1' and 'list2' are now the same list object.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
