Velocity Reviews > Nested for loop problem

Nested for loop problem

Don Low
Guest
Posts: n/a

 11-08-2003
Hi,

I'm studying the python tutorial at python.org. In introducing the
concept of *break* and *continue* Statements, and *else* Clauses on
Loops, there's an example that goes as follows:

#!/usr/bin/python

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 have a problem with this nested for loop, because the answer it yields
doesn't make sense to me. Here is the answer:

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

Where I have the problem is as follows:

n = 2 in the first iteration of range(2, 10); therefore x = [] in the
first iteration of range(2, n). More specifically:

>>> range(2, 10)

[2, 3, 4, 5, 6, 7, 8, 9]
>>> range(2, 2)

[]

>>> for x in range(2, 2):

.... print x
....

Since x prints nothing, I guess x = NULL. If I try:

if 2 % NULL or blank, I get an error message.

So the question is when I run the script, why doesn't it produce an
error message?

OK, I'm sure I'm misunderstanding something, but could someone explain?

--
Thanks,

Don

Irmen de Jong
Guest
Posts: n/a

 11-08-2003
Don Low wrote:
>>>>for x in range(2, 2):

>
> ... print x
> ...
>
> Since x prints nothing, I guess x = NULL. If I try:

That's your misunderstanding. That loop does *nothing*,
it doesn't enter the loop body. When you're looping over
an empty sequence, the loop body is never entered.

(also, NULL is meaningless in Python. I think you meant
None, right?)

--Irmen

Don Low
Guest
Posts: n/a

 11-08-2003
In article <3fad715f\$0\$58715\$(E-Mail Removed)4all.nl>, Irmen de Jong wrote:
> Don Low wrote:
>>>>>for x in range(2, 2):

>>
>> ... print x
>> ...
>>
>> Since x prints nothing, I guess x = NULL. If I try:

>
> That's your misunderstanding. That loop does *nothing*,
> it doesn't enter the loop body. When you're looping over
> an empty sequence, the loop body is never entered.

OK, so python knows not to even bother going through the loop if the
range is [].
>
> (also, NULL is meaningless in Python. I think you meant
> None, right?)

Yes, as in range(2, 2) yields nothing.

I have another question which I hope you can answer. If I do

2 % 2 in the python interpreter, the answer is 0, and yet 2 is a prime
number.

Don
>
> --Irmen
>

--
Thanks,

Don

Michael Geary
Guest
Posts: n/a

 11-08-2003
> #!/usr/bin/python
>
> 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 have another question which I hope you can answer. If I do
>
> 2 % 2 in the python interpreter, the answer is 0, and yet 2 is
> a prime number.

2 modulo 2 is zero. Any number modulo itself is zero.

If the question is why the code above reports 2 as a prime number, that's
because it never gets to the "if n %x == 0" when n is 2. "for x in
range(2,2)" does not execute the loop at all but goes directly to the else.

I think you would find it helpful to step through this code line by line in
any Python debugger such as PythonWin or IDLE--that way you can see exactly
what it does as it executes.

-Mike

Duncan Smith
Guest
Posts: n/a

 11-09-2003

"Don Low" <(E-Mail Removed)> wrote in message
news:boju9f\$1fgli4\$(E-Mail Removed)-berlin.de...
> In article <3fad715f\$0\$58715\$(E-Mail Removed)4all.nl>, Irmen de Jong

wrote:
> > Don Low wrote:
> >>>>>for x in range(2, 2):
> >>
> >> ... print x
> >> ...
> >>
> >> Since x prints nothing, I guess x = NULL. If I try:

> >
> > That's your misunderstanding. That loop does *nothing*,
> > it doesn't enter the loop body. When you're looping over
> > an empty sequence, the loop body is never entered.

>
> OK, so python knows not to even bother going through the loop if the
> range is [].
> >
> > (also, NULL is meaningless in Python. I think you meant
> > None, right?)

>
> Yes, as in range(2, 2) yields nothing.
>

It doesn't return nothing; it returns an empty list. You *can* write a
function that returns None.

>>> def func():

return

>>> a = func()
>>> a is None

True
>>>

Duncan