[Tutor] How to teach Python

Python python at venix.com
Sun Aug 13 01:56:04 CEST 2006


On Sat, 2006-08-12 at 15:09 -0700, Elaine wrote:
> I am going to be teaching "Introduction to Python
> Programming" in the Fall at Foothill College in Los
> Altos Hills, CA. This is a 5 quarter unit class for
> students who already know one programming language.
> 
> I have been teaching programming for 20 years now,
> including Java and C++ as of late. As I prepare this
> class, I have some questions that I I am hoping you
> can shed some light on.
> 
> 1) Programming Style 
> 
>          while 1:
>               x = next()
>               if not x: break
> 
> I have never allowed students to use break or continue
> in any 
> programming class I have taught. I think of this type
> of code as belonging with the dreaded GOTO.
> 
> I find that students who are having difficulty writing
> 
> complex conditions in a loop use multiple if's with
> break's 
> inside the loop instead. Also, it is MUCH easier to
> read a 
> loop if the condition for continuing in the loop is
> right up 
> at the top of the loop where it belongs.
> 
> I have always thought of break and continue as hacks
> that 
> are not compatible with structured programming
> techniques. 
> The only exception is using break in a switch in C,
> C++ and 
> Java.
> 
> However, all Python books seem to contain sample
> programs like this one. Do you think that code like
> this is desirable?

YES.

In C you can code
	while ((c = getchar()) != EOF)
	<loop body>
which combines the assignment and the conditional

The equivalent Python code is something like:
	c = getchar()
	while c != EOF:
	<loop body>
	c = getchar()

Because the assignment is a separate statement and not an expression, it
cannot be combined with the condition.  So the Python consensus seems to
be that you are better of with one assignment and embed the condition
with a break at the proper spot within the loop.  The loop is simply
	while True:	# with newer Pythons
		c = getchar(1)
		if c == EOF: break
		<loop body>

In translating from other languages, note that many while loops can
become for loops in Python.  The while loop above came from K&R page 17,
	(The C Programming Language, Kernighan and Ritchie)
counting lines in a file.  A more natural Python translation would use
	nl = 0
	for line in input:
		nl += 1
	print nl

Certainly break and continue can be abused.  I would normally expect all
break and continue testing to be in one section of the while loop so
that it looks like

while True:
	<get next whatever we are looping on>
	<should we skip it: continue>
	<are we done yet: break>
	<loop body>

I'm glad to see Python getting taught in schools.  When my daughter's
college decided to stop teaching C++ as the first programming language
(the year she took the course), I urged them to seriously consider
Python, but they went with Java.

> 2) Since this is an introductory class, I am tempted
> to leave out "optional" topics like argument matching
> modes, walk, map, filter, reduce, apply. Do you think
> these are required for any Python programmer?

I think the argument matching is too important to skip.  For cases like
HTTP POST data, passing a dictionary around is pretty convenient when
you can pass it using the ** notation.  It allows you to code:

def celsius(fahrenheit, **kwds):
	return (fahrenheit - 32) * 5 / 9.0

postdata['celsius'] = celsius( **postdata)

So functions can specify just the dictionary keys they care about.  My
little example imagines an HTML form with a variety of variables that
include temperature conversions.

Also zip(*args) is the transpose function.  This is too handy to omit,
but requires covering the single * mode of matching arguments.

> 
> 3) I need to teach a GUI in this class. I would like
> something that is easy, standard, and multi-platform.
> I am considering Tkinter and Jython. Any opnions on
> this?
(not from me.)
> 
> Thanks for any answers you might have.
> 
> -Elaine Haight
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list