Sep-26-2022, 06:38 PM
New to Python and working thru the tutorials.
In the example of "for" loops that determines prime numbers. It uses the "%" to determine if there is a remainder of a division. So why does the number 2 become a prime number in the example below?
I know that 2 is a prime number, but I'm probably not seeing how the program works for the number 2, since when I calculate in Python 2 % 2 the result is 0. t\Thus the condition "if n % x == 0" should be True. Correct? Thereby not treating 2 as a prime number in the example.
Thanks for your help
Tutorial 4.4 explaining "break" and "for" loops
Example:
>>> 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')
...
2 is a prime number
3 is a prime number
ets.......
In the example of "for" loops that determines prime numbers. It uses the "%" to determine if there is a remainder of a division. So why does the number 2 become a prime number in the example below?
I know that 2 is a prime number, but I'm probably not seeing how the program works for the number 2, since when I calculate in Python 2 % 2 the result is 0. t\Thus the condition "if n % x == 0" should be True. Correct? Thereby not treating 2 as a prime number in the example.
Thanks for your help
Tutorial 4.4 explaining "break" and "for" loops
Example:
>>> 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')
...
2 is a prime number
3 is a prime number
ets.......