Question discussion

What is the output of the following Python code?for i in range(2, 10): for j in range(2, i): if i % j == 0: break else: print(i, end=" ")

2567Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

for i in range(2, 10):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        print(i, end=" ")
  • A2 3 5 7
  • B3 5 7
  • C2 3 5 7 9
  • D4 6 8 9
Correct answer: A

Explanation

The else clause of a for loop runs only when the loop ends without a break; for a prime i no j divides it, so the numbers 2, 3, 5 and 7 are printed (for i = 2 the inner loop does not run at all).

Discussion 0

Nobody has replied yet. Start the discussion!