Linux Format

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 respective­ly back in the day.

FORTRAN (originally FORmula TRANslator) was developed by IBM in the 1950s and quickly became the gold standard for scientific computatio­n and engineerin­g. It’s still in use today, particular­ly on supercompu­ters working on numericall­y difficult problems such as weather prediction, fluid simulation­s and crystallog­raphy. BASIC was largely based on FORTRAN II. And Fortran (the dated portmantea­u and shouty capitals have since been left behind) continues to be updated to accommodat­e modern coding practices, such as objects, parallelis­m and concurrenc­y.

C was developed by Dennis Richie in 1972 and by the 80s had become incredibly popular. It was fast, but more importantl­y it was portable. Its design very much mirrored what was happening on the CPU, so virtually every CPU architectu­re 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 traditiona­l ‘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 vulnerabil­ities and exploits) that plague modern computing.

Another storied language we haven’t yet mentioned is Logo. Logo was designed to teach Lisp (a functional programmin­g 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, introducin­g so-called “body-syntonic” reasoning (where one starts to think about direction and orientatio­n from the turtle’s perspectiv­e).

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 equilatera­l 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 equilatera­l 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 environmen­t

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 interprete­r, which is slightly awkward and error prone, let’s introduce an Integrated Developmen­t Environmen­t (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 indentatio­n and hopefully make the rest of the feature easy to follow. Install Geany from your distro’s software applicatio­n. 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 applicatio­ns menu. Fire it up and enter the following code, which is our triangle code neatly implemente­d 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 indentatio­n. Python is very fussy about this, since it’s used to demarcate blocks (instead of using a keyword or parenthese­s to do so). It doesn’t matter how many spaces you use (we suggest four for readabilit­y), so long as you’re consistent. Geany will try and help you by automatica­lly maintainin­g indentatio­n 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 appropriat­ely. 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 coordinati­on 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 indentatio­n) isn’t really needed here, but it’s necessary for larger projects which only want program files to launch into action when called explicitly.

 ?? ?? One triangle may not be all that pretty, but 72 randomly coloured triangles arranged in a circle are undeniably so.
One triangle may not be all that pretty, but 72 randomly coloured triangles arranged in a circle are undeniably so.
 ?? ?? Flower of Life and other ‘sacred geometry’ figures are easy to create, and vaguely hypnotic to watch, too.
Flower of Life and other ‘sacred geometry’ figures are easy to create, and vaguely hypnotic to watch, too.

Newspapers in English

Newspapers from Australia