Introducing the "for" loop
On Thu, Oct 5, 2017 7:45 PM, breamoreboy at gmail.com wrote:
On Friday, October 6, 2017 at 2:05:58 AM UTC+1, Irv Kalb wrote:
>> The range function is discussed after that.
>FWIW range isn't a function in Python 3. From
https://docs.python.org/3/library/functions.html#func-range "Rather than being
a function, range is actually an immutable sequence type, as documented in
Ranges and Sequence Types ? list, tuple, range.".
Despite the documentation, I would still be tempted to say that range is a
Taking duck-typing to the meta-level, every time I use range, I use its name
by a pair of parentheses enclosing one to three parameters, and I get back an
immutable sequence object. It sure looks like a function to me.
I would similarly say that map is a function, or an iterator generator I write
with yield statements is a function, both of which also return sequences.
It is not clear to me what the difference really is between my conception
and the official definition -- is it a question about whether it returns a
Or more simply, what is the clear and obvious distinction that I can give to my
non-scientific laypeople about why range isn't a function, if it looks like one?
Oh, and since I think this was snipped from the thread mentioning textbook
I'll just toss in my note that I am using an on-line textbook developed by
and those guys are nice enough to actually collaborate with the instructor about
what goes into the text, and in what order.
I actually like that they choose to present lists and dictionaries before any
structures, because a lot of interesting simple programming problems can be
by making use of those structures -- like simple table-lookup applications.
A C++ teacher really cannot present arrays before control structures, because
there is nothing in the language to manipulate the arrays (without appealing to
But with these objects as built-in's, we can encourage nice and simple code
unnecessarily complicated control structures.
Unfortunately, it seems the zyBooks authors still succumb to the same
as any other programmer who learned control structures before data structures.
Soon after introducing those control structures, they have an exercise to do a
translation, using an ugly 12-way if-else construct nested within a loop where
a dictionary would very easily trim the if-else and str.translate() would
eliminate the loop.
(This is fresh in my mind because I plan to illustrate both their solution
and mine in my very next class)
Pennsylvania State University