[Tutor] while statement

Emil Styrke emil@lysator.liu.se
Fri May 2 07:50:02 2003


Sean 'Shaleh' Perry <shalehperry@attbi.com> writes:

>>
>> Another method is the for loop, it will save you the manual
>> incrementing of n:
>>
>> for n in range(1, N+1):
>>     do_stuff
>>
>> range(x, y) creates a list with the integer elements i, where x <= i < y,
>> and the for loop will iterate through each element in this list,
>> assigning the current element to n.
>>
>
> I personally find this horribly wasteful.  xrange() at least generates the 
> next element when it is needed.

You're right.  I've never worked with ranges so large that it
mattered, but when the difference in code is just one letter, I really
should get into the habit of putting it there.

       /Emil

> range(1,100) # builds a 100 item list, ick.
>
> Sure this is a scripting language, but no need to waste cycles uselessly.
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor