Reputation: 19
for num in range(2,101):
for i in range(2,num):
if (num % i) == 0:
break
else :
print(num, end = ' ')
I don't understand why the output starts from 2
I thought num
starts with 2, and the i
also starts with 2
so, (num % i) == 0
is right
What's wrong with me?
Upvotes: 0
Views: 39
Reputation: 781616
When num == 2
, range(2, num)
is empty, because ranges stop before the end number.
So for i in range(2, num):
never executes the body in that case, and therefore it never tests num % i == 0
. The loop ends immediately, without executing break
, so it goes into the else:
block.
Upvotes: 3