algorithmic art workshop

Viitaniemen koulu, 16...17 March, 2017

by Risto A. Paju

Code comments

In Python and gnuplot, lines starting with # are ignored from the actual code. They are called comments, and can be used to leave notes about what you're doing:

    # Clockwise rotation = negative angle
    x, y = rotate(x, y, -45)
In addition, you can use them to hide code you're not using right now, but might want to use later:
#ninit = 1000
ninit = 100000

Linux/Unix terminals

Having "&" at the end of commands puts them to the background, away from the terminal. It should be used when opening other windows, for example "gedit ifs.py &" or "eog ifs.png &".

Use the up arrow to cycle through previous commands, so you don't need to type the same thing again.

Copying and pasting text is very convenient with the middle button: highlight the text you want to copy, then paste it with the middle button.

List files with time ordering: "ls -lrt". This can be useful in showing which files were last modified or created.

Longer commands and filenames can be completed with the Tab key (left of Q). For example, typing "gnup" and Tab should get you gnuplot.

Where did that download go? Try "find -name ifs.py"

Python hints

Proper indentation is important in Python. For example in

def rotate(x, y, degrees):
    s = math.sin(math.pi*degrees/180)
    c = math.cos(math.pi*degrees/180)
    return c*x - s*y, c*y + s*x
the three lines in the function body must start at the same column. Good editors like Emacs can help with this; use the Tab key.

Statements like "x = 2" mean assignment, rather than a mathematical equation: the value of 2 is assigned to x. So we can have lines like

  x = x + 1
which doesn't make sense in mathematics, but here it means: calculate x + 1 and save the result into x. This way, we can iterate a function f by doing x = f(x) repeatedly.

Powers are written with **, for example x**2 means x2.

The random() function gives numbers between 0 and 1. So if you need numbers from -1 to 1, use 2*random() - 1.

Fractions such as 1/2 make zero: Python considers 1 and 2 whole numbers, so the result of the division is rounded to a whole number. Use 0.5 or 1.0/2 instead.

Some common math functions such as sin and cos are found in the math module: first we "import math" early in the program, then later use these functions as math.sin(x) for instance. Another common function is math.sqrt().

Interactive Python

Running "python" without any files gets you the interactive mode. This can be useful for testing certain features. For example, you can use it as a calculator:

  >>> 3**2 + 4**2
  25
You can exit the interactive mode by pressing Ctrl+D.

Working at home or elsewhere

You can install Python and gnuplot on many operating systems, but if you are serious about programming, I recommend using Linux. Linux Mint is a good distribution to start with. Most Linux distributions already have Python, but you may need to install gnuplot.

You can try Linux without permanent installation, booting from a CD/DVD or a USB stick. Unfortunately, many of these live versions do not have gnuplot. Scientific Linux provides a live version with Python and gnuplot, the LiveDVDextra.

back to main page