osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The "loop and a half"


On Thu, Oct 5, 2017 at 1:20 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> On Thu, 5 Oct 2017 09:42 am, Stefan Ram wrote:
>
>> Steve D'Aprano <steve+python at pearwood.info> writes:
>>>>So, "bottom-up" in this case means: iterators should be
>>>>taught before for-loops.
>>>Why?
>>
>>   The syntax for is (from memory):
>>
>> for <variable> in <expression>: <suite>
>>
>>   . As an example, I might show:
>>
>> for i in range( 3 ): ...
>>
>>   . This raises the question:
>>
>>       "What is the value of ?range( 3 )??".
>
> That's easy: in Python 2, it is a list [0, 1, 2]. In Python 3, it is a range
> object, a lazily calculated list-like object.

Even easier: in any version of Python, it is the series of numbers 0, 1, 2.

You can go a rather long way without worrying about what "series"
actually means at the concrete level. It's a thing you can iterate
over, and that's all that matters to 99%+ of range() usage.

ChrisA