Question 21
What is the output of the following list comparison in Python?
print([1, 2, 3] < [1, 2, 4])
Lists are compared lexicographically element by element. At index 2, 3 < 4 is True, so the overall comparison evaluates to True.
What is the output of the following list comparison in Python?
print([1, 2, 3] < [1, 2, 4])
Lists are compared lexicographically element by element. At index 2, 3 < 4 is True, so the overall comparison evaluates to True.
Calculate the value of count after loop execution:
count = 0
for x in [[]] * 5:
count += len(x)
print(count)
[[]] * 5 creates [[], [], [], [], []]. The length of each inner empty list len(x) is 0. Total sum = 0.
What will be the output of list(filter(lambda x: x % 2 != 0, range(6)))?
range(6) produces 0, 1, 2, 3, 4, 5. The filter keeps numbers where x % 2 != 0 (odd numbers), yielding [1, 3, 5].
What is printed by the following snippet?
vals = [10, 20, 30]
vals.insert(-1, 99)
print(vals[2])
Inserting at index -1 places 99 before the last element (30). vals becomes [10, 20, 99, 30]. vals[2] is 99.
Which of the following correctly creates a list of 10 zeros without sharing inner references?
Since 0 is an immutable integer, [0] * 10 and [0 for _ in range(10)] are both safe and create a flat list of 10 independent integer zeros.