469,951 Members | 2,710 Online

# basic python question about for loop

From the Python.org tutorial:
>>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

first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

http://www.python.org/doc/current/tu...00000000000000

Thanks.
Apr 9 '08 #1
7 1742
jmDesktop schrieb:
From the Python.org tutorial:
>>>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

first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.
Diez
Apr 9 '08 #2
On Apr 9, 4:58*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
jmDesktop schrieb:

From the Python.org tutorial:
>>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
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.

Diez- Hide quoted text -

- Show quoted text -
I do not understand.
Apr 9 '08 #3
jmDesktop wrote:
>>From the Python.org tutorial:
>>>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

first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
>>range(2, 2)
[]
>>>
The loop body executes zero times.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Apr 9 '08 #4
jmDesktop schrieb:
On Apr 9, 4:58 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>jmDesktop schrieb:

>>From the Python.org tutorial:
>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
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.

Diez- Hide quoted text -

- Show quoted text -

I do not understand.
for <variablein <sequenceloops over a sequence. And of course it
doens't if the sequence is empty, because you can't loop over something
that is empty, can't you?

and range(2,2) is the empty sequence.

Diez
Apr 9 '08 #5
|So what is n and x in the first iteration? Sorry. I'm trying.

When n == 2, the inner loop executes 0 times (the length of range(2,n)) and
then falls thru to the else clause, printing the correct answer.

Apr 9 '08 #6

"jmDesktop" <ne***********@gmail.comwrote in message
So what is n and x in the first iteration? Sorry. I'm trying.
Remember how Python's range operator works. range(n, x) constructs a list
that consists of all elements starting with n and up to, but /not
including/, x. For example, range(3, 7) constructs the list [3, 4, 5, 6].

So what happens if you try to construct the list range(2, 2)? In this case,
Python will construct an empty list -- this is its interpretation of "up to,
but not including n" for an argument like range(n, n). This is precisely
what is happening in the first iteration of the first enclosing for loop.
Trace through the code; see that 2 is bound to n in the first iteration; see
that the inner loop then tries to construct the list range(2, n), which is
range(2, 2), which is an empty list. And a "for x in []" statement will not
execute even once.

As it happens, your loop is perfectly correct. You are testing for divisors
for a number excluding 1 and the number itself. For the number 2, the
number of possible divisors satisfying this condition is an empty set. So 2
is, quite correctly, adjudged to be prime.
Jun 27 '08 #7
jmDesktop wrote:
[...]
So what is n and x in the first iteration? Sorry. I'm trying.
Somewhat feebly, if you don't mind my saying so, but don't worry.

The usual way to proceed in the face of such ignorance is to insert some
form of output that will tell you the answer to your question.

So:
>>for n in range(2, 20):
.... print range(2, n)
.... for x in range(2, n):
.... if n % x == 0:
.... print n, 'equals', x, '*', n/x
.... break
.... else:
.... print n, "is prime"
....
[]
2 is prime
[2]
3 is prime
[2, 3]
4 equals 2 * 2
[2, 3, 4]
5 is prime
[2, 3, 4, 5]
6 equals 2 * 3
[2, 3, 4, 5, 6]
7 is prime
[2, 3, 4, 5, 6, 7]
8 equals 2 * 4
[2, 3, 4, 5, 6, 7, 8]
9 equals 3 * 3

and so on! This is the value of the interactive interpreter.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Jun 27 '08 #8

### This discussion thread is closed

Replies have been disabled for this discussion.