Nested for loop problem

D

Don Low

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)
[]
.... 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?
 
I

Irmen de Jong

Don said:
... 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
 
D

Don Low

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.

Thanks for your time,

Don
 
M

Michael Geary

#!/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
 
D

Duncan Smith

Don Low said:
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.
return

Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top