[Edu-sig] First things first (Re: Python in the Classroom)

Christian Mascher christian.mascher at gmx.de
Sat Feb 25 20:53:48 CET 2006


Hi,

I'm currently introducing 9th graders (15-16 year-old) with no prior
coding experience (apart from a little bit of HTML/CSS) to basic
programming with Python. We have only just started in this group.
Some background: We meet twice a week, on mondays for 90 minutes (two
lesson hours, one of them not in the computer lab, so only demonstration
possible)), on thursday for 45 minutes.

@ Kirby: 8th-graders are 14 year-olds, right? (That is: do you count
classes from primary school grade 1 (like we do here)? How many lesson 
hours do you have?

When starting with total newbies, you have to decide very carefully,
what to explain in what order. Python being so "easy" (for someone who 
already knows what programming is about), it is also easy to go too 
fast. And you will always want to introduce quite a lot of ideas pretty 
soon, in order do to something interesting. On the other hand: Learners 
will probably appreciate a common solution (say: loops) better, if they 
have noticed the tediousness of programming without such a means _first_.

I have looked at two books which address these items in  a similar way: 
Gregor Lingls German Book "Python für Kids" and the new book from 
Stephane Ducasse: Squeak - Learn programming with Robots (doing it the 
smalltalk way). Both take a Logo-like approach with turtle graphics as a 
first start. Something which always seems to work with children of all 
ages in my experience (besides producing nice graphics as a visual 
feedback and reward).

Differences (in the mentioned books) of course concern the syntax and 
the environment. For Python you have to explain the difference between 
interacting with Python via the Idle Shell-Window and using a 
File-Window (for saving the work properly) soon. These are "first 
things" you can't leave out.

Then Ducasse produces turtles as Objects and calls methods on these to 
draw simple pictures. No problem. Rather than using turtle.py directly, 
I have always put an extra file named turtlegraphics in a reachable 
directory on the network (in my pythonpath). I have now modified it to 
include no module-level functions but only a Turtle class - of course 
just an interface to turtle.py. I also like it, when the method names 
are German - you see at a glance the difference between Python keywords 
and simple methods of an Object.
So the standard interaction is (translated back to English):
 >>> from turtlegraphics import Turtle
 >>> joe=Turtle()	# I don't really explain this much
 >>> joe.forward(80)
 >>> joe.color('red')

So right from the start, we have dot-notation calling methods on 
instances we first have to create by calling a "factory" Turtle.

Another good thing is also that
 >>> dir(joe)
will only deliver the known methods, wheras the module turtle.py (where 
all the real programming was done for me) contains all sorts of things, 
and no class Turtle, but a Pen.

This is quite the way Ducasse does it in Smalltalk/Squeak. I only can't 
exchange turtle joe's shape with my own drawing (like in squeak)- but I 
have found Python Tkinter runs a lot faster on my old machines than the 
fullblown squeak environment. And why not stay simple, when teaching 
simple, beginner things?

How do we start?

I try to introduce only _one_ *big idea* at once. On top of this there
is still a lot of small bits (syntax, file saving, using the IDLE-Shell,
correcting errors etc.) they have to learn along the way.

Some first *big* ideas in my sequence are:

1. Code as a linear flow of statements as "commands"
2. Functions without parameters as abbreviation for longer parts of
reuseable code
3. Loops on a list

We actually did some "Using Python as a Calculator" in the Idle-Shell in
the first lesson (Big Idea #1: Shell commands). Long Integers are a
motivating factor (which pocket calculator could really tell you the
result of 2**100?). I also introduced math.sqrt() because luckily they
have just learned sqrt in maths, and it gives a first impression of
"functions".
More than one of them stumbled over some "wrong" results (float
representation): To explain this properly, I would have needed another
lesson, but I guess at this stage it is not that they are _that_
interested in the question (just wanted a _short_ answer).
In a second lesson we defined some own mathematical functions in an
informal way: Just like a function in maths with an x between the
brackets and "return" something to deliver the result. No further 
treatment of this at this stage - I want turtlegraphics now.

In the first lesson with turtlegraphics, pupils drew simple pictures 
(and had to use pythagoras a**2+b**2=c**2 to compute the length of a 
diagonal) using turtles they gave a funny name first.

Then we defined a function triangle() (deliberately without parameters 
first time) to draw *many* triangles, reusing code by calling "subprograms".

I let them figure out how to draw a "color wheel" with six triangles,
using our triangle() function. This, they should do in a file, so they
know about saving modules:
-----start file-----
from turtlegrphics import Turtle
t=Turtle()
def triangle():
   <snip: its a regular (60-degrees) Wossname-triangle>

# main program:
t.color('red')
t.fill(1)
triangle()
t.fill(0)
t.left(60)
t.color('orange')
t.fill(1)
triangle()
t.fill(0)
<and so on 4 more triangles, purple last>
---------------
I hope this tedious repetition with only the color changing
is motivation enough to call for a loop on a list:

for colorname in ['red','orange','yellow','green','blue','purple']:
	t.color(colorname)
	t.fill(1)
	... you get the impression

 From there on I will develop the loop for drawing a square:
>>> for sidenumber in [1,2,3,4]:
... actually we don't need sidenumber here
>>> for sidenumber in [0,1,2,3]:
... because computer scientists start counting with a zero
>>> for sidenumber in range(4):
... made it to range() at last. Now we can do quite neat things looping
with one or more turtles, exercising for-loops (and without much talk, 
lists on the way)

>>> for i in range(18):
	for i in range(4):
		joe.forward(50)
		jack.forward(50)
		joe.left(90)
		jack(right(90)
	joe.left(10)
	jack.right(10)

Sorry for the rather long post, but you didn't have to read it,
did you ;-)

Cheers, Christian




More information about the Edu-sig mailing list