Re: using range() in for loops

2006-04-05 Thread Alex Martelli
Georg Brandl <[EMAIL PROTECTED]> wrote: > Steven D'Aprano wrote: > > On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote: > > > >> [EMAIL PROTECTED] wrote: > >>> hi John, > >>> Python doesn't provide for loop like C / C++ but using Range() or > >>> Xrange() you can achive all the function

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
Steven D'Aprano wrote: > On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote: > >> [EMAIL PROTECTED] wrote: >>> hi John, >>> Python doesn't provide for loop like C / C++ but using Range() or >>> Xrange() you can achive all the functionalities of the C for loop. >> >> Not quite. > > Care

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
Steven D'Aprano wrote: > On Wed, 05 Apr 2006 16:21:02 +0200, Georg Brandl wrote: > >> Because of backwards compatibility. range() returns a list, xrange() an >> iterator: list(xrange(...)) will give the same results as range(...). > > Georg is pretty much correct in his explanation, but just to d

Re: using range() in for loops

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 16:21:02 +0200, Georg Brandl wrote: > Because of backwards compatibility. range() returns a list, xrange() an > iterator: list(xrange(...)) will give the same results as range(...). Georg is pretty much correct in his explanation, but just to dot all the I's and cross all the

Re: using range() in for loops

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote: > [EMAIL PROTECTED] wrote: >> hi John, >> Python doesn't provide for loop like C / C++ but using Range() or >> Xrange() you can achive all the functionalities of the C for loop. > > Not quite. Care to explain what the differences are,

Re: using range() in for loops

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 09:16:37 -0400, AndyL wrote: > Paul Rubin wrote: >> Normally you'd use range or xrange. range builds a complete list in >> memory so can be expensive if the number is large. xrange just counts >> up to that number. > > so when range would be used instead of xrange. if xrange

Re: using range() in for loops

2006-04-05 Thread Sion Arrowsmith
AndyL <[EMAIL PROTECTED]> wrote: >Paul Rubin wrote: >> Normally you'd use range or xrange. range builds a complete list in >> memory so can be expensive if the number is large. xrange just counts >> up to that number. >so when range would be used instead of xrange. if xrange is more >efficient,

Re: using range() in for loops

2006-04-05 Thread Adam DePrince
On Tue, 2006-04-04 at 21:54 -0400, John Salerno wrote: > I'm reading Text Processing in Python right now and I came across a > comment that is helping me to see for loops in a new light. I think > because I'm used to the C-style for loop where you create a counter > within the loop declaration,

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
AndyL wrote: > Paul Rubin wrote: >> Normally you'd use range or xrange. range builds a complete list in >> memory so can be expensive if the number is large. xrange just counts >> up to that number. > > so when range would be used instead of xrange. if xrange is more > efficient, why range was

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
[EMAIL PROTECTED] wrote: > hi John, > Python doesn't provide for loop like C / C++ but using Range() or > Xrange() you can achive all the functionalities of the C for loop. Not quite. Georg -- http://mail.python.org/mailman/listinfo/python-list

Re: using range() in for loops

2006-04-05 Thread AndyL
Paul Rubin wrote: > Normally you'd use range or xrange. range builds a complete list in > memory so can be expensive if the number is large. xrange just counts > up to that number. so when range would be used instead of xrange. if xrange is more efficient, why range was not reimplemented? --

Re: using range() in for loops

2006-04-05 Thread John Salerno
Roel Schroeven wrote: > In many cases loops really are for iterating over sequences; more so > than I realized when using for loops in C or C++. In these cases, > Python's for statement works better than C-style loops. And if you > really need to do something a certain number of times, there's

Re: using range() in for loops

2006-04-05 Thread Roel Schroeven
John Salerno schreef: > I'm reading Text Processing in Python right now and I came across a > comment that is helping me to see for loops in a new light. I think > because I'm used to the C-style for loop where you create a counter > within the loop declaration, for loops have always seemed to m

Re: using range() in for loops

2006-04-05 Thread Ant
It's not just a Python thing, Java for example generally uses the idiom: for (Iterator it = list.iterator(); it.hasNext(); ) { Object next = it.next(); //Do stuff to next } Horrible compared to the python idiom of course (though the latest version supports for (x : list){}) Ruby has some

Re: using range() in for loops

2006-04-05 Thread Ben Sizer
John Salerno wrote: > The reason for this distinction comes from the fact that I read a lot > how using range and for is somewhat discouraged, because it doesn't > really use a for loop for it's true purpose. So my question is, is this > just a Python-oriented opinion about for loops, or is it a ge

Re: using range() in for loops

2006-04-04 Thread sushant . sirsikar
hi John, Python doesn't provide for loop like C / C++ but using Range() or Xrange() you can achive all the functionalities of the C for loop.If you wants distributed for loop You can use Xrange. John Salerno wrote: > I'm reading Text Processing in Python right now and I came across a > comment

Re: using range() in for loops

2006-04-04 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > The reason for this distinction comes from the fact that I read a lot > how using range and for is somewhat discouraged, because it doesn't > really use a for loop for it's true purpose. So my question is, is > this just a Python-oriented opinion about for