CBSE Class 12 Computer Science Question 61 of 68

Data Structures - I : Linear Lists — Question 17

Back to all questions
17
Question

Question 15(ii)

Find the error. Consider the following code and predict the error(s):

(y for y in range(100) if y % 2 == 0 and if y % 5 == 0)
Answer
  1. In the code, round brackets are used for list comprehensions, but list comprehensions work with square brackets only.
  2. The syntax error arises from the use of two if statements within the list comprehension. To resolve this error, we should use a single if statement with both conditions combined using the and operator.