[Edu-sig] Using Python in CS1
André Roberge
andre.roberge at gmail.com
Wed Apr 20 01:07:54 CEST 2005
From time to time, the question of convincing colleagues to adopt
Python as a teaching language in CS1 arises on this list. Here's
something you might consider...
In 1981, Richard Pattis wrote a delightful little book titled Karel
the Robot, a Gentle Introduction to the Art of Programming. In this
book, Pattis introduces the main concepts of sequential programming
(including loops and decisions, but not variable assignments) using the
paradigm of instructing a robot capable of only four basic actions
(turning left, moving one step forward, picking up and putting down
beepers). Through the "magic" of programming, the robot "learns" to
combine those four basic actions in order to accomplish tasks of
increasing complexity. Pattis used Pascal, the preferred language of the
day, as a means of "teaching" the robot new tricks. Since then, many new
versions of Karel the Robot have surfaced, used to introduce students to
various computer languages, with a preference for Java and C++, which
are both based on the modern Object-oriented programming (OOP) approach.
However, the complexity of Java and C++ contrasts with the simplicity of
the robot world; both these languages seem at odds with the idea of
providing a Gentle Introduction to the Art of Programming.
Enter Python... Python, like Java and C++, is an OOP language. However
Python also allows a non-OOP programming style which is more suitable
for interacting with Pattis's robot. A first implementation of Karel the
Robot in Python was called PyKarel. The current implementation is called
Guido van Robot (GvR for short), and is available at sourceforge.net.
While the included lessons have enough material for only about two weeks
of classes, they do, imho, illustrate (to anyone familiar with Karel the
Robot in other languages) the advantage of using Python.
========
If you are still with me at this point....
Consider the following situations:
1) a nameless robot (like Pattis's Karel) is stuck in a maze. He looks
for the exit in a random walk fashion. The exit is marked by a beeper.
Solution: (example of procedural code; Look Before You Leap)
# random maze solver
# exit identified by beeper
def turn_right():
turn_left()
turn_left()
turn_left()
while True:
if front_is_clear():
move()
if next_to_a_beeper():
turn_off() # success!
r = random()
if r < 1./3:
turn_left()
elif r < 2./3:
turn_right()
# keeps the same direction if r > 2./3 (and < 1)
==================
2) a named Robot (instance of a class) is stuck in a maze. He looks for
the exit in a random walk fashion. The exit is marked by a beeper.
Solution: (example of oop code; Better to Ask Forgiveness than Permission)
# random maze solver
# exit identified by beeper
Guido = BetterRobot() # knows how to turn right!
while True:
try:
Guido.move()
if Guido.next_to_a_beeper():
Guido.turn_off() # success!
except HitWallException:
pass
n = Guido.throw_dice()
if n in [1, 2]:
Guido.turn_left()
elif n in [3, 4]:
Guido.turn_right()
# keeps the same direction if n in [5, 6]
=========================================
I believe that the above examples
1) are easily readable and understood (Python as pseudocode!)
2) illustrate different coding practices (procedural vs oop)
without complicated syntax
3) illustrate different "philosophies" (LBYL vs BAFP)
4) are short enough to be used in a classroom situation
Might they help in convincing colleagues of the advantage of using
Python in CS1?
André Roberge
=========
Disclaimer: you can't quite do the above two examples with Guido van
Robot. A program similar to GvR, with supporting lessons, will soon be
available "publicly" (and freely!) that supports the above code.
More information about the Edu-sig
mailing list