[Python-ideas] Simpler syntax for basic iterations

Andre Roberge andre.roberge at gmail.com
Fri Oct 9 22:22:43 CEST 2015

Summary:  Visual environments based on using
simple functions without using variables have been found
to be useful to introduce programming concepts to young
learners.  While Python is an ideal language for beginners,
the Python construct for iterations, namely

for var in range(n):
    # block
    # of
    # code

does require a variable and is needlessly complicated for
beginners.  A simpler construct like:

repeat n:    # n is a specific integer
    # block
    # of
    # code

would be useful to have in such contexts.  Other keywords could
be chosen instead of "repeat" used above.


For young (and perhaps not so young) students
visual environments are an ideal choice to learn programming.
A well-known example of a visual environment for beginners
is Karel the Robot, invented by Richard Pattis in 1981.
In this environment, a robot can perform a limited number
of basic functions which can be combined to create
complex tasks.

In the original Karel the Robot, variables were not
allowed.  However, new procedures could be created.

One essential concept that can be demonstrated in such worlds
is that of iterations.  In the original Karel, this was
done using the following syntax:




where "n" is a specific integer.  In the above, indentation
has been introduced only to make the code easier to read.

A popular Python-like implementation of Pattis' idea
is Guido van Robot [1], also known as GvR.
In GvR, iteration is done as follows:

do n:
    # block
    # of
    # code indented like Python

To do something equivalent in Python requires to use:

for var in range(n):
    # block
    # of
    # code

which requires to use variables, which are not part of
the original Karel the Robot philosophy, as well as an
additional builtin function.  I would argue that,
for beginners, this is needlessly complicated.
A better alternative would be to use a similar
syntax to that used by Guido van Robot for this case.
This would require to either introduce a new keyword
(such as "do", "repeat", "iterate", or "loop")

repeat n:
    # block
    # of
    # code

An advantage of using one of these keywords would
be for easier readability (for English speakers).
The disadvantage is that some existing Python programs
might use variables with the names suggested

An alternative would be to reuse the "for" keyword by
adding this new construct to the grammar:

for n:
    # block
    # of
    # code

This last choice, while perhaps not as immediately readable
to English speakers as some of the previous ones, would
have the advantage of not requiring any change to
any existing Python programs; it could thus be implemented
rather quickly without needing some transition
time and the use of the __future__ module.

There exists at least 4 different pure Python implementations of
Karel the Robot where this new construct could, in principle,
be immediately put to use:

RUR-PLE [2], Rurple NG [3], both of which are desktop versions,
Reeborg's World [4], a web version using Brython as a Python
interpreter, and Monty Karel [5], an Eclipse plugin which
is targeted at older students as it uses a not so simple
OOP syntax even for the simplest programs.
Young learners using any of the first three such implementations
would likely benefit if they could learn programming first
using this simpler construct.

André Roberge

[1] Guido van Robot:

[2] RUR-PLE: https://code.google.com/p/rur-ple/

[3] Rurple NG:

[4] Reeborg's World  documentation: http://reeborg.ca/docs/en/index.html
    World itself: http://reeborg.ca/world.html
    Python tutorial: http://reeborg.ca/docs/begin_py_en/

[5] Monty Karel:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151009/6d4dbff2/attachment.html>

More information about the Python-ideas mailing list