Question discussion

What is the output of the following Python code?s = {x % 3 for x in range(10)} print(s, sum(i for i in range(5) if i % 2))

1905Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

s = {x % 3 for x in range(10)}
print(s, sum(i for i in range(5) if i % 2))
  • A{1, 2} 4
  • B{0, 1, 2} 10
  • C{0, 1, 2, 3} 4
  • D{0, 1, 2} 4
Correct answer: D

Explanation

The set comprehension keeps only the distinct remainders 0, 1, 2; the generator adds the odd numbers 1 + 3 = 4.

Discussion 0

Nobody has replied yet. Start the discussion!