Well done!
You have completed Understanding and Utilizing List Comprehensions in Python Quiz!
Quiz Question 1 of 5
Suppose you need to generate a list of tuples containing the number and its square from 1 to 10, but only for odd numbers. Which of the following list comprehensions would achieve this goal and why?
Choose the correct answer below:
-
A
[(i, i*i) for i in range(1, 11) if i % 2 == 0]
because it correctly identifies odd numbers and pairs them. -
B
[(i, i**2) for i in range(1, 11) if i % 2 == 0]
because it pairs even numbers with their squares. -
C
[(i**2, i) for i in range(1, 11) if i % 2 == 0]
because it pairs each square with its original number. -
D
[(i, i**2) for i in range(1, 11) if i % 2 != 0]
because it filters out even numbers before calculating the square.