Scratch is great for learning the basics of programming, but sooner or later you're going to run into its limitations. Now we're going to take a look at a popular general-purpose language, Python.

The first thing you need to know about Python is that, unlike Scratch, it's entirely text-based. This doesn't mean that it can't create graphics, but that the program code is text rather than drag-and-drop blocks.

Python 1

To create a program, then, we just need to open up a text editor. Any text editor will work; Leafpad is included by default on the Raspberry Pi, so we'll use this, but if you continue to program, it's well worth experimenting with a few different ones to see which works best for you (Geany is quite popular with new Python programmers). Nb, word processors such as LibreOffice Write or Abiword won't work, because they will mess up the formatting.

Open a new file in Leafpad and, on the first line, add:

#!/usr/bin/python

This line, rather cryptically called a shebang, tells the system to use the program python, in the folder /usr/bin/ to run the file. You'll need to add it to the start of all your Python programs. We can now get onto the guts of programming.

There's a long-standing computing tradition of having your first program output "Hello World!", and we're not going to break it here. Leave the second line blank (not strictly necessary, but it makes your code easier to read), and on the third type: print "Hello World!" and save your work in a file called hello.py.

To run the program, you need to open a terminal and navigate to where you saved the file. Run chmod a+x hello.py to tell the system that the file is executable, and enter ./hello.py to run it. You should see Hello World! appear on the screen. This shows us that the system is running properly, but it's not a very useful program.

As with the Scratch project, we'll add some user input. However, with Python we'll need to add a variable to store what the user types. Delete the Hello World line (leaving just the shebang), and add the line:

name = raw_input('what is your name? ')

This line creates the variable name, displays the prompt 'What is your name? ', and stores what the user types in name. We have to enclose this in inverted commas so that the computer knows it's a single chunk of text. We can then use this variable to make our print statement a little more personal with the line:

print 'Hello', name

Since the computer will run the commands in order, this one needs to be below the previous one. If they are the other way round, the program will throw an error because we are using a variable before we have created it. You can now save the file, and enter ./hello.py at the command line to run the program.

Decisions decisions

This makes the program a little more functional, but it's still pretty lifeless. It just follows the same two steps, then finishes. To make it useful, we need to add a decision step, where the computer looks at the input, and performs different actions depending on what it finds.

Remember the if block in Scratch? Well, we can use the same thing here. The basic structure of the block is:

if <expression> :
<indent> code block

<expression> must be replaced with anything that can be true or false. For example, 1 > 2, or more usefully, num > 2 where num is a variable. In our case, we'll check if the name entered is a particular value:

if name == 'Ben' :

Why ==? Well, computers (and programmers for that matter) don't deal well with ambiguity. Each symbol or word we use should have precisely one meaning, otherwise things get confusing. = is used to assign a value to a variable, so we need to use something else to check equality. Again, we have to enclose Ben in inverted commas so the computer knows it's text. The colon tells it that we've finished our expression and we're about to tell it what to do.

We may want this if command to run more than one line of code, so we need a way to group code into blocks. In Python, this is done using indents (Python is more-or-less unique in this respect, and this method is a bone of contention to Python-haters). Indents can be a space or a tab, but it's really important that you always use the same throughout your project, otherwise it can get horribly confusing, since it doesn't go on the amount of indentation, but the number of indents. Personally, I use two spaces for each indent, because that's the way I was taught, and I'm too stubborn to change.

So, what do we want the computer to do if name == 'Ben'? Well, obviously, we want it to greet him in the appropriate manner:

if name == 'Ben' :
print "Ben, you're awesome"

Note the two spaces at the start of the second line. Note how we use double speech marks. This is because the text we're enclosing has an apostrophe in it. We don't want to be rude to other people, so we'll add an else block that runs whenever the if expression is false:

else :
print 'Hello', name

One last feature we'll add to our program is a loop. This will work much like the one we added to our Scratch program, except that it won't only run 24 times, it'll keep running until we tell it to stop.

We do this using a while loop and the syntax:

while <expression> :
<indent>code block

We can tell the program to stop by entering the name quit. So, our while loop will be:

while name != 'quit' :

Solving problems

Python 2

Don't ask us why, but exclamation marks are often used to mean not in programming. But this still leaves us with a bit of a problem. If we put it before name = raw_input… then it will throw an error, saying it doesn't know what name is. If we put it after, it will only ask us to enter a name once, then keep spitting out its greeting indefinitely.

To solve this, we can simply assign the empty string to name before while. This stops it erroring, and will always trigger the while expression. So, our little program now looks like this:

#!/usr/bin/python

name = ''

while name != 'quit' :
name = raw_input('What is your name? ')

if name == 'Ben' :
print "Ben, you're awesome"
else :
print 'Hello', name

Note the four spaces before each print line. This is because they're indented twice: once for the while loop and once for the if statement. You can save this as hello.py, as before, and run it with ./hello.py.

Where to go now?

If you've followed this tutorial and enjoyed writing your first programs, then you may be wondering what to do next. Well, both Scratch and Python are great languages to get started with, so first you have to pick the one that appealed to you.

If it was Scratch, the best place to start is at http://scratch.mit.edu. Here, you'll find loads of projects that other people have done that you can take a look at, and video tutorials and tours to help you learn the environment.

Python is a far more popular language in the real world, so you'll find many more resources to help you learn. The official website has a tutorial, which explains the language well, but can be a bit dry. There are a number of excellent books on the subject (such as Dive into Python, which is included on the DVD, or can been read for free at www.diveintopython.net).

Print subscribers to Linux Format can access all of our previous Python tutorials via the archives on www.linuxformat.com, and our Code Concepts series, which helps introduce the key ideas behind programming.