4 First Programs

Once you’ve got everything installed and setup, you’re able to crack on with writing some programs. Open Geany and type in the following line of code:

print("aloha world")

Once you’ve typed it in, save it as first.py and notice how the colors of the text will change. By saving the file with a .py extension the editor knows it’s a Python program and will use different colors for different sections of the code. This is useful because it makes it easier to spot typing errors (certain parts of the program will have the wrong color) and it makes the code easier to read and understand.

To make the computer process the code, you execute or run it by clicking on the Execute icon or by pressing the F5 key. If everything works ok, you should get the words aloha world written on your screen.

So what’s going on here? The command you gave to the computer was a function called print(). What this does is write to the screen anything that is inside the brackets (). The part “aloha world” is a type of data called a string (covered more in the chapter on variables) and the print function takes this string and displays it on the screen. You’re able to type anything you like between the “” and the computer will print it.

COMMENTS

When starting to write a new piece of software, I usually start off by putting some comments in the first lines. A comment is line of text that the Python interpreter ignores, and won’t process as a command. The comment starts with the # symbol and any text written after that until the end of the line is ignored by the computer.

# first program created on 2019.03.13

print ("aloha world")

Each line of commands you type in is technically known as a statement. Programs are written by combining many of these statements into a working whole, in order to create a software application.

I recommend typing in the code snippets as you come across them in each chapter, because people usually learn better by doing rather than just reading (it will also familiarize you with the error messages that occur if you make a typing mistake.)

<– back to contents