Test and improve your understanding of Python dictionaries with these multiple choice questions. Practice key concepts like key-value pairs, dictionary methods, and data manipulation with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.
Topic: list-comprehension
print([x * 2 for x in range(3)])
[0, 2, 4]
Each number in range(3) is multiplied by 2: [0*2, 1*2, 2*2].
print([char for char in "abc"])
['a', 'b', 'c']
It iterates through each character in the string 'abc'.
print([x.upper() for x in ["a", "b", "c"]])
['A', 'B', 'C']
The upper() method is applied to each string.
result = []
for x in range(3):
result.append(x + 1)
[x + 1 for x in range(3)]
The for loop appends x+1, which matches this comprehension.
numbers = [1, 2, 3, 4, 5]
print([x for x in numbers if x % 2 == 0])
[2, 4]
It filters only even numbers using the condition `x % 2 == 0`.
print([x for x in range(10) if x % 3 == 0])
[0, 3, 6, 9]
It includes numbers divisible by 3 from 0 to 9.
print([i for i in range(5) if i not in [1, 3]])
[0, 2, 4]
It excludes 1 and 3 from the list.
print([x**2 for x in range(4)])
4
range(4) produces 0, 1, 2, 3 → four elements.
print([i for i in range(5) if i % 2])
[1, 3]
i % 2 is true for odd numbers.
result = [x**2 for x in range(10) if x % 2 == 0]
print(result)
[0, 4, 16, 36, 64]
Even numbers from 0 to 9 are squared.
print([x for x in range(5) if x])
[1, 2, 3, 4]
In Python, 0 is considered False in a boolean context.
print([x*y for x in [1, 2] for y in [10, 100]])
[10, 100, 20, 200]
It evaluates 1*10, 1*100, 2*10, 2*100 in that order.
print([x+y for x in 'ab' for y in '12'])
['a1', 'a2', 'b1', 'b2']
It produces all combinations of characters from both strings.
print([[i, j] for i in range(2) for j in range(2)])
[[0, 0], [0, 1], [1, 0], [1, 1]]
It generates all pairs (i, j) from 0 and 1.
print([i.lower() for i in "HELLO" if i not in "EO"])
['h', 'l', 'l']
It skips 'E' and 'O', and converts remaining to lowercase.
Tutorials, Roadmaps, Bootcamps & Visualization Projects