[Tutor] Python as Teaching Language

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Jan 20 11:41:37 CET 2014


On Sun, Jan 19, 2014 at 02:18:54PM -0500, Keith Winston wrote:
> On Sun, Jan 19, 2014 at 11:55 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> > It has reached the point that I'm back to looking for a new teaching
> > language. In Python 3 the decision has clearly been made to focus on
> > supporting Python's role as a professional software engineering language
> > at the expense of being a successor to ABC or for use in CP4E etc.
> > That's a fair enough decision but it does mean Python is no longer the
> > easiest option for non Comp Sci beginners. It's not worse than the others
> > but it's no longer clearly superior. (IMHO at least! )
> >
> > But what else is there? that's the problem.... :-(
> 
> Hi Alan, since this is off-topic from it's original thread, but I
> wanted to respond to it, I popped it into a new thread, I hope you
> don't mind (original was subject "iterators").

That's the right thing to do. The usual convention is to change the subject
line to "Python as a teaching language [Was: iterators]" so that it's clear
from the subject line that you've spawned a new thread from an existing one.

Alan, next year I will be teaching a new unit for our first-year Engineering
undergrads using Python as an introduction to programming so I've been
thinking about these things quite a lot recently. Any language has features
that you can't use in an intro course: so just leave them out!

If someone wants to spend lots of time learning Python comprehensively then
they can do that later. Thankfully you can do a lot in Python without fully
understanding its "underbelly".

As a case in point I don't plan to teach generators or iterators. I will
probably broach that subject as follows:

In Python there are many types of objects we can loop over with a "for"
statement. We have already seen examples of this with lists and strings e.g.:

>>> a = [4, 2, 5]
>>> for x in a:
...     print(a, 2*a)
4 8
2 4
5 10

Objects that we can loop over are known as "iterable". There are other types
of objects that are not iterable such as ints and floats:

>>> b = 123
>>> for x in b:
...     print(b)
TypeError: 'int' object is not iterable

Note how the error message tells us that this type of object ('int') is not
'iterable'.

We've already seen examples of looping over the lines of a text file. It's
common in other programming languages to write something like:

f = open('myfile.txt')
while True:
    line = f.readline()
    if not line: # empty string when we get to the end of the file
        break
    print(line.upper())  # Do whatever you want with the line here
f.close() # Always close the file when done!!!

Looping over the lines of a file is so common though that Python has a more
convenient version using the fact that a file object is iterable:

f = open('myfile.txt')
for line in f:
    print(line.upper())
f.close()

One other thing that we need to know here is that some iterables (e.g. lists
and strings) can be looped over multiple times whereas file objects can only
be looped over once. If you want to loop over the lines of the file again you
need to re-open the file (or seek back to the beginning).

I would then go on to relate all of the above to list comprehensions. I don't
think I'd bother with talking about iterators/iterables and iter/next except
in a more advanced Python course. In that course I would also cover generators
and other things but for an intro just skip over it.


Oscar


More information about the Tutor mailing list