So, you've completed the first tutorial which basically gets you set up with Geany and now you're ready to start programming in Python.
The first thing to say is that there are two versions of Python, 2 and 3. We are going to be using version 3 for two reasons. First, it seems to make sense to use the latest version, and second, we are going to use things that won't necessarily work with version 2.
To make sure we are using Python 3, we need to do a minor change to Geany.
Start up Geany and then open a Python file. You should find the one from the last tutorial in Recent Files under the File menu.
Now go to the Build menu and select Set Build Commands...
Click and you'll see the following window. Now you need to look near the bottom and locate Execute. The box to the right is editable and you will see that it reads python "%f". Change it so that it reads python3 "%f".
Click OK, and we are really ready to roll.
Input and output
Most programs fall into the same pattern. Get some data (input), do something with the data (processing) and then give some sort of result (output).
So, let's write a simple program like that.
That's three lines of code and, as you can no doubt see, it asks for your name (input), constructs a reply (processing) and then prints it out (output).
It would be a good idea to type that into Geany, save it as "example2.py" and hit the paper plane to run it.
It works, right? If it doesn't, you typed something wrong. Make sure that the program is exactly as shown above and make sure that there are no spaces at the beginning of each line (this is important).
How does it work?
In the first line, we see what is known as a function, it's called input and it gets some typing from the keyboard. Functions are like sub-progams, they perform some sort of... well, function. They often take some data as input and they sometimes provide a result. input does all of these things. The data it takes as input is the text contained in the braces following the name, i.e. "What's your name?".
It uses this as a prompt and then waits for you to type something in. When you hit the return key it finishes and provides what you typed as it's output.
We need to talk briefly about data. There are two fundamental types of data: numbers and text.
Numbers can be whole numbers, also known as integers, like 1, 42 or 1096. Or they can have a decimal part like 3.1459. These we call real numbers.
Text is anything you can read, or print, like words, spaces and punctuation marks. We call these strings and a string is represented as characters enclosed in quotes. "What's your name?" is a string consisting of 17 characters.
Ok, that's enough about data for now.
How it works, continued
We give input a string as input and it provides us with another string as output (what we typed in). Now we have to do something with it. And what we do is put it in a variable.
You really need to know about variables because they are fundamental to programming. A variable is a thing that takes a value, and once it has that value, when you use the variable name in a program, you get that value.
Here's an example :
The equals sign means 'give the value to', so x = 35, means give the value, 35, to x. What is printed is the sum of the values of x and y, which is, of course, 42.
How it works, continued, again
So what the first line of the program does is get the string that is typed in and put it into the variable called name.
Next we use another variable called reply and give that the value of the string 'Hello ' plus the value of name (and when I say 'plus' I mean that the strings are joined together, or concatenated).
The last line uses the function print to print the result on the screen.
That's your first Python program.
The next tutorial is here.