[Tutor] Doubt in for loop

bessenkphilip bessenkphilip at gmail.com
Thu Apr 4 03:29:02 CEST 2013


Hi all,

I'm having a doubt in the below program's 2n'd "for" loop.

 >>> 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

My doubt is that "will 'x' be always of value 2, if so why that for loop 
"for x in range(2, n):"
i don't know how the first output , as If 2%2==0:(this satisfies the if 
loop as x =2) , so how the else part came to output i.e 2 is a prime number.

Please educate me in this.

Thanks in advance,
  -Bessen


More information about the Tutor mailing list