When dealing with computers, programming means giving the computer instructions on how to perform a particular task. All applications that you work with on a computer such as Word, Excel, image processing applications, video editing software etc. are all computer programmes. Of course those programmes are a lot more complex than the simple programmes that you will start with, but by the end of the course you may have an idea on how to tackle one of them. Also programmes such as those mentioned above were not written by one person but by a team of perhaps one hundred programmers. In fact big applications such as those mentioned are strictly not programmes but a group of hundreds of small programmes that interact with each other.
In this course you will start by building very small and simple programmes that will get bigger and more sophisticated as you learn more techniques, until eventually you will have a small business application that will be similar to an accounting application.
The underlying concepts of programming include Sequence, Variables, Input, Output, Selection and Modules. Many of those cocepts are interrelated and learning one is easier if you know the other one. The concept that is independent of all of the others is Sequence and therfore we shall start with that concept.
Sequence means things occurring in an orderly fashion one after the other. A computer program is series of commands or instructions that we give to the computer. We place those commands in the order we want them executed and the program runs or executes those commands in the order that we have specified, i.e the commands are executed in Sequence.
For our first example of programming we shall create a very simple program that allows a person to describe him or herself. For our example we shall use James Jones, a 28 year old teacher from Upper Hutt who wants to tell us all about himself. This means that he wants us to know his name, age, where he lives and what he works at.
Before writing a program for this we first plan out in ordinary English what exactly we want the program to do. This is called creating an algorithm.
The algorithm above is a plain English set of instructions of what we want the computer do do for us. As the computer does not understand English, or any other human language, those instructions must be translated into a language that the computer understands. In our case we will be translating them into Python and our translation is shown in Listing 1 below
Line 1 of this program begins with the "#" symbol. This tells us that it is a comment which means that the program will ignore the line. So if it is to be ignored by the program, why is it there in the first place? The answer is that the purpose of a comment line is to explain to another programmer who may look at the program later, what the program is doing. As we go through this course we will be looking at using comments to make our programs more understandable.
Line 2 starts with the word print(). Normally this means committing something to paper, but in Python it means to write something on the computer screen. So what will be written on the computer screen? The answer is the quoted text that appears between the brackets, i.e. My name is James Jones in this case.
Lines 3 - 6 work in exactly the same way as line 2 and thus there is no need to explain them here. The output of the program is shown below.
In the example above the four print() lines appeared on the screen so fast that they appeared to have been printed simultaneously. This impression that they were printed simultaneously is due to the high speed at which they were printed. They were in fact printed in sequence one after the other.
In order to make this fact more clear we shall create an example where there is a five second gap between each item being displayed in order to demonstrate that the command lines are actually executed in sequence. The algorithm is shown below.
The program based on the algorithm is shown below in Listing 2
#Sequence Example 2import timeprint("My name is James Jones")time.sleep(5) print("I am 28 years old")time.sleep(5) print("I live in Upper Hutt")time.sleep(5)print("I am a teacher")
Listing 2
In the program above the command time.sleep(5) is inserted between the print() commands. This sleep() command causes the program to hald for five seconds before executing the followig line, thus enabling you to see the line being executed one after the other.
Naturally how long the program pauses execution depends on the value between the bracket of the sleep()function. In this case it is five but you can make it any number you wish - within reason.
In our examples so far we have shown data for only one individual. We can, however, show data for as many individuals as we want by simply adding extra sets of lines.
How we print out one person's details is exactly the same as we print everyone else's and thus we have been easygoing with our algorithm above. The final instruction is simply to repeat the previous instructions twice more. We cannot, however, be this imprecise with our program code and therefore when writing the code below we must type out each individual instruction as shown in Listing 3. (Later in the course, however, we shall look at ways of circumventing this problem.)
Listing 3
Here lines 3 - 6, 8 - 11 and 13 - 16 display the details of three fairy tale characters. To add more characters we simply repeat the same group of four lines, each time with a different character.
In this lesson we looked at the concept of Sequence, i.e. that program lines are executed one after each other. The fact that the computer executes commands very fast may give the impression that the commands are executed all at once, but this is not true. There may be only a millisecond delay between each command, but the delay is still there.
To emphasies this delay we deliberately adde a sleep() command between each print() command.
Exercise 1: Using listing 1 as an example write a program that describes yourself, someone you know or make up a ficticious character and describe him or her. As well as name, age, address and job, describe the person further, i.e. their likes, dislikes, favourite sport, movie, rock band etc.
Exercise 2: Extend your example above to include sleep() commands between the print() commands. You don't need to adhere to 5 seconds, make the pauses as long or short as you wish - within reason of course.
Exercise 3: Extend your program for Listing 1 above so that the details of at least three characters are displayed. Again the likes, dislikes etc. of each character must be displayed.