[Python-ideas] Simpler syntax for basic iterations
Andre Roberge
andre.roberge at gmail.com
Sun Oct 11 03:44:13 CEST 2015
On Fri, Oct 9, 2015 at 11:30 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 10/9/2015 4:22 PM, Andre Roberge wrote:
>
>> 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.
>>
>
> ...
>
> 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
>>
>
> ...
>
> An alternative would be to reuse the "for" keyword by
>> adding this new construct to the grammar:
>>
>> for n:
>> # block
>> # of
>> # code
>>
>
>
> ---
>
> I think trying to persuade core developers to mutilate Python a bit, at
> couple of years in the future, is futile.
It is unfortunate, given the original CP4E goal, but not altogether
unexpected -- although, I do find the word "mutilate" to be a bit strong.
> And unneeded.
I respectfully disagree (however see below).
The reason is that I don't believe in students having to unlearn something
(like a special repeat syntax available only in a specific environment)
when they might have been under the impression that it was standard Python
syntax. Furthermore, it might instill doubt in their mind about other
construct they have learn (are they standard? ... or were they too
something only available in the specific environment?...)
> Instead, translate, say 'repeat n:' into 'for _ in range(n):' with
> something like
>
> def robotrans(codetext):
> out = []
> for i, line in enumerate(codetext.splitlines()):
> if line.startswith('repeat'):
> <check rest of line and either append translation
> or raise SyntaxError with proper details>
> else:
> out.append(line)
> return '\n'.join(outlines)
>
> You could, for instance, patch IDLE two places (for shell and editor code)
> to run robotran on user code before compiling. You could also add 'repeat'
> to the list of keywords for colorizing. I would be willing to help
> (without any guarantee of code stability).
>
I don't use IDLE. To make things as easy as possible for beginners, I have
moved away from desktop programs (RUR-PLE) which require a download, to a
purely web version, accessible via a single URL. However, prompted by
your suggestion, I did implement a simplified syntax for iteration
(tentatively using "repeat"). If you click on the link below, it will
bring up a concrete example, ready to run. (Note: the syntax used in the
example may not work in the future ... say, in a few days from now, after I
discuss with other users of my site about this new possible syntax.) If I
do end up keeping something like this, I'll have to make sure it is flagged
very clearly as being non-standard, so that the students are properly
warned.
http://reeborg.ca/world_dev.html?proglang=python-en&world=%7B%0A%20%20%22robots%22%3A%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%22x%22%3A%201%2C%0A%20%20%20%20%20%20%22y%22%3A%201%2C%0A%20%20%20%20%20%20%22orientation%22%3A%200%2C%0A%20%20%20%20%20%20%22_prev_x%22%3A%201%2C%0A%20%20%20%20%20%20%22_prev_y%22%3A%201%2C%0A%20%20%20%20%20%20%22_prev_orientation%22%3A%200%2C%0A%20%20%20%20%20%20%22objects%22%3A%20%7B%7D%0A%20%20%20%20%7D%0A%20%20%5D%2C%0A%20%20%22walls%22%3A%20%7B%7D%2C%0A%20%20%22description%22%3A%20%22%22%2C%0A%20%20%22small_tiles%22%3A%20false%2C%0A%20%20%22rows%22%3A%2012%2C%0A%20%20%22cols%22%3A%2014%0A%7D&editor=from%20library%20import%20left%2C%20right%0A%0Arepeat%204%3A%0A%20%20%20%20move()%0A%20%20%20%20left()%0A%0Aright()%0A&library=left%20%3D%20turn_left%0A%20%20%20%20%0Adef%20right()%3A%0A%20%20%20%20repeat%203%3A%0A%20%20%20%20%20%20%20%20turn_left()%0A
> Guido recently approved on idle-dev that we rethink IDLE for beginners. I
> have already thought about adding the necessary hooks so that education
> developers like you could define beginner Python dialects, such as you
> suggest here. This would open a world of possibilities, such as 'move 3'
> versus 'move(3)', and foreign language keywords. I just started a thread
> 'Running beginner Pythons'.
I'll have a look. However, as I mentioned above, I'm not keen on
introducing what amounts to a potentially completely different language.
Python is a fantastic language for beginners (and advanced) programmers ...
I was just hoping to make it a tiny bit easier to learn for complete
beginners who are introduced to Python in visual environments (like the
turtle module or a Karel the Robot clone).
André Roberge
>
>
> --
> Terry Jan Reedy
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151010/f5c18609/attachment.html>
More information about the Python-ideas
mailing list