I teach Python at two colleges in Silicon Valley. I currently teach an
introductory course on Python and most of my students have no programming
background whatsoever. Up until now, my colleges have been using Python 2.
But now, one of the colleges has made the jump to Python 3. So I am updating
my curriculum to Python 3's syntax and concepts. In general, it's all going
fine, the changes from raw_input to input, and print from a keyword/statement
to the print function have been very straight-forward. But ...
Now I am looking at the change in the range function. I completely understand
the differences between, and the reasons for, how range works differently in
Python 2 vs Python 3. The problem that I've run into has to do with how to
explain what range does in Python 3. 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):
explaining that range would return a list, and the for statement iterated over
that list. Very clear, very easy for new students to understand.
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.
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?
Thanks,
Irv
--
https://mail.python.org/mailman/listinfo/python-list