Teaching the "range" function in Python 3

Irv Kalb Irv at furrypants.com
Fri Jun 30 13:07:52 EDT 2017


> On Jun 29, 2017, at 2:21 PM, Chris Angelico <rosuav at gmail.com> wrote:
> 
> On Fri, Jun 30, 2017 at 6:57 AM, Irv Kalb <Irv at furrypants.com> wrote:
>> 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?  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?
>> 
> 
> The easiest way is to use the word "collection" for things you iterate
> over (rather than the concrete term "list"). So you can loop through
> (or iterate over) a collection of explicitly given numbers:
> 

Thanks to everyone who responded to my question about teaching the range function.  

I particularly like the ideas of using the words "collection" and "sequence"- those seem to be very clear.

In my curriculum, I first go through a in detail discussion of the need for, the syntax of and the usage of lists. Then introduce for loops and explain how to iterate through a list (and I do use a naming convention).  At the very end of my discussion of lists and loops, I talk about the additional use of "range".   I do not want to go into any details of the underlying implementation of range, I just wanted a way to explain how it could be used in a for statement.  

(I understand that xrange does the same thing in Python 2 as range in Python 3, but for the sake of simplicity, I do not teach xrange in my Python 2 class.)

Thanks for all the comments about range not being a generator - I'll be sure to steer clear of that word in my introductory class. 

Not sure how this got into a discussion of function calls, etc.  

Thanks very much,

Irv




More information about the Python-list mailing list