Python Turtle Graphics
A couple weeks ago, I was telling some people about the Logo programming
language from Seymour Papert, how much of an impact it had on my
formative years as a great educational tool: not just for programming,
but also for math, geometry and simply "logic" in general. I kept that
conversation in my mind for a while and then I started to wonder how
cool it would be to write a simple set of Logo commands to be used from a
Python prompt. Nothing fancy, just a small module that would allow one
to do something like this:
from pylogo import *
And then
proceed to combining regular Python commands with calls to forward(),
right() and other common Logo commands right away. For the sake of
simplicity, the module would take care of managing a simple
tkinter-powered window with the Logo graphics in it.
I eventually
saved some time to proceed implementing that idea. And I think it was
around the first 5 minutes of writing the very few first blocks of my
boilerplate code, I ran into an "issue". I got this warning from
Pylance:
"turtle.py" is overriding the stdlib module "turtle"
I
actually paused there for a while, reading that multiple times to make
sure I didn't need new glasses. Like, what? Does Python's standard
library have a "turtle" module? What's that? What would that even do?!
And then after a quick search on Ecosia, I found out about this:
https://docs.python.org/3/library/turtle.html
That's
right. Python has its own Logo implementation, a.k.a. "Python Turtle
Graphics". And apparently, this is not new to Python 3, it's been there
since Python 2 at least. I mean, how amazing is that? And how incredible
it is that four decades after enjoying Logo as a kid on 8bit MSX
computers, I somehow become a Python developer along that journey, and
then now I discover Python has its own "Logo" implementation embedded in
it? I can only believe this story because I have just lived it.
You can try it yourself, right now, on your own computer:
- open a terminal
- enter "python" and hit enter
- copy and paste these simple instructions there:
from turtle import *
for steps in range(3):
forward(100)
right(120)
You
should see a triangle in a canvas window. The code basically says:
repeat 3 times moving a 100 pixels forward and turning 120 degrees to
the right on each step.
If this episode did not confirm to me how
much of a great programming language (and also a tool, in general)
Python is, nothing will. And I'm now really looking forward to showing
some "turtle graphics" to the next enthusiastic kid that approaches me
with curiosity about computing and programming (and math, and geometry,
the list goes forever on).
That's it. I hope you enjoyed learning
that Python is full of surprises and having just a little taste of what
Logo can do. And Python never ceases to amaze us in a very positive
way.
Comments
Post a Comment