Stefan Ram wrote:
"ROGER GRAYDON CHRISTMAN" <d...@psu.edu> writes:
On Wed, Oct 4, 2017 22:42 Stefan Ram (r...@zedat.fu-berlin.de) wrote:
Steve D'Aprano <steve+pyt...@pearwood.info> writes:
So, "bottom-up" in this case means: iterators should be
taught before for-loops.
Why?
The easy answer here is to not use the range in the first for loop.
   I never intended to use »range«. But I also will not use lists.

   Very early in the course, I teach numeric and string literals:

1, 2.3, 'abc'

   then come operators, functions and »if«, »while« and »try«.
   But neither »range« nor lists have been shown so far.

As long as I have two teachers here, which textbooks are you using? I am hoping to teach a college course in Python next fall.

Thanks,
Bill




   The basic course may already and there after about 12 - 18 hours.
   (This time includes many exercises in the classroom.)

   But if I have time to introduce »for«, I'll do it as follows
   at this point in the course:

   <now speaking like a teacher:>

   We want to walk through (traverse) a string
   character-by-character:

   To do this we need a walker. A walker can be
   obtained using »iter«:

|>>> walker = iter( 'abc' )

   Now, we can get character after character from the walker
   using »next« (transcript simplified):

|>>> next( walker )
|'a'
|>>> next( walker )
|'b'
|>>> next( walker )
|'c'
|>>> next( walker )
|StopIteration

   We can use »while« to automate this:

def example():
     walker = iter( 'abc' )
     try:
         while True:
             print( next( walker ))
     except StopIteration:
         pass

   A walker also is known as an /iterator/.

   An object, one can get an iterator from
   is called an /iterable object/.

   Strings are iterable objects.

   This for-loop does just what our previous
   while-loop did:

def example():
     for ch in 'abc':
         print( ch )

   It gets an iterator from »'abc'« and then does the suite
   repeatedly with »ch« being the result of a each »next«
   call until StopIteration.

   </end of "speaking like a teacher">

   No »range«, no list.

   (Yes, »print('\n'.join('abc'))« can do the same and will
   be shown later in the course if there is still time.)


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to