2 views
in Python

I am a newbie in Python, I am working on some code and there I didn't get what is the function of % in 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')

...

From the above code, I  understood that the double equals (==) is for the comparison purpose, but I don't understand the if n % x part.

What does if n % x actually signifies?

by (108k points)

Kindly be informed that the % symbol is a Modulus operator in Python that calculates the remainder of the division like:

• 3 % 1 will be zero (as 3 divides evenly by 1)
• 3 % 2 will be 1 (as dividing 3 by 2 results in a remainder of 1).

If you are a beginner and want to know more about Python basics, then do check out the Python Certification course that will help you out in a better way.