Looking a help for Python script

Dear Friends,

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’)

1 Like

Hi @kousikbangalore,

Welcome to the Github Community Forum!

The code:

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 :slight_smile:

-Gabriele-

Mark helpfull posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great contents!

1 Like

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.