Im new in python programming, I have a small doubt. I need your help to clear it.

for below code why its using x == 0; we know == means is eqal to.

for n in range(2, 10):
… for x in range(2, n):
… if n % x == 0:
… print(n, ‘equals’, x, ‘*’, n//x)
… break
… else:
… # loop fell through without finding a factor
… print(n, ‘is a prime number’)

for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')

Use the logical operator == with the result of n%x and not only in the x value. If you have n = 4 and x = 2 the result of n%x will be 0 and 0 == 0 return a true value, if instead you have n = 5 and x = 3 the result of n%x will be 2 and 2 == 0 is false.

Hope this solve your doubts

-Gabriele-

Mark helpfull posts withAccept as Solutionto help other users locate important info. Don’t forget to giveKudosfor great contents!

The operator “==” represents a comparison operator! For your code, that means the expression “n % x == 0” will return True if and only if “n” is divisible by x! So, for whatever values you put into the parameters (n, or x) of the “range()” functions, the “n % x == 0” expression will take those inputs and evaluate them accordingly.