1.

Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.(a) [x in range(1, 1000) if x%3==0](b) [x for x in range(1000) if x%3==0](c) [x%3 for x in range(1, 1000)](d) [x%3=0 for x in range(1, 1000)]The question was posed to me during an interview.My question is based upon List Comprehension in chapter Lists & List Comprehension of Python

Answer»

Correct ANSWER is (b) [X for x in range(1000) if x%3==0]

The best I can explain: The LIST COMPREHENSION [x for x in range(1000) if x%3==0] produces a list of numbers between 1 and 1000 that are divisible by 3.



Discussion

No Comment Found

Related InterviewSolutions