Teaching the "range" function in Python 3

Rick Johnson rantingrickjohnson at gmail.com
Sun Jul 2 11:24:44 EDT 2017


On Thursday, June 29, 2017 at 9:58:23 PM UTC-5, Chris Angelico wrote:
> On Fri, Jun 30, 2017 at 12:33 PM, Rick Johnson
> > 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...
> 
> ... except that you've made your first function so trivial
> that it doesn't need to be a function. How does that help
> anyone understand why they need functions? 

The point, Christopher, is to showcase in the most simple
form possible, that functions package a block of code for
easy reuse. Actually this example showcases three major
elements of functions:

    (1) That functions accept arguments. (in this
    case, positional arguments. Keyword arguments and default
    arguments can be introducted in an advanced lesson)
    
    (2)That functions perform a specific task within the "body"
    of the function. (No need to complicate the example by
    turning it into a generator, calculating a Fibonacci
    sequence, starting a new thread, or creating a class factory
    for your effing bot-net!!!)
    
    (3) That functions can return a value to the caller.

Easy peasy. ABC, 123. Get it?

> What's the point of writing "add(5, 7)" rather than "5 +
> 7"?

Does the acronym "DRY" mean anything to you?

Does the concept of "code reuse" mean anything to you?

Does the concept of creating a "general solution" as opposed
to a "specific solution" mean anything to you?

Of course, no programmer would require such a basic "add"
function like this since Python numeric types have
mathematical operations available (it would be as practical
as training wheels on a tour de france racing bike!), but
the point is to offer simple examples that anyone can
understand. For example, even a primary school student
understands how to add two numbers together to produce a
result, and has also been exposed to variables in basic
algebra:

   x + 3 = 10 (What is the value of x?)
   
   7 + y = 10 (What is the value of y?)
   
So any student with even a basic understanding of
mathematics can intuit that x and y are simply placeholders
for numeric values.

> So you need something else around the outside to justify
> even having a function at all.

Why? So i can teach a noob to write spaghetti code? Okay, just
for the Lulz factor, do you care to offer a code example?
I'd love to watch your squirm your way out of this one!

> In the example in the tutorial, the details ...
> 
> > ... 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
> 
> don't need to be explained at all! They *just work*. You
> also don't have to explain in detail how garbage collection
> works, the way that Python's object and type models work,
> or how the electrons in your CPU are kept from portalling
> across in a quantum tunnel. None of that matters. So I
> support the tutorial's example over yours.

Of course you do! Because you don't care if new programmers
are presented with poor examples. From your POV, if someone
else is suffering, it's not your problem.

> Chris "not-so-Angelic, oh?!"





More information about the Python-list mailing list