Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (50.2k points)

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?

1 Answer

0 votes
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. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 11 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Feb 4 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Dec 6, 2020 in Python by ashely (50.2k points)

Browse Categories

...