ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(sum(1 for i in range(1, 1001) if '7' in str(i)))
  1. A. 270
  2. B. 272
  3. C. 271
  4. D. 300

Correct answer: C. 271

Among 0 to 999 there are 9**3 = 729 numbers with no 7 digit, so 1000 - 729 = 271 numbers contain a 7. The number 1000 has no 7, so the count stays at 271.

More Python Programming questions

Exams that ask this topic