Teaching the "range" function in Python 3

Rick Johnson rantingrickjohnson at gmail.com
Thu Jun 29 22:33:37 EDT 2017


On Thursday, June 29, 2017 at 4:01:07 PM UTC-5, Irv Kalb wrote:
>
> [...]
>
> But Python 3's version of the range function has been
> turned into a generator.  Again, I understand why this
> happened, and I agree that this is a good change.  The
> problem is, how can I explain this concept to students who
> are just learning lists, function and function calls, etc.
> The concept of how a generator works would be extremely
> difficult for them to get.  The best approach I have seen
> so far has been to build a simple for loop where I print
> out the results, like this:
>
> for someVariable in range(0, 10):
>     print(someVariable)
>
> However, I'm worried that this might lose a lot of
> students.  From their point of view, it might seem
> difficult to understand that range, which looks like a
> simple function call passing in the same values each time,
> returns a different value every time it is called.

You are making the same fatal mistake that many "too bright
for their own good" teachers have made since antiquity, and
that is, you are bombarding the student with too many
concepts at once. And for the archetypal pythonic example of
what *NOT* to do, one need look no further than the grease
and semen stained pages of the official python tutorial:

    [QUOTE]

    4.6. Defining Functions

    >>> def fib(n):    # write Fibonacci series up to n
    ...     """Print a Fibonacci series up to n."""
    ...     a, b = 0, 1
    ...     while a < n:
    ...         print(a, end=' ')
    ...         a, b = b, a+b
    ...     print()
    ...
    >>> # Now call the function we just defined:
    ... fib(2000)
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

    [/QUOTE]

As you can see, the author seems more interested in
showboating than teaching. :-( If the purpose of this section 
is to teach the fundamentals of of functions, then the author
has failed miserably. A better *FIRST* example would be
something like this:

    def add(x, y):
        return x + y

When teaching a student about functions, the first step is
to help them understand *WHY* they need to use functions,
and the second is to teach them how to define a function. In
my simplistic example, the practical necessity of functions
can be easily intuited by the student without the
distractions of (1) doc-strings, (2) tuple unpacking, (3) an
inner loop structure, (4) implicitly writing to IO streams
using the print function, (5) using advanced features of the
print function, (6) more tuple unpacking (this time with a
twist of lime), (7) and an implicit newline insertion using
the print function

(deep breath)
    
Now, would someone please remind me, again, what were we
learning about???

GHEEZ! 

So with that plate of scholastic spaghetti in mind, let's
return to your dilemma:

> for someVariable in range(0, 10):
>     print(someVariable)
>
> However, I'm worried that this might lose a lot of
> students.  From their point of view, it might seem
> difficult to understand that range, which looks like a
> simple function call passing in the same values each time,
> returns a different value every time it is called.

First of all, the range function is only called *ONCE* in your
example, which can easily be demonstrated by executing this
code...

    >>> for value in range(10):
    ...     print(value)
    ...
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

And to further drive home the point, you can manually insert
a list literal to prove this:

    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> for value in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    ...     print(value)
    ...
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

Furthermore, you should not introduce loops and the range
function in the same example. Do not follow the lead of the
Python tutorial author. Instead, introduce both concepts
separately, and using the most simple example you can
muster.

    >>> for char in "abc":
    ...     print(char)
    ...
    a
    b
    c

Followed by...

    >>> for item in (1,2,3):
    ...     print(item)
    ...
    1
    2
    3

Now, once the student understands that range simply returns
a list of things, and that "for loops" iterate over each
_thing_ in a collection of _things_ (be that collection a
list, or a tuple, or a string, or whatever...), the student
will not be so dumbfounded by this code

    >>> for value in range(10):
    ...     print(value)
    ...
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

> I am wondering if other teachers have run into this.  Is
> this a real problem?  If so, is there any other way of
> explaining the concept without getting into the underlying
> details of how a generator works?

Generators are an advanced topic and should not be presented
until the student has a firm grasp of general programming
fundamentals. In python, the general fundamentals would be a
very basic, but confident, understanding of: integers,
floats, strings, lists, tuples, dicts, variables,
conditionals, loops, and functions. Followed by an in depth
study of the aforementioned.

> Do you think it would be helpful to use the words "sequence
> of numbers" rather than talking about a list here - or
> would that be even more confusing to students?

Being that the word "sequence" is highly generalized and
naturally intuitive, i don't find it to be confusing at all.
OTOH, the word "list" falls more into the realms of defining
organizational behavoirs that are specific to humans and
written language. But hey, tell that to the idiot who
designed virtual strings with escape codes for such distinct
mechanical phenomenon as line-feeds and carriage-returns!



More information about the Python-list mailing list