KnowledgeBoat Logo

Computer Science

Suggest corrections for the errors in both the previous questions.

Linear Lists

2 Likes

Answer

(i)

y for y in range(100) if y % 2 == 0 and if y % 5 == 0

The list comprehension should be enclosed in square brackets, and we should use a single if statement with both conditions combined using the and operator.

The corrected code is:

[y for y in range(100) if y % 2 == 0 and y % 5 == 0]

(ii)

(y for y in range(100) if y % 2 == 0 and if y % 5 == 0)

The list comprehension should be enclosed in square brackets, and we should use a single if statement with both conditions combined using the and operator.

The corrected code is:

[y for y in range(100) if y % 2 == 0 and y % 5 == 0]

(iii)

["good" if i < 3: else: "better" for i in range(6)]

The list comprehension does not include colon.

The corrected code is:

["good" if i < 3 else "better" for i in range(6)]

Answered By

3 Likes


Related Questions