#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0

    newb: loop comparison


    well ..... my concern is: do those two loops the same? if yes which one should be preferred and why?

    I'm a bit confused b/c I don't really know if the range function is inclusive or not (I'm opted for not in that code)
    assuming digit and maxDigit are both times the same value

    Code:
    for digit in range(maxDigit + 1):
              blah...
    and

    Code:
    while digit <= maxDigit:  
              blah...
              digit += 1
    any help really appreciated
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The easiest way to find the answer to questions like this is to try it and see. The Python interactive console makes it very easy to experiment, so take advantage of it:

    Code:
    >>> maxDigit = 10
    >>> for digit in range(maxDigit+1):
    ... 	print digit,
    ... 	
    0 1 2 3 4 5 6 7 8 9 10
    >>> digit = 0
    >>> while digit <= maxDigit:
    ... 	print digit,
    ... 	digit += 1
    ... 	
     0 1 2 3 4 5 6 7 8 9 10
    >>>
    You can also get interactive help from within the session:

    Code:
    >>> help(range)
    Help on built-in function range:
    
    range(...)
        range([start,] stop[, step]) -> list of integers
        
        Return a list containing an arithmetic progression of integers.
        range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
        When step is given, it specifies the increment (or decrement).
        For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
        These are exactly the valid indices for a list of 4 elements.
    
    >>>
    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0
    thx
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You might also want to look at the xrange() function. Its the same deal as range(), only being a generator, xrange() doesn't create the entire list in-memory. So for large numbers its much more efficient.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0
    thx ... nice to know I'll of course test it, but it'll normally proof to be better when it's in the memory in my case
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Of course . May i ask why it would be better in memory? Since there isn't usually any knoticable differences between the two, only the latter is more memory efficent.

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0
    I'm checking for 'Noble Numbers' and therefor I init a matrice with all the powers in my range and then I start to check if they are 'noble' ... so perhaps it'd be faster if it's in memory or perhaps this assumption is just bs
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The list is still in memory, that hasn't changed, the difference is that with a generator the values are created as-needed rather than all at once (so takes up more memory).
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0
    thx ... now I get what you mean ... for my case it doesn't work now b/c the structure of the code had the other one in mind

IMN logo majestic logo threadwatch logo seochat tools logo