Archive for December, 2011

Programmer Tips


If you are getting into programming, you might want to consider a proper editor rather than using the Python IDE (Integrated Development Environment).

Top computer programmers use advanced text editors to write their code. It’s not a good idea to use word processing software, as this puts all kinds of formatting information into your files.

My sources tell me that two of the best editors around are Geany and Vim.

Geany is a nice simple piece of software that you will find easy to use. It’s a great idea to get used to using Geany rather than the Python IDE. Geany can do lots of things that the Python editor can’t. For instance, if you want to have a go at programming in C++ or Java, Geany will make it easy to do so and to run your programs.

If you are up for more of a challenge, you might consider Vim. Many people consider Vim to be the most powerful editor available. It certainly is packed with useful features. If you want to have a go, you might find this free book helpful: A Byte of Vim.

One thing that will certainly help you become a better programmer is to learn to type. This site has some nice free lessons.

Happy Coding



Python #5 Mathematical Operators


This is just a quick post about maths in Python. Computer languages use slightly different symbols for mathematical operations. Here’s a few examples from Python 3.

The easiest way to see this working is to use the interpreter (the first window that opens up when you start Python – the one with >>>).

Adding and taking away are the same:  just type:  “1+1” and : “1-1” and you’ll see.

Multiplcation is done by the * symbol. Division by /. Try a few out!

There are some other nice division operators that might be new to you. // gives the integer division. So if you type 7//2 you should get the answer “3”. The other one is called “modulus”. This gives the remainder after division has happened, so 7%2 will return “1”.

There are also some shortcuts you can do. If you want to add a number to a variable and then put the answer back in the variable you can do it this way. Let’s say you have put the value 1 into a variable i (type in “i=1” and hit enter). Now type “i+=5” and press enter. If you now tell the interpreter to “print(i)”, it will give you the answer “6”. You can do this with the other operators I’ve mentioned also. Have a play! You might even want to write a simple program that does something like asking for some numbers, doing some sums with them, and then printing the results.


Python #4: The “for” Loop


There’s another nice way to get the computer do do things a number of times: the “for” loop. This time, we get to specify exactly how many times we want the thing to happen. Try this little program:

The loop names a variable “i” and tells the computer to keep going, adding one each time, for ten repetitions. If you run the program, you’ll see it starts at zero and ends at nine. This is because the computer starts counting at zero. If we want to make it do one to ten we need to make a little change.

The  “range”  command lets us specify a start and end point, separated by commas. We can also tell the machine what steps to go in.

So, if I alter my program to this:

It will count down from 10 to 1 in steps of -1.



Python #3 the while loop


Now we are going to move on to one of my favourite commands – the “while loop”. The programs we have looked at so far did something once and then stopped. If you want to repeat part of a program a number of times, you need to use a loop.

In the image you can see a program called It begins with the usual comment lines and the input line we have used before. The important new feature is the while command.

This compares the contents of the variable “name” with … well nothing. The while loop tells the computer to keep looping through the following few lines until “name” is empty. In other words, if the user presses the enter key, instead of entering a name, the program will exit the loop. Notice the exclamation mark and equals sign: this means “is not equal to”. Also notice the colon at the end of the while command. Just like with the “if” statement, this tells the computer that some instructions are coming which it must perform if the preceding condition is met.

So, the program will keep looping around asking for names and saying: “hello”, until we press enter.

Notice the indentations. Everything in the while loops is indented.The print lines are indented further as they are conditional on the preceding if commands. Another thing you might notice is that I get the input before entering the loop and then again at the end of the loop. I need to do this because, if I don’t have anything in “name” when it hits the while statement, the condition will not be met and the loop will be skipped. I need to ask for input again before going back to the start of the loop, so that the code in the loop will have some new data to work on.

Infinite Loops

One of the great things about computers is that they don’t argue, even when you ask them to do something stupid. For instance, look at this short program:

while 1>0:
    print(“You suck and I rule!”)

If you run this program, it will print the string over and over, until you force the program to quit by closing the window. This is the sort of program it used to be great fun to write on BBC micros in Dixons. It used to really annoy the salesmen (of course in those days we wrote in BASIC). The fun bit is in the while command. It asks the computer to keep doing the next line while one is greater than zero. Now since one is always going to be greater than zero, it’ll keep doing it for ever. On the whole, you’ll want to avoid creating infinite loops in your programs!


Python #2


Okay, that first program was very simple. The next couple of ideas I want to introduce are comments and variables. If you put the “#” sign at the start of a line, the computer ignores it. Programmers use commenting to make their  code more readable for human beings. Here’s a short program that includes some comments and a variable.

The comment lines are here to explain what is going on.

Next, I create a variable called “name” and use it to store the string “antiloquax”. Strings are a type of variable that stores things simply as a string of characters. You can’t do maths on strings, so if you want your variable to store numbers for doing calculations, you have to use define it differently (we’ll cover this soon).

So, now when the computer prints: “Hello”, it also prints whatever is in the memory “box” called “name”.

That’s all well and good, but it would be nice to interact with the computer a bit. This next version of the program introduces inputs.

The input command makes the computer expect an input from the user. Whatever you type in goes into the variable “name”. I’ve made a slight addition to the print command in order to add a full-stop to the end of the line!

Al this is quite simple, I think you’ll agree. I’m going to introduce the “if” command next.

The if command is very important. Sometimes we want the computer to make a decision based on a condition. You can see what is happening here: if the user enters the name “mark” then the computer says hello. If some other name is entered, the computer prints the longer statement saying it doesn’t know the user.

There are a couple of important points to remember about this. Firstly: when we are comparing two values to see if they are the same, we use 2 equals signs, in Python. Secondly, at the end of the if statement (and the else statement) there’s a colon. Thirdly, the line  (or lines) following the colon are indented. This makes it very easy to see the parts of the program that are only going to be used if certain conditions are met.

Right, that’s all for now!