The body syntonic
Get started with Python and a famous part of the venerable Logo language – Turtle Graphics.
Today’s top developers learned to code through a number of very different pathways. We mentioned BBC BASIC earlier, but we didn’t mention FORTRAN, COBOL or any of the Algol family, which were de rigeuer for scientists, mainframe operators and academics respectively back in the day.
FORTRAN (originally FORmula TRANslator) was developed by IBM in the 1950s and quickly became the gold standard for scientific computation and engineering. It’s still in use today, particularly on supercomputers working on numerically difficult problems such as weather prediction, fluid simulations and crystallography. BASIC was largely based on FORTRAN II. And Fortran (the dated portmanteau and shouty capitals have since been left behind) continues to be updated to accommodate modern coding practices, such as objects, parallelism and concurrency.
C was developed by Dennis Richie in 1972 and by the 80s had become incredibly popular. It was fast, but more importantly it was portable. Its design very much mirrored what was happening on the CPU, so virtually every CPU architecture at one stage had a C compiler written for it. It’s still very much around today (most of the Linux Kernel is written in C), but the traditional ‘hard’ languages (C, C++, Fortran and Java) are all facing something of a reckoning. Unlike the old, new languages – namely Rust, Go and Swift – don’t trust the programmer to manage memory (de)allocation. These ‘memory-safe’ languages obviate the buffer overflows (and associated vulnerabilities and exploits) that plague modern computing.
Another storied language we haven’t yet mentioned is Logo. Logo was designed to teach Lisp (a functional programming language), but it’s best known for its use of Turtle Graphics. This dialect is a drawing (more accurately ‘plotting’) language where commands are issued to a virtual turtle who can then draw, turn and change colour in any way you can express. Turtle Graphics are as good a teaching aid as they ever were, introducing so-called “body-syntonic” reasoning (where one starts to think about direction and orientation from the turtle’s perspective).
One of the easiest ways to try it is through Python’s built-in module. If you have Python installed you can get started by opening a terminal and entering:
$ python
>>> import turtle
>>> turtle.forward(100)
>>> turtle.right(120)
>>> turtle.forward(100)
>>> turtle.right(120)
>>> turtle.forward(100)
We’ll break there because a few things have happened, namely a window opening and a roughly equilateral triangle being rendered by an arrow that we’ll call a turtle. Having imported the Turtle module, the second line of code tells our turtle to go forward 100 pixels, leaving a trail behind it. Then it turns right 120 degrees and draws another edge. And then it does that again, completing the figure. The (internal) angles of an equilateral triangle are all 60 degrees, but if you put yourself in the mind of the turtle (turning clockwise) a little geometry (or ‘body syntonic’ reasoning) will show you that the required angle is 120 degrees.
Choose your text editing environment
We could tidy up that code a little by putting it in a loop, but we haven’t yet said what that is. We’ll do that in just a moment. First we’ll start to be a little more grown-up about our program entry. Rather than continue typing code straight into the Python interpreter, which is slightly awkward and error prone, let’s introduce an Integrated Development Environment (IDE).
For a simple project like this (which can be housed in a single file) all we need is a semi-decent text editor. You might one day go on to work on larger projects and want something a little more fully featured. In which case check out PyCharm, VSCode or something of that ilk. We’re going to use Geany which isn’t quite an IDE, but it will highlight our syntax and understand our indentation and hopefully make the rest of the feature easy to follow. Install Geany from your distro’s software application. Users of Debian-based distros might prefer to do it from the command line, like so:
$ sudo apt install geany
You should now be able to find it in the applications menu. Fire it up and enter the following code, which is our triangle code neatly implemented in a loop, and then wrapped in a function.
So let’s use Python’s range function to illustrate: import turtle
def triangle(): for side in range(3): turtle.forward(100) turtle.right(120)
The line beginning def defines our function and the next sets up our loop. Here we iterate over a variable called side which, by way of the range() function, takes on the values 0, 1 and 2 ( range() on its own returns a list). Note the two levels of indentation. Python is very fussy about this, since it’s used to demarcate blocks (instead of using a keyword or parentheses to do so). It doesn’t matter how many spaces you use (we suggest four for readability), so long as you’re consistent. Geany will try and help you by automatically maintaining indentation levels. It’s also set up to translate tabs to four spaces, so you can indent faster.
Now save your file as pyturtle.py . Geany will deduce that we’re writing Python code and highlight it appropriately. If you want to be proper about it the first line of this file should be:
#! /usr/bin/env python that tells the kernel what to do with this file if invoked as a script. Don’t worry about what that means at this stage, but do preface all of your Python code with the ‘shebang’ above. You can also run the code from with within Geany, either by pressing F5 or using the cog-like icon on the toolbar. That won’t do anything at this stage because all we’ve done is define a (rather simple) function. So let’s add some code which does something.
We can combine loops with a little bit of colour coordination to make, well, a thing. Add the following stanza to the end of pyturtle.py: if __name__ == “__main__”: import random colours = ["red”, “orange”, “yellow”, “green”, “blue"] for j in range(72): turtle.color(random.choice(colours)) triangle() turtle.left(5)
The ugly first line (and consequent indentation) isn’t really needed here, but it’s necessary for larger projects which only want program files to launch into action when called explicitly.