problems with a for loop (noobie)

Charles Mantha charles_mantha at hotmail.com
Fri Jul 5 22:39:53 EDT 2002


Reading the Python Tutorial by Guido van Russom and there's an example that
I don't get, and seems to not be explained (at least not enought for me).

>>> 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
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

The line < ...         if n % x == 0: >, what does the % do?
Also I just seem to not get the logic of this program. Would be nice if
someone would explain it to me.

Thanks
Charles Mantha

ps : and for some reason, the output is not the same as in the tutorial when
I try this.







More information about the Python-list mailing list