In the tutorial there is an example for finding prime numbers:
>>> 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')
...
I understand that the double ==
is a test for equality, but I don't understand the if n % x
part. Like I can verbally walk through each part and say what the statement does for the example. But I don't understand how the percentage sign falls in.
What does if n % x
actually say?
Modulus operator; gives the remainder of the left value divided by the right value. Like:
3 % 1
would equal zero (since 3 divides evenly by 1)
3 % 2
would equal 1 (since dividing 3 by 2 results in a remainder of 1).