Nested for loop problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Don Low

    Nested for loop problem

    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:
    [color=blue][color=green][color=darkred]
    >>> range(2, 10)[/color][/color][/color]
    [2, 3, 4, 5, 6, 7, 8, 9][color=blue][color=green][color=darkred]
    >>> range(2, 2)[/color][/color][/color]
    []
    [color=blue][color=green][color=darkred]
    >>> for x in range(2, 2):[/color][/color][/color]
    .... 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 misunderstandin g something, but could someone explain?


    --
    Thanks,

    Don
  • Irmen de Jong

    #2
    Re: Nested for loop problem

    Don Low wrote:[color=blue][color=green][color=darkred]
    >>>>for x in range(2, 2):[/color][/color]
    >
    > ... print x
    > ...
    >
    > Since x prints nothing, I guess x = NULL. If I try:[/color]

    That's your misunderstandin g. 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

    Comment

    • Don Low

      #3
      Re: Nested for loop problem

      In article <3fad715f$0$587 15$e4fe514c@new s.xs4all.nl>, Irmen de Jong wrote:[color=blue]
      > Don Low wrote:[color=green][color=darkred]
      >>>>>for x in range(2, 2):[/color]
      >>
      >> ... print x
      >> ...
      >>
      >> Since x prints nothing, I guess x = NULL. If I try:[/color]
      >
      > That's your misunderstandin g. 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.[/color]

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

      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[color=blue]
      >
      > --Irmen
      >[/color]


      --
      Thanks,

      Don

      Comment

      • Michael Geary

        #4
        Re: Nested for loop problem

        > #!/usr/bin/python[color=blue]
        >
        > 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'[/color]
        [color=blue]
        > 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.[/color]

        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




        Comment

        • Duncan Smith

          #5
          Re: Nested for loop problem


          "Don Low" <m_tessier@symp atico.ca> wrote in message
          news:boju9f$1fg li4$1@ID-145503.news.uni-berlin.de...[color=blue]
          > In article <3fad715f$0$587 15$e4fe514c@new s.xs4all.nl>, Irmen de Jong[/color]
          wrote:[color=blue][color=green]
          > > Don Low wrote:[color=darkred]
          > >>>>>for x in range(2, 2):
          > >>
          > >> ... print x
          > >> ...
          > >>
          > >> Since x prints nothing, I guess x = NULL. If I try:[/color]
          > >
          > > That's your misunderstandin g. 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.[/color]
          >
          > OK, so python knows not to even bother going through the loop if the
          > range is [].[color=green]
          > >
          > > (also, NULL is meaningless in Python. I think you meant
          > > None, right?)[/color]
          >
          > Yes, as in range(2, 2) yields nothing.
          >[/color]

          It doesn't return nothing; it returns an empty list. You *can* write a
          function that returns None.
          [color=blue][color=green][color=darkred]
          >>> def func():[/color][/color][/color]
          return
          [color=blue][color=green][color=darkred]
          >>> a = func()
          >>> a is None[/color][/color][/color]
          True[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          Duncan


          Comment

          Working...