ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
print(sum(x for x in range(1, 11) if x % 2 == 0) - sum(x for x in range(1, 11) if x % 2))
  1. A. -15
  2. B. 55
  3. C. 5
  4. D. 25

Correct answer: C. 5

The even numbers 2 to 10 sum to 30 and the odd numbers 1 to 9 sum to 25, so the difference is 5.

More Python Programming questions

Exams that ask this topic