Irv Kalb wrote:
In Python 2, I easily
demonstrated the range function using a simple print statement:

print range(0, 10)

I discussed how range returns a list.  I gave many examples of different
values being passed into range, and printing the resulting lists.

Next, I introduced the concept of a for loop and show how you use it to
iterate over a list, for example:

for number in [12, 93, -45.5, 90]: # Some operation using each number (for
example, adding up al the numbers)

When that was clear, I would go on to explain how you could incorporate range
in a for loop:

for someVariable in range(0, 10):

Don't start with range(). Start with lists, and introduce the for
loop as a way to iterate over lists. Leave range() until much later.
You should be able to go a *long* way without it -- it's quite
rare to need to iterate over a range of ints in idiomatic Python
code.

When you do get to range(), just say it returns an object that
produces a series of ints when you iterate over it. By now they
should have seen enough examples of other iterable objects --
lists, tuples, etc. -- that this will make sense.

DON'T call it a "generator", btw -- that term is reserved for
a very special kind of iterator, and range() isn't one of them.

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

Reply via email to