Practice question from Python Fundamentals

What does this list comprehension create? A. [0, 1, 2, 3, 4] B. [1, 4, 9, 16, 25] C. [0, 1, 4, 9, 16] D. [2, 4, 6, 8, 10] E. Error

squares = [x**2 for x in range(5)]
print(squares)

Answer

C

Explanation

The list comprehension iterates through range(5), which produces 0, 1, 2, 3, 4. Each value is squared: 0²=0, 1²=1, 2²=4, 3²=9, 4²=16, resulting in [0, 1, 4, 9, 16].

More questions from Python Programming