Nested for loop problem

Discussion in 'Python' started by Don Low, Nov 8, 2003.

  1. Don Low

    Don Low Guest

    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
    Don Low, Nov 8, 2003
    #1
    1. Advertising

  2. 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
    Irmen de Jong, Nov 8, 2003
    #2
    1. Advertising

  3. Don Low

    Don Low Guest

    In article <3fad715f$0$58715$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.

    Thanks for your time,

    Don
    >
    > --Irmen
    >



    --
    Thanks,

    Don
    Don Low, Nov 8, 2003
    #3
  4. > #!/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
    Michael Geary, Nov 8, 2003
    #4
  5. Don Low

    Duncan Smith Guest

    "Don Low" <> wrote in message
    news:boju9f$1fgli4$-berlin.de...
    > In article <3fad715f$0$58715$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
    Duncan Smith, Nov 9, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    456
    Victor Bazarov
    Dec 7, 2007
  2. notahipee

    nested for loop problem

    notahipee, May 5, 2008, in forum: C++
    Replies:
    8
    Views:
    352
  3. Adrienne Boswell

    Re: problem with nested loop (I think)

    Adrienne Boswell, Jan 25, 2009, in forum: HTML
    Replies:
    1
    Views:
    355
  4. middletree

    problem with nested loop (I think)

    middletree, Jan 23, 2009, in forum: ASP General
    Replies:
    8
    Views:
    110
    Adrienne Boswell
    Jan 25, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page